Do custom user roles have any default capabilities?

I’ve found that the WordPress has_cap() function, which is relied on by functions like user_can() and current_user_can, explicitly returns false for empty capabilities. Example: If a capability is passed as an argument using current_user_can(), this function will pass the capability to has_cap() and return the results: return call_user_func_array( array( $current_user, ‘has_cap’ ), $args has_cap() will … Read more

How to let the Contributor role preview unpublished posts?

Unfortunately, WordPress doesn’t use a separate capability for previewing posts. A user needs the ability to edit a post in order to preview it. As seen in the WP_Query::get_posts() method: // User must have edit permissions on the draft to preview. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) { $this->posts = array(); } What you could … Read more

How can I change a user role upon visiting a page?

EDIT I updated the function to check for role for which we want to make changes for, if the current user role is not within our roles_to_change array then the function exits. Note that I haven’t tested for bugs, so if you find any, just report them here. @Malisa’s answer is a good approach. However, … Read more

How to filter the role selector?

The UI select element On user-edit.php, you see the drop-down in the UI. The drop down <select> wrapper is hard coded. Then the admin interface does a nifty thing 1) according to the inline comment: // Get the highest/primary role for this user. In fact it is getting the first role, that was assigned to … Read more

Temporarily disable user role login and replace with message

With a some digging and learning, I managed to combine various help and create these 2 functions… // MAINTAINANCE MODE function site_maintenance() { if ( current_user_can(‘media’) || current_user_can(‘genpo’) ) { $logout_url = wp_login_url().’?mode=maintainance’; wp_logout(); wp_redirect( $logout_url, 302 ); } } add_action(‘get_header’, ‘site_maintenance’); // CUSTOM LOGIN MESSAGES function my_login_message() { if( $_GET[‘mode’] == ‘maintainance’ ){ $message=”<p … Read more

Allow editors access to added plugins

Please add the following code. function activate_plugin_name() { $role = get_role( ‘editor’ ); $role->add_cap( ‘manage_options’ ); // capability } // Register our activation hook register_activation_hook( __FILE__, ‘activate_plugin_name’ ); function deactivate_plugin_name() { $role = get_role( ‘editor’ ); $role->remove_cap( ‘manage_options’ ); // capability } // Register our de-activation hook register_deactivation_hook( __FILE__, ‘deactivate_plugin_name’ );` Refer my tutorial for … Read more

How to update role capabilities

add_role() will not do anything if the role already exists, so it can’t be used to modify capabilities. To modify capabilities use the add_cap() and remove_cap() method of the WP_Role object. You can get a WP_Role for your role using get_role(): $role = get_role( ‘event-planner’ ); $role->add_cap( ‘edit_others_events’ ); Here’s the thing though, roles are … Read more