When working on a WordPress database you don’t want users to make changes to posts or settings which may be lost when making database migrations. Ideally, you will want to conduct work on your database when the site is at the least active stage, usually early mornings, however, this isn’t always possible. We often use this simple function to redirect users away from the WordPress admin while work is being conducted on a database, this prevents the entire site from having to be taken offline but also prevents updates being lost.

You can use this function in a Must Use Plugin to ensure it runs before any other plugin or theme:

function wph_block_wp_admin_init() {
	if (strpos(strtolower($_SERVER['REQUEST_URI']),'/wp-admin/') !== false) {
		if ( !current_user_can('manage_options') ) {
			wp_redirect( get_option('siteurl'), 302 );
			exit;
		}
	}
}
add_action('init','wph_block_wp_admin_init',0);

This function checks if the user is an Administrator and then redirects them to the homepage if not, you can adjust this to even block administrators by using is_user_logged_in() to redirect any logged in user (including yourself).

If you have any questions or problems with the function please leave a comment or get in touch with us.