How to hide WordPress admin for your subscribers?

Sharing is caring!

These functions will help you for hiding  WordPress admin for your subscribers. We’ve used a capability of “edit_posts” which is just outside of the roles of a Subscriber user. If the user is logged in, but can’t edit posts, then hide the admin bar on the frontend of the website. If the user is logged in, but can’t edit posts, do not allow them to access the WordPress admin panel.
This code could go in functions.php of your theme or in a plugin you create.


/**
* Disable admin bar in frontend for subscribers.
*/
function themeblvd_disable_admin_bar() {
if( ! current_user_can('edit_posts') )
add_filter('show_admin_bar', '__return_false');
}
add_action( 'after_setup_theme', 'themeblvd_disable_admin_bar' );

/**
* Redirect to homepage and not allow access to
* WP admin for Subscribers.
*/
function themeblvd_redirect_admin(){
if ( ! current_user_can( ‘edit_posts’ ) ){
wp_redirect( site_url() );
exit;
}
}
add_action( ‘admin_init’, ‘themeblvd_redirect_admin’ );

Leave a Reply

Your email address will not be published. Required fields are marked *

Got Project on mind? Let's ConnectContact Us

Secured By miniOrange