How to Restrict Admin Access to WordPress Dashboard

How to Restrict Admin Access to WordPress Dashboard 1

if you looking to restrict WordPress dashboard access by user capability to prevent registered users without admin capability to access WordPress admin so if you need to it in just one minute by this code snippet where let you filter by WordPress built-in hooks admin_init

Steps to restrict admin access to WordPress dashboard:

  1. Open WordPress admin panel then go to Appearance > Theme Editor
  2. Open theme functions.php file, if you work localhost open the same file using any code editor IDE
  3. Add the following code at the bottom of the file.
  4. Save the changes.
function zytheme_restrict_admin() {
    if ( ! current_user_can( 'manage_options' )  && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX )  ) {
        wp_redirect( home_url() );// you can change it if you need to redirect to another page like register.
add_action( 'admin_init', 'zytheme_restrict_admin', 1 );

Did you find the WordPress snippets in this article helpful? Share it with your friends and if you have any questions and want to ask us about it, please don’t hesitate to add comments.