Redirect Your WordPress Site to Another Domain, but Still Access WP Admin

Redirect Entire Site Frontend Only Except For Login Page And Logged In Users Snipsnip.pro .code Snippets

If you need to temporarily (or long-term, I suppose) redirect your entire WordPress site to another domain or URL, but you still want to access the backend WP Admin, then this takes care of everything. Unless you’re logged-in (or on the login page), this forces all frontend pages to redirect to a new URL.

Note #1: Of course you’ll need to edit the snip by changing it to the NEW_URL you want.

Note #2: Sometimes, right after saving this, you might get redirected. If so, just go back to WordPress site admin URL (usually: site.com/wp-admin/). If you still cannot get there, try adding Snippet Safe Mode at the end of your URL, like this: ?snippets-safe-mode=true

Read more about Snippet Safe Mode >

What it does…

Sends all visitors to a new webpage.

Why it does it…

If you need to hide your site for a short time, but you still want to access the backend WP-Admin to keep working on it.

How it does it…

Checks if the user is logged in and checks if the visitor is on the login page, if neither of those are true, it redirects the user to the NEW_URL.

Wanna know more? Okay…

This code is a PHP script that redirects the entire site, except for the login page and logged-in users, to a new URL. It is implemented as a class named qszZ3EkE, with several methods.

The const NEW_URL is a class constant that stores the URL of the new site where visitors will be redirected.

The constructor method __construct() initializes the class by calling the init() method.

The init() method checks if the user is not already logged in and if the current page is not the login page. If either of these conditions is true, the method calls the run_the_redirect() method.

The run_the_redirect() method sets the $url variable to the value of NEW_URL and then uses the WordPress function \wp_redirect() to redirect the user to the new URL. The exit function is then called to terminate the script.

The user_is_already_logged_in() method checks if the user is not already logged in using the WordPress function \is_user_logged_in(). If the function does not exist, the method returns false.

The this_is_the_login_page() method checks if the current page is the login page. It does this by checking if the file path for the current script includes either wp-login.php or wp-register.php, or if the current page is wp-login.php. If any of these conditions is true, the method returns true.

Finally, a new instance of the qszZ3EkE class is created with the new qszZ3EkE() statement at the end of the script, which initiates the class and starts the redirection process.

Use the JSON file for easily importing into your CodeSnippetsPro plugin. Use the PHP file or just copy/paste the code sample into your functions.php if you’re not using CodeSnippetsPro.

Related Snips

Leave a Reply

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