Add Capability of Custom Plugin to a Role


To add new Capability of a custom made plugin in WordPress to a specific Role.

function add_theme_caps() {
    // gets the author role
    $role = get_role('editor');

    // This only works, because it accesses the class instance.
    // would allow the author to edit others' posts for current theme only
    // just mention your plugin filename like I have edit_events.php as my plugin
    $role->add_cap('edit_events'); 
}
add_action( 'admin_init', 'add_theme_caps');

To add new Capability of a custom made plugin in WordPress to a specific user.

function add_theme_caps() {
    // get the user
    $user = new WP_User( $user_id );

    // This only works, because it accesses the class instance.
    // would allow the author to edit others' posts for current theme only
    // just mention your plugin filename like I have edit_events.php as my plugin
    $user->add_cap('edit_events'); 
}
add_action( 'admin_init', 'add_theme_caps');
Advertisements