Keep subscribers and customers out of the admin area
Users who can’t write posts are sent to the home page when they open the admin area. Their profile page and background requests still work.
<?php
add_action(
'admin_init',
static function () {
if ( wp_doing_ajax() || current_user_can( 'edit_posts' ) || ! is_user_logged_in() ) {
return;
}
global $pagenow;
// Leave the profile screen and admin-post.php (form handlers) alone.
if ( in_array( $pagenow, array( 'profile.php', 'admin-post.php' ), true ) ) {
return;
}
wp_safe_redirect( home_url( '/' ) );
exit;
}
);
Good to know
WooCommerce does the same for customers already; this covers every other role without editing rights.
In these packs
- More admin tweaks: Useful columns, a tidier admin bar, a welcome box for clients, and fewer menus for editors.
- Users and profiles: Registration dates, last logins, social profile fields, longer sessions and stricter passwords.