WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 5 years ago

#13937 closed enhancement (wontfix)

Pass named arguments to add_meta_box()

Reported by: scribu Owned by:
Milestone: Priority: normal
Severity: minor Version:
Component: Options, Meta APIs 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 9 years ago.
add_meta_box.2.diff (3.1 KB) - added by scribu 9 years ago.
Introduce wp_numeric_to_associative()

Download all attachments as: .zip

Change History (16)

@scribu
9 years ago

#1 @scribu
9 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.

#2 @scribu
9 years ago

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

#3 @johnbillion
9 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.

@scribu
9 years ago

Introduce wp_numeric_to_associative()

#4 follow-up: @scribu
9 years ago

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

#5 in reply to: ↑ 4 ; follow-up: @johnbillion
9 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.

#6 in reply to: ↑ 5 @scribu
9 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.

#7 @johnbillion
9 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 ); 

#8 @johnbillion
9 years ago

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

#9 @scribu
9 years ago

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

#10 @scribu
9 years ago

Related: #15066

#11 @nacin
9 years ago

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

#12 @jltallon
9 years ago

  • Cc jltallon added

#13 @scribu
7 years ago

Related: #22100

#14 @DrewAPicture
5 years ago

  • Component changed from General to Options, Meta APIs
  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

No activity in a couple of years, and there doesn't seem to be a lot of interest in rocking the boat with this one. Closing as wontfix.

Note: See TracTickets for help on using tickets.