0

I know how to add custom user roles, but how can i make it so that when a user with, lets say a role with 'DJ', they come to the dashboard AND ONLY GET TO SEE:

  • Users > Your Profile
  • custom menu DJ Housekeeping Panel

and not see any other menu items.

3
  • I have got a solution and will post it tomorrow since I have less than 10 reputation Basically, when you create a new role user_dj, you add capabilities to that role dj_user_view, and when you add_menu_page and add_submeu_page <?php add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position ); ?, you put the capability there dj_user_view
    – John Zon
    Apr 30, 2014 at 12:10
  • Show your code how you are adding a role, and we can help improve that code to achieve what you want. Apr 30, 2014 at 13:50
  • Each menu item was checked via capability. But you can not change the role for each menu item. You can create custom roles, changes default roles with the plugin Members. Also you can hide areas, menu-items and much more, also custom css elements with the plugin Adminimize.
    – bueltge
    Apr 30, 2014 at 14:39

1 Answer 1

0

Okay, so I have done it.

Reading about roles and capabilities: http://codex.wordpress.org/Roles_and_Capabilities

A default set of capabilities is pre-assigned to each role, but other capabilites can be assigned or removed using the add_cap() and remove_cap() functions. New roles can be introduced or removed using the add_role() and remove_role() functions.

Add a capability: http://codex.wordpress.org/Function_Reference/add_cap

If you want to add a new role with capabilities, just add them when you add the role using add_role();.

Reading about adding a menu: http://codex.wordpress.org/Function_Reference/add_menu_page

$capability (string) (required) The capability required for this menu to be displayed to the user.

Step 1: Register an activation hook to create a new user role

register_activation_hook( __FILE__, 'plugin_newuserrole' );

function plugin_newuserrole() { 
global $wp_roles;

$result = add_role(
    'user_dj',
    __( 'DJ' ),
    array(
        'read'         => true,  // true allows this capability

    )
);
/* if ( null !== $result ) {
    echo 'Yay! New role created!';
}
else {
    echo 'Oh... the user_dj role already exists.';
} */
}

Step 2: Add a capability to the role

function plugin_caps() {
// gets the new role made earlier
$role = get_role( 'user_dj' );

// This only works, because it accesses the class instance.
// would allow the author to edit others' posts for current theme only
$role->add_cap( 'dj_hk_user' ); 
}
add_action( 'admin_init', 'plugin_caps');

Step 3: Change capability options in menu and submenu

function add_your_menu() {
add_menu_page( 'Title', 'Menu Label', 'dj_hk_user', 'plugin-slug.php', '', 'dashicons-microphone');
add_submenu_page( 'plugin-slug.php', 'Overview', 'Overview', 'dj_hk_user', 'plugin-slug.php', 'function_of_overview' );
add_submenu_page( 'plugin-slug.php', 'Sample Edit Title', 'Sample Edit Label', 'dj_hk_user', 'sample_slug', 'function_of_page1' );    
}
add_action('admin_menu', 'add_your_menu');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.