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
- How to Enable/Disable Automatic Update for All WordPress Plugins
- How to Display Product Description on WooCommerce Shop Page
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.