• Resolved Michael

    (@mikeyhoward1977)


    Hi,
    I have created a plugin which has custom menu pages.
    The plugin creates two roles upon activation (let’s say role 1 and role 2).

    role 1 is used for some coding and works fine, WordPress users are assigned this role and the code works correctly.

    Where I am struggling is that I would the plugin menu’s to only be displayed to Administrators and users who have role 2 assigned to them.

    I am running the following from a function called by my activation hook…

    add_role( 'role 1', 'Role 1', array( 'read' => true, 'level_0' => true ) );
    		add_role( 'role 2', 'role 2', array( 'read' => true, 'level_0' => true ) );
    		add_action( 'admin_init', 'f_mdjm_caps');

    the function f_mdjm_caps contains the following

    function f_mdjm_caps()	{ /* Capabilities */
    		$role = get_role( 'role 2' );
    		$role->add_cap( 'manage_myplugin' );
    	}

    I tried adjusting my menu code to be

    add_menu_page( 'My Menu', 'My Menu', 'manage_myplugin', 'mdjm-dashboard', 'f_mdjm_admin_dashboard' );

    but the menu disappears.

    Any help appreciated

    Thanks!

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You need to add the ‘manage_myplugin’ role to the admin role and any other roles that should have access besides ‘role 2’. Admins do not automatically have all capabilities. Someone could remove all capabilities from admin on their site and certain things would be inaccessible to anyone unless the capabilities are reassigned.

    It’s also possible to add capabilities to individual users. You could give yourself the capability, then only you and ‘role 2’ have access, any other admins would still not have access.

Viewing 1 replies (of 1 total)
  • The topic ‘Custom Capability – show plugin menu to these users and admins only’ is closed to new replies.