Restrict Dashboard to Administrators Only

Restrict Dashboard to Administrators Only

? The Snippet:

function restrict_admin_access() {
	if ( is_admin() && ! current_user_can( 'administrator' ) && ! wp_doing_ajax() ) {
		wp_redirect( home_url() );
		exit;
	}
}
add_action( 'init', 'restrict_admin_access' );

ℹ️ About the Snippet

By default in WordPress, users have access to the dashboard and their profiles. But, there may be some cases when you want to restrict dashboard to administrators only. In case, if you have an eCommerce store or a membership website. Therefore, changing the user’s role can limit access to the WordPress backend. But, for many users, it’ll be tedious work to do.

✅ How to use the Snippet

To disable the dashboard access for all the users except admins. You can either use a plugin or a small snippet of code. Using the below-mentioned snippet, you only have to add the code snippet to the theme’s functions.php file.

function restrict_admin_access() {
	if ( is_admin() && ! current_user_can( 'administrator' ) && ! wp_doing_ajax() ) {
		wp_redirect( home_url() );
		exit;
	}
}
add_action( 'init', 'restrict_admin_access' );

Sharing is caring!

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Get regular WordPress updates directly in your inbox.

shares