How to restrict dashboard access to Admins only in WordPress

How-to-restrict-dashboard-access-to-Admins-only
264 Views

To restrict access to the WordPress dashboard to only administrators, you can use a code snippet in your website’s functions.php file. Here’s how:

<?php
add_action( 'init', 'cxc_restrict_admin_access_call_back' );
function cxc_restrict_admin_access_call_back() {
    if ( is_admin() && ! current_user_can( 'administrator' ) && ! wp_doing_ajax() ) {
        wp_redirect( home_url() );
        exit;
    }
}
?>

This code snippet checks if the current user is not an administrator and if the request is not an AJAX request. If both conditions are true, it redirects the user to the home page of the website and stops the script execution using the exit function.

Now, only users with the administrator role will be able to access the WordPress dashboard.

Read more WordPress tutorial

Popular FAQs

How can I restrict access to the WordPress dashboard to only administrators?

You can restrict access to the WordPress dashboard to only administrators by adding a code snippet in your website’s functions.php file.

Where is the functions.php file located in WordPress?

The functions.php file is located in your WordPress theme’s directory. You can access it by connecting to your website via FTP or by using the file manager in your web hosting control panel.

Was this article helpful?
YesNo

Leave a comment

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