PHP Developer

Monday, 28 March 2016

Add and Removes a Capability for a User Role in Wordpress

Add and Removes a Capability for a User Role in Wordpress

Add this code in function.php to Add and Removes a Capability from a User Role

<?php
/**
 * Remove capabilities from editors.
 *
 * Call the function when your plugin/theme is activated.
 */

function wpcodex_set_capabilities() {

    // Get the role object.
    $editor = get_role( 'editor' ); // Get alredy registerd Role "Editor" in wordpress

    // A list of capabilities to remove from editors.
    $caps_remove = array(
        'moderate_comments',
        'manage_categories',  // Allows user to manage post categories
        'manage_links',  // Allows user to edit links
        'edit_others_posts',  // Allows user to edit others posts not just their own
        'edit_others_pages',  // Allows user to edit pages
        'delete_posts',  //  Allows user to delete posts
        'edit_themes', // false denies this capability. User can’t edit your theme
        'install_plugins', // User cant add new plugins
        'update_plugin', // User can’t update any plugins
        'update_core', // user cant perform core updates
    );
   
    // A list of capabilities to Add for editors.
    $caps_add = array(
        'read', // true allows this capability
        'edit_posts', // Allows user to edit their own posts
        'edit_pages', // Allows user to edit pages
        'edit_others_posts', // Allows user to edit others posts not just their own
        'create_posts', // Allows user to create new posts
        'manage_categories', // Allows user to manage post categories
        'publish_posts', // Allows the user to publish, otherwise posts stays in draft mode
    );

    foreach ( $caps_remove as $cap ) {
        // Remove the capability.
        $editor->remove_cap( $cap );
    }
   
    foreach ( $caps_add as $capd ) {
        // Add the capability.
        $editor->add_cap( $capd );
    }
}
add_action( 'init', 'wpcodex_set_capabilities' );

?>

No comments:

Post a Comment