Make WordPress Core

Opened 14 years ago

Closed 14 years ago

Last modified 14 years ago

#5924 closed defect (bug) (fixed)

add_meta_box function not defined at plugin init

Reported by: jhodgdon Owned by:
Milestone: 2.5 Priority: normal
Severity: normal Version: 2.5
Component: General Keywords: has-patch
Focuses: Cc:


The API for adding sections to the post/page/link edit screens has changed for WordPress 2.5 -- previously you would use, for instance:

  add_action('dbx_page_advanced', 'your_function' );

and in 2.5 you are supposed to use the add_meta_box function.

The problem is that if you want your plugin to work in pre-2.5 and 2.5, you need to detect which version is being used. The suggested logic is:

if( function_exists( 'add_meta_box' )) {
  add_meta_box( etc. );
 } else {
  add_action('dbx_page_advanced', etc. );

The problem with this is that when the plugin is first loaded, and also during the 'init' action, the function add_meta_box is not yet defined. It is loaded later, from file wp-admin/includes/template.php

My suggestion is to move the add_meta_box function into a different file, such as wp-includes/plugin.php, which is where the add_action and add_filter functions are defined.

Tested in [6914]; I'll add a patch soon to move the function.

Attachments (1)

move_func.diff (2.7 KB) - added by jhodgdon 14 years ago.
patch that moves the add_meta_box function from wp-admin/includes/template.php to wp-includes/plugin.php

Download all attachments as: .zip

Change History (6)

14 years ago

patch that moves the add_meta_box function from wp-admin/includes/template.php to wp-includes/plugin.php

#1 @jhodgdon
14 years ago

  • Keywords has-patch added

This patch fixes the problem. I think add_meta_box belongs more logically in wp-includes/plugin.php anyway, as it is a function for plugins to call.

#2 @ryan
14 years ago

How about an admin_init action. Throwing more crap in wp-includes will bloat front page loads even more than they already are.

#3 @jhodgdon
14 years ago

Yeah, Ozh suggested doing the test logic during the admin_menu action. That works in 2.3.x and 2.5 bleeding.

Go ahead and mark this as won't fix then; I can add that suggestion to the Codex migrating page, which at least is workable.

#4 @ryan
14 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [6915]) Add admin_init action. fixes #5924

#5 @ryan
14 years ago

I went ahead and added an admin_init action to complement the init action. It's in admin.php after all the setup but before plugin page loading.

Note: See TracTickets for help on using tickets.