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 have a snippet of code I've written in the functions.php file which is simply to remove menu pages if the user cannot activate plugins. It works as intended; however it is showing a PHP error in the admin area only if the logged in user is an administrator.

Here's the code snippet:

<?php
/************ Remove admin menu items from anyone who isn't an admin ************/

if (!current_user_can('activate_plugins') ) { 
    function my_remove_menu_pages() {
        remove_menu_page('link-manager.php');
        remove_menu_page('tools.php');
        remove_menu_page('edit-comments.php');
    }
};

add_action( 'admin_menu', 'my_remove_menu_pages' );
?>

Here's the error:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'my_remove_menu_pages' not found or invalid function name in /var/www/vhosts/mydomain.com/httpdocs/wp-includes/plugin.php on line 406

I've tried rewriting the snippet a couple different ways, and each time it basically does the same thing. Works as intended, removing admin menu pages for less than admins, but gives the same error in the WP backend when logged in as admin. Any suggestions or thoughts on this?

share|improve this question
2  
Have you tried to put the add_action line inside the if (!current_user_can('activate_plugins') ) {? –  Mike Madern Feb 27 '13 at 14:47
    
I have been over thinking this since yesterday afternoon. Post your comment as an answer and I'll accept it. Worked like a charm. –  AndyWarren Feb 27 '13 at 14:56

2 Answers 2

up vote 3 down vote accepted

Put the add_action line inside the if (!current_user_can('activate_plugins') ) {:

<?php
/************ Remove admin menu items from anyone who isn't an admin ************/

if (!current_user_can('activate_plugins') ) { 
    function my_remove_menu_pages() {
        remove_menu_page('link-manager.php');
        remove_menu_page('tools.php');
        remove_menu_page('edit-comments.php');
    }

    add_action( 'admin_menu', 'my_remove_menu_pages' );
};
?>
share|improve this answer
    
Perfect. Thanks again. –  AndyWarren Feb 27 '13 at 15:21

You are only defining your function when a user cannot 'activate_plugins' but you are attempting to use the function all the time. You'd be better off doing what Mike Madern suggested in a comment:

if (!current_user_can('activate_plugins') ) { 
    function my_remove_menu_pages() {
        remove_menu_page('link-manager.php');
        remove_menu_page('tools.php');
        remove_menu_page('edit-comments.php');
    }

    add_action( 'admin_menu', 'my_remove_menu_pages' );
}

Or this:

function my_remove_menu_pages() {
    remove_menu_page('link-manager.php');
    remove_menu_page('tools.php');
    remove_menu_page('edit-comments.php');
}

if (!current_user_can('activate_plugins') ) { 
    add_action( 'admin_menu', 'my_remove_menu_pages' );
}

That should get rid of the Warning. I am still concerned about this though. You should be able to show and hide the various pages by manipulating user Roles and Capabilities. That is how I'd suggest you do this, rather than just brute force removing admin pages. You may cause yourself some trouble that way. You will need a plugin to do any detailed manipulation of user capabilities or write your own code for it.

share|improve this answer
    
Thanks for the answer. I am also using the Members plugin for roles and capabilities; however it does not give options to remove or restrict some of the admin menu pages (like the ones in my function). So that is the reason for removing them via a function. –  AndyWarren Feb 27 '13 at 15:02
    
Log into your site as a subscriber and you will notice that most of the pages do not show up. Admins, Editors, Authors, etc. all see different things. That is because capabilities are different. You should be able to accomplish this by manipulating capabilities for your users and Roles. For example, remove the 'edit_pages' capability and the "Pages" menu does not show up for that user/Role. –  s_ha_dum Feb 27 '13 at 15:19

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.