How to Redirect WordPress Admin Pages to Any Location

Home » Snippets » How to Redirect WordPress Admin Pages to Any Location
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

As a website owner, have you ever wanted to redirect your WordPress admin pages to a different location? Maybe you’re looking to increase security by limiting access to your admin pages, or you simply want to create a more streamlined experience for yourself or your team. Whatever your reasons may be, this article will show you how to easily redirect your WordPress admin pages to any location of your choosing. With just a few simple steps, you’ll be able to customize your WordPress login experience like never before.

					function wpturbo_redirect_admin_pages() {
    global $pagenow;
    if(is_admin() && $pagenow != 'wp-login.php' && $pagenow != 'wp-register.php') {
        wp_redirect('https://example.com/'); //Redirects to example.com
        exit();
    }
}
add_action('admin_init', 'wpturbo_redirect_admin_pages');
				

In this article, we’ll go over how to redirect WordPress admin pages to any location in just a few simple steps. To follow along, make sure to have access to your WordPress site’s functions.php file.

Firstly, we create a new function called wpturbo_redirect_admin_pages(). This function is triggered by the admin_init action hook, which runs just after WordPress has finished loading the core files.

The next line of code uses the global keyword to set the $pagenow variable, which contains the name of the current admin page that is being visited.

global $pagenow;

We then use an if statement to check if we’re on an admin page and that the current page is not wp-login.php or wp-register.php. By checking that we’re not on the login or register pages, we ensure that visitors can still log in or register as normal.

if (is_admin() && $pagenow != 'wp-login.php' && $pagenow != 'wp-register.php') {

The next step is to perform the redirect. We use the wp_redirect() function to redirect to the desired location. In this code, we are redirecting to https://example.com/, but you should replace this with your own desired URL.

wp_redirect('https://example.com/'); // Redirects to example.com

Finally, we need to exit the WordPress page using the exit() function. This is necessary because, without it, WordPress would continue loading the page as normal instead of redirecting.

exit();

And there you have it! With these few lines of code, you can now easily redirect WordPress admin pages to any location you want. Just copy and paste the code into your functions.php file and replace the redirect URL to suit your needs.

Register an account to save your snippets or go Pro to get more features.