admin管理员组

文章数量:1122846

I get messages in the debug.log for PHP Deprecated:

Optional parameter $function declared before required parameter $page is implicitly treated as a required parameter

code

function add_meta_box($id, $title, $function = '', $page, $context = 'advanced', $priority = 'default'): void
{
    require_once(ABSPATH . 'wp-admin/includes/template.php');
    add_meta_box($id, $title, array(&$this, $function == '' ? $id : $function), $page, $context, $priority);
}

I get messages in the debug.log for PHP Deprecated:

Optional parameter $function declared before required parameter $page is implicitly treated as a required parameter

code

function add_meta_box($id, $title, $function = '', $page, $context = 'advanced', $priority = 'default'): void
{
    require_once(ABSPATH . 'wp-admin/includes/template.php');
    add_meta_box($id, $title, array(&$this, $function == '' ? $id : $function), $page, $context, $priority);
}
Share Improve this question asked Jun 25, 2024 at 11:54 Vincenzo PiromalliVincenzo Piromalli 1989 bronze badges 1
  • just an aside, why does add_meta_box exist outside the admin? Also &$this is unnecessary and a holdover from PHP 4.x, you should never add a & in any WordPress code written in the last 15 years, it can be removed safely with no downsides, all you need is $this without the ampersand. Also is is_admin() is false should it not return early? – Tom J Nowell Commented Jun 25, 2024 at 12:22
Add a comment  | 

1 Answer 1

Reset to default 3

At your function declaration you need to set the required parameters first, the optional second, because you couldn't skip $function if $page has to be given:

function add_meta_box($id, $title, $page, $function = '', $context = 'advanced', $priority = 'default'): void
{
    require_once(ABSPATH . 'wp-admin/includes/template.php');
    add_meta_box($id, $title, array(&$this, $function == '' ? $id : $function), $page, $context, $priority);
}

Or make the $page variable optional too:

function add_meta_box($id, $title, $function = '', $page = null, $context = 'advanced', $priority = 'default'): void
{
    require_once(ABSPATH . 'wp-admin/includes/template.php');
    add_meta_box($id, $title, array(&$this, $function == '' ? $id : $function), $page, $context, $priority);
}

Or make both required:

function add_meta_box($id, $title, $function, $page, $context = 'advanced', $priority = 'default'): void
{
    require_once(ABSPATH . 'wp-admin/includes/template.php');
    add_meta_box($id, $title, array(&$this, $function == '' ? $id : $function), $page, $context, $priority);
}

本文标签: pluginsPHP Deprecated function Optional parameter function