Wordpress: Block Access To WP Admin

You can block access to the Wordpress admin area using the code snippet below.

I have found this to be handy if you need to lock a client out of the admin area temporarily to prevent database conflicts between stages.

Pop the following into your functions.php or a plugin.

add_action('admin_init', 'no_mo_dashboard');
function no_mo_dashboard() {

    if ($_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') { 
        wp_redirect("http://google.co.uk"); 
        exit;
    }
}

Or you could use this terminal command to do the same job. It will add the above code to the very top of your functions.php file. I have this set up as a Capistrano task so i can issue a command to lock user out the prod CMS lickety split.

echo "<?php add_action('admin_init', 'no_mo_dashboard'); function no_mo_dashboard() {if ($_SERVER['DOING_AJAX'] != '/wp-admin/admin-ajax.php') {wp_redirect("http://google.co.uk/"); exit;}}?>" >> "functions.php"   

Edd Smith

Read more posts by this author.

Subscribe to Edd Smith | Technical Direction & Web Development

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!