0

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);
}
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 at 12:22

1 Answer 1

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.