Keep Subscribers Out of wp-admin
Redirects users who cannot edit posts (subscribers, customers) to the homepage when they try to open the admin area. AJAX and admin-post requests keep working.
Opens WPIDE on
Code
add_action( 'admin_init', function () {
global $pagenow;
if ( wp_doing_ajax() || 'admin-post.php' === $pagenow || current_user_can( 'edit_posts' ) ) {
return;
}
wp_safe_redirect( home_url( '/' ) );
exit;
} );
Notes
Users can still change their password through the front-end account page if your site has one.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.