hlashbrooke / WordPress-Plugin-Template

A robust code template for creating a standards-compliant WordPress plugin.
https://hughlashbrooke.com/
GNU General Public License v2.0
1.03k stars 329 forks source link

admin->add_meta_boxes() #17

Closed yivi closed 10 years ago

yivi commented 10 years ago

I'm sure it's me doing something wrong... but I'm a bit confused by this:

In my main plugin file I do:

function yosilose_init() {
    $instance = Yosilose::instance( __FILE__, '1.0.0' );

    if ( is_null( $instance->settings ) ) {
        $instance->settings = Yosilose_Settings::instance( $instance );
    }

    return $instance;
}

$y = yosilose_init();

$y_post = $y->register_post_type(
    'ys_reviews',
    __( 'Reviews', $y->_token ),
    __( 'Review', $y->_token )
);

$y_cats = $y->register_taxonomy(
    'ys_category',
    __('Categories', $y->_token),
    __('Category', $y->_token),
    array($y_post->post_type)
);

$y->admin->add_meta_box(
    $y_post->post_type . '_main_meta',
    __('Various', $y->_token),
    array($y_post->post_type),
    'side',
    'default'
);

But then I get an error about "add_meta_box" not being defined. The call gets to the Admin API and the admin API calls wordpress' version of add_meta_box... but seemingly can't find it in scope.

Which is weird, I can see in the call list that WP is completely loaded by that point:

( ! ) Fatal error: Call to undefined function add_meta_box() in /Users/yivi/Documents/Sites/yosilose-2.0/wp-content/plugins/yosilose/includes/lib/class-yosilose-admin-api.php on line 239
Call Stack
#   Time    Memory  Function    Location
1   0.0238  344440  {main}( )   ../edit.php:0
2   0.0822  403176  require_once( '/Users/yivi/Documents/Sites/yosilose-2.0/wp-admin/admin.php' )   ../edit.php:10
3   0.0825  418488  require_once( '/Users/yivi/Documents/Sites/yosilose-2.0/wp-load.php' )  ../admin.php:31
4   0.0826  428832  require_once( '/Users/yivi/Documents/Sites/yosilose-2.0/wp-config.php' )    ../wp-load.php:29
5   0.0835  509648  require_once( '/Users/yivi/Documents/Sites/yosilose-2.0/wp-settings.php' )  ../wp-config.php:62
6   0.3034  14122096    include_once( '/Users/yivi/Documents/Sites/yosilose-2.0/wp-content/plugins/yosilose/yosilose.php' ) ../wp-settings.php:215
7   0.3093  14424872    Yosilose_Admin_API->add_meta_box( ???, ???, ???, ???, ???, ??? )    ../yosilose.php:64

Shouldn't this work as it is? Again, I feel it's very likely to be user error... but can't understand why it thinks add_meta_box is undefined at that point. :(

Thanks and regards

hlashbrooke commented 10 years ago

The most likely issue is that you're running the $y->admin->add_meta_box(); function in the main plugin file here. That needs to be called on the add_meta_boxes hook otherwise it won't work correctly. If you call it in the main plugin file it will give the error that you're seeing there.

yivi commented 10 years ago

Once more, it was a classic case of "holding it wrong". Got it now, and also have a better understanding of how the templates+apis are meant to be used.

Many thanks for all your help. I hope I can contribute back a bit later on.

hlashbrooke commented 10 years ago

No worries - happy to help :)

At some point I'm going to create an example plugin that shows you how to use this template in a practical way, but I haven't had time to do that just yet.