How to allow newly created role to login into wordpress admin?

I have created new role in wordpress, I want that user with this role can login into wordpress admin.

            $result = add_role( 'sub_admin', __(

            'Sub Admin' ),

            array(
                'read' => true, // true allows this capability
                'edit_posts' => true, // Allows user to edit their own posts
                'create_posts' => true, // Allows user to create new posts
                'publish_posts' => true, // Allows the user to publish, otherwise posts stays in draft mode
                'install_plugins' => false, // User cant add new plugins
                'update_plugin' => false, // User can’t update any plugins
                'update_core' => false // user cant perform core updates
            )

        );

This is the code i added to add new role..Can any one suggest me the solution?

Related posts

4 comments

  1. read allows access to administration panel. You already have it. You can find more about capabilities and which roles they’re assigned to by default here

  2. While creating new role, make sure that appropriate capabilities are assigned to that role. For example, following code will add three permissions

    1. "read"
      Allows access to Administration Panel options:
      i.e. Dashboard and Users > Your Profile

    2. "edit_posts"
      Allows to edit posts

    3. "delete_posts"
      Allows to delete posts

    So user added to this role will have all these capabilities inherited from this role.

    $result = add_role(
        'basic_contributor',
        __( 'Basic Contributor' ),
        array(
            'read'         => true,  // true allows this capability
            'edit_posts'   => true,
            'delete_posts' => false, // Use false to explicitly deny
        )
    );
    if ( null !== $result ) {
        echo 'Yay! New role created!';
    }
    else {
        echo 'Oh... the basic_contributor role already exists.';
    }
    
  3. You can try this plugin https://wordpress.org/plugins/capability-manager-enhanced/screenshots/

    It will provide you all the functionalities like

    • Create a new role
    • Update user role’s capabilities

    Steps to do this :

    • Install and activate plugin
    • There will be new menu capabilities under users menu
    • In there, there will be option to create role in right sidebar
    • There will be one option Select Role to View / Edit to modify capability of specific role
    • And now you have to assign that role to desired user

    Moreover, You can view the capabilities of admin and author user role to understand which capabilities will be required to allow user to access specific area.

    That’s all

    WordPress is all about plugins. So, it would be easy if you find any plugin for your requirements. It will save your all the time and it will do all the coding for you.

  4. $result = add_role('sub_admin', __('Sub Admin' ),array());
    
    // gets the sub admin role
    $sub_admin = get_role( 'sub_admin' );
    
    //lets assign contributor other roles
    $sub_admin->add_cap( 'read' );
    
    $sub_admin->add_cap( 'edit_posts' );
    $sub_admin->add_cap( 'create_posts' );
    $sub_admin->add_cap( 'publish_posts' );
    

    This worked , Nicely … It solved my problem.

Comments are closed.