1

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

}

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;
}
  • 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

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.