i want author role can't access wp-admin but still enable the capabilities so they can delete the posts from front end page ?
Use this code in your functions.php
file or in a plugin-
function wpse_253580_prevent_author_access(){
if( current_user_can( 'author' ) && is_admin() ) {
// do something here. maybe redirect to homepage
wp_safe_redirect( get_bloginfo( 'url' ) );
}
}
add_action( 'admin_init', 'wpse_253580_prevent_author_access' );
This will check if current user is an author and he is trying to access wp-admin area. If true, then redirect him to homepage.
The above answer by Mukto90 is a little outdated. A better approach would be:
- Check capabilities instead of role name (
current_user_can
does support a role name but it's not guaranteed to work and these roles can be renamed) - Only redirect if it's not doing AJAX
- exit after safe redirect
Updated answer follows:
add_action( 'admin_init', function(){
if( (!current_user_can( 'edit_others_posts' ) && current_user_can('edit_posts') && is_admin()) && !wp_doing_ajax() ) {
// do something here. maybe redirect to homepage
wp_safe_redirect( get_site_url() );
exit;
}
} );