WordPress.org

Make WordPress Core

Opened 4 years ago

Last modified 19 months ago

#13937 new enhancement

Pass named arguments to add_meta_box()

Reported by: scribu Owned by:
Milestone: Future Release Priority: normal
Severity: minor Version:
Component: General Keywords: has-patch
Focuses: Cc:

Description

add_meta_box() currently takes 7 arguments!

It should accept an associative array of arguments instead. Advantages:

  • more readable code
  • easier to deprecate arguments in the future

Attachments (2)

add_meta_box.diff (2.4 KB) - added by scribu 4 years ago.
add_meta_box.2.diff (3.1 KB) - added by scribu 4 years ago.
Introduce wp_numeric_to_associative()

Download all attachments as: .zip

Change History (15)

scribu4 years ago

comment:1 scribu4 years ago

Ah, just realized that the patch should also change all the internal calls to add_meta_box().

It works, but it throws tons of deprecated warnings, as expected.

comment:2 scribu4 years ago

PS: The add_submenu_page() family of functions should get similar treatment.

comment:3 johnbillion4 years ago

That code block at the beginning of your patched add_meta_box() (lines 2779 to 2793) should be abstracted out to another function so it can be used in other functions, much like wp_parse_args can. Something like wp_get_args maybe.

scribu4 years ago

Introduce wp_numeric_to_associative()

comment:4 follow-up: scribu4 years ago

@johnbillion: thanks for the suggestion. I abstracted a smaller portion, to keep it more flexible. See add_meta_box.2.diff

comment:5 in reply to: ↑ 4 ; follow-up: johnbillion4 years ago

Replying to scribu:

@johnbillion: thanks for the suggestion. I abstracted a smaller portion, to keep it more flexible. See add_meta_box.2.diff

Nice one. Pedantic improvement if this goes in: no need for func_num_args and func_get_args to be assigned to variables (as they're only used once each), they can be used in place.

Maybe another ticket can be opened to identify other candidate functions for this treatment. I know it was brought up on wp-hackers a while ago. Certainly functions where more than half the parameters are optional would benefit from working like this.

comment:6 in reply to: ↑ 5 scribu4 years ago

Replying to johnbillion:

Pedantic improvement if this goes in: no need for func_num_args and func_get_args to be assigned to variables (as they're only used once each), they can be used in place.

Yes, unfortunatly the PHP manual says:

Note: Because this function depends on the current scope to determine parameter details, it cannot be used as a function parameter in versions prior to 5.3.0. If this value must be passed, the results should be assigned to a variable, and that variable should be passed.

comment:7 johnbillion4 years ago

Hmm, I think this might need a little more thought. Query string style parameters can't be used with this, only an associative array.

Maybe wp_parse_args could be extended with a third parameter, which is the list of parameter names (as passed as the second argument to wp_numeric_to_associative in Scribu's patch). Inside wp_parse_args we could check if the first parameter is a numerically indexed array and if so assign it named keys using wp_numeric_to_associative.

Sorry for the following paste but I'm sitting in bed on my tiny little netbook with nothing except a text editor installed on it.

function add_meta_box( $args ) { 
if ( func_num_args() > 1 ) { 
    _deprecated_argument( __FUNCTION__, '3.1', __('Passing individual arguments is deprecated. Use an associative array of arguments instead.') );
    $args = func_get_args();
}

$defaults = array( 
    'id' => '', 
    'title' => '', 
    'callback' => '', 
    'callback_args' => null, 
    'page' => '', 
    'context' => 'advanced', 
    'priority' => 'default', 
); 

extract( wp_parse_args( $args, $defaults, array( 'id', 'title', 'callback', 'page', 'context', 'priority', 'callback_args' ) ), EXTR_SKIP ); 

comment:8 johnbillion4 years ago

Ignore that last comment, it's nonsense. Bedtime for me.

comment:9 scribu4 years ago

  • Summary changed from Pass list of arguments as array in add_meta_box() to Pass named arguments to add_meta_box()

comment:10 scribu3 years ago

Related: #15066

comment:11 nacin3 years ago

  • Milestone changed from Awaiting Triage to Future Release
  • Type changed from defect (bug) to enhancement

comment:12 jltallon3 years ago

  • Cc jltallon added
Note: See TracTickets for help on using tickets.