1

I want to allow users to edit the posts without giving the admin access. I have a Basic Authentication plugin which stopping them to access it. But when i deactivate it they're able to edit the posts with contributor role access.

Now, My requirement is Basic Authentication plugin should be active and the users should able to edit the posts.

Can someone help me with this.

Thanks

4
  • Why not give them a custom role that allows them to edit posts and their user info, but nothing else?
    – WebElaine
    Dec 5, 2019 at 19:37
  • I have used capability and given the contributor and edit access to the role. but still i am having the issue
    – chinnam
    Dec 5, 2019 at 22:17
  • You might need to check with the Basic Authentication plugin developer. I meant, give them regular accounts and a custom role that restricts what they can access in the admin area, and then potentially get rid of the authentication plugin that's giving you trouble.
    – WebElaine
    Dec 5, 2019 at 23:00
  • We are using the basic authentication for rest API calling for Micro services if i disable it that service not work.
    – chinnam
    Dec 5, 2019 at 23:42

1 Answer 1

1

It would seem like the Basic Authentication Plugin is modifying the contributor user role, removing their edit_posts capability. As you need this plugin activated, I see a couple of options

Make a new user role with edit-post capabilities

Create a new role that has only the capabilities you need it to have. That way you avoid messing with the permissions of an existing role. Add this piece of code to your plugin main file (changing function names, capabilities and role data as you seem fit).

function add_new_special_rol(){
    $role = add_role( 'special_role_slug', 'Special Role Name', array(//Capabilities array
       'edit_posts'     => true, // True allows that capability
       'switch_themes'  => false, // False restricts that capability
    ));
}

function remove_special_rol(){
    $role = remove_role('special_role_slug');
}

register_activation_hook( __FILE__, 'add_new_special_rol' );
register_deactivation_hook( __FILE__, 'remove_special_rol' );

Keep in mind that the add_role and remove_role functions modify the database, so they should be used only when deactivating or activating a plugin/theme, thats why we hook the functions with register_activation_hook and register_deactivation_hook.

You can see all capabilities available here https://wordpress.org/support/article/roles-and-capabilities/#switch_themes

Edit the Contributor role capabilites

You can override whatever value the edit_posts capability has for the contributor role.

function modify_contributor_role_capabilities($wp_roles){
    //We get the contributor role object
    $contributor_role = $wp_roles->get_role('contributor');
    if($contributor_role)
        //then we make them be able to edit posts modifying their capabilities array
        $contributor_role->capabilities['edit_posts'] = true;
};
//Hook the function to the 'wp_roles_init' hook, that gives us access to the WP_Roles instance
//high priority just in case
add_action( 'wp_roles_init', 'modify_contributor_role_capabilities', 999 );

In this case we are be modifying the contributor role directly, no database modifications. This is posible through the wp_roles_init action hook, which can only be used in a plugin.

1
  • Thanks Raba for your inputs.
    – chinnam
    Dec 6, 2019 at 17:52

Your Answer

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

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