Block access to WP admin for non Admin’s

So you have a WordPress site you want to access  the site admin but allow your  site users  to have  access to your admin area  if you add the bellow code snippet to your functions.php file or or somewhere similar. In most cases its not that there are any security issues its just that you want to control how users  sign in

1 add_action( 'init', 'blockusers_init' );
3 function blockusers_init() {
4 if ( is_admin() && ! current_user_can( 'administrator' ) &&
5 ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
6 wp_redirect( home_url() );
7 exit;
8 }
9 }

Ta-da! Now, only administrator users can access wp-admin, everyone else will be re-directed to the homepage.

No comments yet.

Leave a Reply