Ticket #5924 (closed defect: fixed)

Opened 5 months ago

Last modified 5 months ago

add_meta_box function not defined at plugin init

Reported by: jhodgdon Assigned to: anonymous
Priority: normal Milestone: 2.5
Component: General Version: 2.5
Severity: normal Keywords: has-patch
Cc: jhodgdon

Description

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

move_func.diff (2.7 kB) - added by jhodgdon on 02/19/08 19:46:01.
patch that moves the add_meta_box function from wp-admin/includes/template.php to wp-includes/plugin.php

Change History

02/19/08 19:46:01 changed by jhodgdon

  • attachment move_func.diff added.

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

02/19/08 19:46:50 changed by jhodgdon

  • keywords set to has-patch.

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.

02/19/08 19:47:14 changed by ryan

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

02/19/08 20:17:14 changed by jhodgdon

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.

02/19/08 20:24:51 changed by ryan

  • status changed from new to closed.
  • resolution set to fixed.

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

02/19/08 20:25:54 changed by ryan

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.