Add Custom URL Redirects to Your WordPress Dashboard Areas or Login Page

Ever wanted to use something like example.com/backend or example.com/dash to access your WordPress dashboard or login area? Here is a simple snippet of PHP for your functionality plugin or functions.php to do just that — it uses standard WordPress URL rewrite API.

Update: turns out that in WordPress 3.4 there are new default redirects (/admin, /dashboard and /login) implemented in the core (see this ticket). So here is a better way to add your own redirects which will work only if you don’t have a page or post with the same name (slug) already:

Updated Version:

add_action('template_redirect', 'add_my_custom_redirects');

function add_my_custom_redirects() {
	if (!is_404())
		return;

	$current_uri = untrailingslashit($_SERVER['REQUEST_URI']);
	$my_admin_uris = array(
		home_url('dash', 'relative'),
		home_url('your-custom-uri', 'relative')
	);

	if (in_array($current_uri, $my_admin_uris)) {
 		wp_redirect(admin_url());
 		exit;
	}
}

The Obsolete Version:

add_filter('generate_rewrite_rules', 'add_my_custom_rewrites');

function add_my_custom_rewrites($wp_rewrite) {
    $my_rewrites = array(
	'dash' => 'wp-admin'
    );

    $wp_rewrite->rules = $my_rewrites + $wp_rewrite->rules;
}

Never lose a single business lead or enquiry again! Get my latest plugin Storage for Contact Form 7 now!

Buy now for only $10 →

13 Comments

  1. Juani says:

    Can you add ‘login’ => ‘wp-login’ in array?

  2. Now that is a sweet little snippet … mind if I add it to my BNS Theme Add-Ins plugin … attributed to you at this post?

  3. Istivan says:

    Would this leave the standard “example.com/login” and “example.com/wp-admin” active? Is there a way to disable those (for more security)?

    • Kaspars says:

      Istivan, thanks for mentioning the default “login” one — I didn’t know about it!

      The answer to you second question is yes — this filter leaves all of the default rewrites intact. Secondly, you can’t remove wp-admin, because it is an actual folder that is part of the WordPress core file structure.

  4. Serg says:

    This is great Kaspars! I have a question: Do you know how to change the registration module so new subscribers pay a small fee to be able to subscribe to my site without interfering with my e-commerce plugin?
    What I’m saying is something you can change/add in the wordpress framework without touching the current e-commerce-paypal standard already installed in the system.
    This is because as soon as I enable “anyone can register” on my settings, I get a lot of new subscribers with fake emails.
    I’d rather not install another plugin to do this, I prefer just add a little code snippet, if it’s possible.
    Thanks for your advice!

    • Kaspars says:

      Serg, I really don’t know as it depends on the plugin you’re using and the filters and action hooks it provides for you to customize the functionality of the plugin.

  5. Well, I’ve tried everything and quite simply this is not working for me … the code makes sense and I’m not seeing any errors but I cannot seem to get it to work locally or live.

    It will have to wait …

  6. w.holmes says:

    Sounds cool but it’s not doing anything in my setup similar to @Edward Caissie. Thanks for any ideas.

    WP 3.3
    Multisite

  7. Kaspars says:

    w.holmes, Edward Caissie, I have update the post with a new version of the snippet and a reference to the recent changes in the WordPress core (as per ticket 19880).

  8. w.holmes says:

    Thank you sir! Gonna try it right now…

  9. I could also not get it to work first, but after changing:

    $current_uri = untrailingslashit($_SERVER['REQUEST_URI']);

    to

    $current_uri = home_url( untrailingslashit($_SERVER['REQUEST_URI']) );

    Then it work great.

Leave a Reply