8

When setting up meta boxes in a custom post type I've been do so using the add_meta_boxes hook, e.g.

add_action('add_meta_boxes', 'meta_box_setup');
function meta_box_setup()
{
    add_meta_box( 
        'mb_movie_review', 
        'Movie Review Details', 
        'display_movie_review_mb', 
        'movie-reviews', 
        'side', 
        'high' 
    );
}

But I've just read this tutorial which does the following using the admin_init hook, like so...

add_action('admin_init', 'meta_box_setup');
function meta_box_setup()
{
    add_meta_box( 
        'mb_movie_review', 
        'Movie Review Details', 
        'display_movie_review_mb', 
        'movie-reviews', 
        'side', 
        'high' 
    );
}

This is the first time I've seen it done using the admin_init hook.

Question

The latter method (using admin_init) does work but ...

  • is this the preferred method? (i.e. more optimized?)
  • just a different way of achieving the same results, or
  • a bad way to add meta boxes? (if so, why?)
  • 4
    Have a look at this list: codex.wordpress.org/Plugin_API/Action_Reference - it doesn't matter which one you use as long as it's not too early and not too late. It's best to use intuitive and predictable hooks, so add_meta_boxes is preferred. – Paul Sep 26 '13 at 22:15
  • @Paul Great. Can you post this as an answer (so I can credit you)? – sleeper Sep 26 '13 at 23:48
  • 3
    Agreed, I think that simply the tutorial author was not aware of add_meta_boxes. We have even "add_meta_boxes_$post_type" for finer control. – brasofilo Sep 27 '13 at 5:18
4

Have a look at this list: http://codex.wordpress.org/Plugin_API/Action_Reference

  1. It doesn't matter which one you use as long as it's not too early and not too late. It's best to use intuitive and predictable hooks, so add_meta_boxes is preferred. Someday in the future WordPress may change something and by using the most appropriate hooks you increase your chances that your code will still work in the future.
  2. There is one exception that I can think of to that. Sometimes (in cases that are very unlikely to happen) you may need to for instance call add_theme_support() which is generally used with after_setup_theme action hook only for logged in users that are administrators but this hook doesn't allow you to access this information yet. You would therefore have to probably use set_current_user or init action hooks instead (after doing some research if it's safe to do that).
|improve this answer|||||
2

There are several other hooks:

do_action( 'add_meta_boxes', $post_type, $post );

and immediately afterwards runs:

do_action( "add_meta_boxes_{$post_type}", $post );

If you want to perform different abort checks from within the hook/callback that actually registers the meta boxes, use one of the above as the \WP_Screen object isn't filled as early as admin_init runs.

If you used the more generic add_meta_boxes hook, you can check against its first argument:

function register( $post_type, $post )
{
    if ( get_current_screen()->post_type !== $post_type )
        return;

    add_meta_box( /* etc. */ );
}

If you don't want to add your meta box as long as the post hasn't been saved at least once, you could check

if ( 'add' !== get_current_screen()->action )
    return;

and so on. Conclusion: If you want to narrow things down, use the hooks shown above.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.