Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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',
));

}

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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;
}
share|improve this answer
    
Thanks Dimas, Worked a treat! I also learnt about get_stylesheet_directory() - did not know that you could use that instead of TEMPLATEPATH. –  Andrew Jul 5 '11 at 1:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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