• I am using the following piece of code to hide the Users menu from the admin area.

    function remove_admin_menus()
    {
        // provide a list of usernames who can edit custom field definitions here
        $admins = array( 
            'Super Admin Username', 
        );
        // get the current user
        $current_user = wp_get_current_user();
        // match and remove if needed
        if( !in_array( $current_user->user_login, $admins ) )
        {
            remove_menu_page( 'users.php' );               //Users
        }
    }
    add_action( 'admin_menu', 'remove_admin_menus', 999 );

    This way only the named “Super Admin” can edit the user database.

    The question is, how do I hide access to Code Snippets to prevent other admins from deactivating the code?!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    There’s a checkbox on the Network Settings page: Menu Settings > Enable administration menus > Snippets.

    Unchecking this box will ensure that the Snippets menu on subsites is only visible to Super Admins.

    Hello @bungeshea, I’m looking for a solution to the same exact problem. Your response to @perchpole makes no sense to me. Where is the network settings page? I’ve looked everywhere in the WordPress admin dashboard and in the settings for the plugin.

    Can you give more clear instructions?

    Plugin Author Shea Bunge

    (@bungeshea)

    Hi @blackpassportstamps,

    Is your site a WordPress Multisite network? You can find the Network Settings page in the Network Admin, if so.

    If not, then the Snippets menu is already restricted to administrators on regular WordPress sites.

    @bungeshea No my site is not a multisite network. I have a developer working on my site but I don’t want him to see my woocommerce payment settings and secret keys (like stripe and amazon) so I added a snippet to hide that menu from his user role (which works).

    The problem is, with his user role he has to have both a (web developer) and (admin) user roles to be able to perform his work, which means he can see the snippets menu and potentially undo the code.

    I want to hide the snippets menu from his specific user ID. How can I do this? I tried to add this code .. but it doesn’t work.

    function hide_menu(){
    global $current_user;
    $user_id = get_current_user_id();
    // echo “user:”.$user_id; // Use this to find your user id quickly

    if(is_admin() && $user_id == ‘2638’){
    remove_menu_page( ‘admin.php?page=snippets’ );
    remove_submenu_page( ‘admin.php?page=snippets’, ‘admin.php?page=snippets’ );
    remove_submenu_page( ‘admin.php?page=snippets’, ‘admin.php?page=add-snippet’ );
    remove_submenu_page( ‘admin.php?page=snippets’, ‘admin.php?page=edit-snippet’ );

    }
    }

    add_action(‘admin_head’, ‘hide_menu’);

    Plugin Author Shea Bunge

    (@bungeshea)

    You can have Code Snippets use a different user role by adding a filter snippet:

    add_filter( 'code_snippets_cap', function () {
         return 'something';
    } );

    Of course, replace ‘something’ with a capability that your account has but your developer’s account does not.

    Im not weell versed in php.

    I just want to hide the ability for user role ‘web_dev’ to see the snippets menu item or edit snippets. Can you give me a complete code for that please?

    Plugin Author Shea Bunge

    (@bungeshea)

    ‘web_dev’ isn’t a standard WordPress role, so I don’t know what capabilities it has attached.

    If you like, you can create a new capability and only add it to administators:

    add_action( 'init', function () {
    	$role = get_role( 'administrator' );
    	$role->add_cap( 'manage_snippets', true );
    } );

    And then tell Code Snippets to use this new role:

    add_filter( 'code_snippets_cap', function () {
    	return 'manage_snippets';
    } );

    Okay I just implemented this and the developer can still access snippets plugin.

    I’m logged into an account with this user role by the way to test it.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Snippet to Hide Snippets (from Menu)’ is closed to new replies.