How to restrict access to WP-ADMIN while working on Database

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:

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.