WordPress.org

Support

Support » Plugins and Hacks » Add Capability to User Role in Theme Functions File?

Add Capability to User Role in Theme Functions File?

  • I’m looking for pointers on how to add a capability to a specific user role without using plugins.

    For example if I’d like Editors to be able to edit and add users, I thought something like:

    function give_user_edit() {
    	if(current_user_can('edit_others_posts')) {
    		global $wp_roles;
    		$wp_roles->add_cap('editor','edit_users' );
    		$wp_roles->add_cap('editor','create_users' );
    	}
    }
    add_action('admin_init', 'give_user_edit', 10, 0);

    Would work, but it seemed to add the capabilities for everyone.

    Any pointers greatly appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • This is a pointer – it gets the capabilities from say Administrator, then subtracts the ones it doesn’t want, then assigns the new capabilities to a new role.

    I’m still struggling myself 🙂

    // THIS WORKS  http://sillybean.net/wordpress/users-and-roles/creating-roles/
    $newrole = get_role('administrator'); /*choice of Administrator, Editor, Author, Contributor, Subscriber - http://codex.wordpress.org/Roles_and_Capabilities*/
    
    $caps = $newrole->capabilities;
    
    // Remove the stuff we don’t want in the new role.
    unset($caps['activate_plugins']);
    unset($caps['edit_plugins']);
    unset($caps['update_plugins']);
    unset($caps['delete_plugins']);
    unset($caps['install_plugins']);
    unset($caps['edit_users']);
    unset($caps['delete_users']);
    unset($caps['create_users']);
    unset($caps['import']);
    
    // Add the new role.
    add_role('Designer', 'Designer', $caps);

    I’ve gone the easy route – props to this perfect little plugin:
    http://wordpress.org/extend/plugins/user-role-editor/

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add Capability to User Role in Theme Functions File?’ is closed to new replies.
Skip to toolbar