Creating an “admin only” meta box with WPAlchemy. Getting a fatal error on front-end when using current_user_can

I’m trying to create an “admin only” meta box using WPAlchemy. The box for example may contain a “feature post” check box and other functionality.

How can I show this meta box for only the admin, yet have it work properly? I’m currently using the following code below, but I receive a “Fatal error: Call to a member function the_meta() on a non-object” when trying to echo the value of whatever is inside on the front-end. Everything works as intended if I don’t use current_user_can and works also if I’m logged in as admin and viewing the front-end.

Read More
if (current_user_can('administrator')) {
$custom_admin_mb = new WPAlchemy_MetaBox(array(
    'id' => '_custom_admin_meta',
    'title' => 'Admin only',
    'template' => TEMPLATEPATH . '/custom/admin_meta.php',
));

}

Related posts

Leave a Reply

1 comment

  1. try the following:

    $custom_admin_mb = new WPAlchemy_MetaBox(array(
        'id' => '_custom_admin_meta',
        'title' => 'Admin only',
        'template' => get_stylesheet_directory() . '/custom/admin_meta.php',
        'output_filter' => 'my_output_filter',
    ));
    
    function my_output_filter() {
        if (current_user_can('administrator')) return true;
        return false;
    }