jgravois
jgravois

Reputation: 2579

Restricting Wordpress Admin Options to Admins

I thought this would be an easy thing but many hours have gone by and still no results.

I am creating a Wordpress plug-in that should only appear in the dashboard if the user is an admin. I wrapped my hooks in the is_admin() method, but when I log in as a user who is just a subscriber, I still see the menu.

Isn't it just that easy???

Here's a code except starting right below the comment section to register the plugin... everything not shown is just functions doing their job ...

if( is_admin ){ 

add_action('admin_menu', 'ppm_talentexpo_add_page');
add_action('admin_menu', 'ppm_expos_submenu');

} // end is_admin

function ppm_talentexpo_add_page() {
    $mypage = add_menu_page('Talent Expo', 'Talent Expos', 2, 'ppmtalentexpo', 'jwg_talentexpo_options_main_page', '/wp-admin/images/media-button-music.gif' , 21);
    add_action( "admin_print_scripts-$mypage", 'jwg_ppmtalentexpo_admin_head' );
} // end function

Upvotes: 0

Views: 171

Answers (1)

Jared Paolin
Jared Paolin

Reputation: 247

It looks like you left out the parentheses when calling is_admin in the conditional.
Try

if( is_admin() ){
    add_action('admin_menu', 'ppm_talentexpo_add_page');
    add_action('admin_menu', 'ppm_expos_submenu');
}


Also if you're not using an older WordPress install, add_menu_page allows you to specify a capability that WordPress will check for. This lets WordPress manage showing the item or not.
So you can define a custom capabilty (or reuse an existing one), and the menu should take care of itself.

add_action('admin_menu', 'ppm_talentexpo_add_page');
add_action('admin_menu', 'ppm_expos_submenu');

function ppm_talentexpo_add_page() {
    $mypage = add_menu_page('Talent Expo', 'Talent Expos', 'my_custom_talent_expos_capability', 'ppmtalentexpo', 'jwg_talentexpo_options_main_page', '/wp-admin/images/media-button-music.gif' , 21);
    add_action( "admin_print_scripts-$mypage", 'jwg_ppmtalentexpo_admin_head' );
}

Upvotes: 1

Related Questions