Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#15000 closed enhancement (duplicate)

add_meta_box() should accept array of post types

Reported by: coffee2code Owned by:
Milestone: Priority: normal
Severity: minor Version: 3.0.1
Component: Administration Keywords: has-patch
Focuses: Cc:


I often find myself defining an identical meta box for different post types. It would be nice if add_meta_box() accepted an array as its $page argument. For backward-compatibility the function will still accept a string. So for all else being equal, the same metabox could be added to multiple post types in a single line of code. Of course, if the meta box needs per-post_type configuration, add_meta_box() could be called separately for each post type.


add_meta_box( 'my-geo', 'Geolocation Info', array( &$this, 'geo_form' ), 'post', 'side' );
add_meta_box( 'my-geo', 'Geolocation Info', array( &$this, 'geo_form' ), 'page', 'side' );
add_meta_box( 'my-geo', 'Geolocation Info', array( &$this, 'geo_form' ), 'event', 'side' );

would become:

add_meta_box( 'my-geo', 'Geolocation Info', array( &$this, 'geo_form' ), array( 'post', 'page', 'event' ), 'side' );

Of course, the former would continue to work.

One could certainly loop through the post types manually to call add_meta_box(), but I think the proposed patch clarifies coding intention.

Patch is attached. (In the patch, I didn't indent the content of the newly introduced foreach() ... endforeach; since it would look like the whole function got changed when in fact I only made a very minor change. The function also has a precendent of a non-indented foreach. However, I would anticipate the actual commit might want the indentation, which can be provided.)

(Note: affects, or affected by, #13937)

Attachments (1)

15000.diff (1.6 KB) - added by coffee2code 4 years ago.
Aforementioned patch.

Download all attachments as: .zip

Change History (4)

coffee2code4 years ago

Aforementioned patch.

comment:1 nacin4 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #13305 which was wontfix'd.

comment:2 coffee2code4 years ago

A roundabout solution presented there, but so it goes. A straightforward foreach would be more concise:

foreach ( array( 'post', 'page', 'event' ) as $page )
    add_meta_box( 'my-geo', 'Geolocation Info', array( &$this, 'geo_form' ), $page, 'side' );

comment:3 hakre4 years ago

+1 for wontfix, iterating should be something a developer handles on it's own normally.

Note: See TracTickets for help on using tickets.