WordPress.org

Ready to get started?Download WordPress

Forums

Add Capability to User Role in Theme Functions File? (3 posts)

  1. Jackson
    Member
    Posted 4 years ago #

    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.

  2. richarduk
    Member
    Posted 4 years ago #

    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);
  3. Jackson
    Member
    Posted 4 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic