I recently had to overcome what seems like a simple problem but ended up taking way too much time to do so, and this post is my try to help anyone else with the same issue.
So i just had a client ask me to help him set up a nicer and easier to remember login URL to his WordPress site and I know that the simplest way to do that is to add a rewrite rule to the htaccess file like this:
RewriteEngine On RewriteRule ^login$ en.bainternet.info/wp-login.php [NC,L]
But then i found out that the host that he is using doesn't allow htaccess file editing and only they can edit the file which was how he had Pretty permalinks in the first place.
So in order to achieve the task in hand (the nice easy login URL) I create a simple plugin which uses the WordPress' way of creating rewrite rules and with one little function:
// Create new rewrite rule add_action( 'init', 'NLURL_rewrite' ); function NLURL_rewrite() { add_rewrite_rule( 'login/?$', 'wp-login.php', 'top' ); add_rewrite_rule( 'register/?$', 'wp-login.php?action=register', 'top' ); add_rewrite_rule( 'forgot/?$', 'wp-login.php?action=lostpassword', 'top' ); }
and then two more function to flush the rewrite rules on plugin activation and deactivation:
// Add rewrite rule and flush on plugin activation register_activation_hook( __FILE__, 'NLURL_activate' ); function NLURL_activate() { NLURL_rewrite(); flush_rewrite_rules(); } // Flush on plugin deactivation register_deactivation_hook( __FILE__, 'NLURL_deactivate' ); function NLURL_deactivate() { flush_rewrite_rules(); }
And all was done, the new plugin made WordPress understand login, register and forgot URL's in the form of:
/* * login url * example.com/login * register url * example.com/register * forgot password url * example.com/forgot */
But then I needed WordPress native functions to return these New location instead of the old ones so i looked up the code and found a few filters that i can hook my functions with like so:
//register url fix add_filter('register','fix_register_url'); function fix_register_url($link){ return str_replace(site_url('wp-login.php?action=register', 'login'),site_url('register', 'login'),$link); } //login url fix add_filter('login_url','fix_login_url'); function fix_login_url($link){ return str_replace(site_url('wp-login.php', 'login'),site_url('login', 'login'),$link); } //forgot password url fix add_filter('lostpassword_url','fix_lostpass_url'); function fix_lostpass_url($link){ return str_replace('?action=lostpassword','',str_replace(network_site_url('wp-login.php', 'login'),site_url('forgot', 'login'),$link)); }
And it all works nice accept the register URL in the default login form (the one in the image bellow)
And to fix that i looked up the code , opend a ticket in the WordPress Trac which as later closed as a duplicate and looked up some more code in the core until i found this:
//Site URL hack to overwrite register url add_filter('site_url','fix_urls',10,3); function fix_urls($url, $path, $orig_scheme){ if ($orig_scheme !== 'login') return $url; if ($path == 'wp-login.php?action=register') return site_url('register', 'login'); return $url; }
Now the whole code as a plugin:
<?php /* Plugin Name: Nice Login URL Plugin URI: en.bainternet.info Description: Simple plugin to redirect login/register to a nice url Version: 1.0 Author: bainternet Author URI: en.bainternet.info */ // Add rewrite rule and flush on plugin activation register_activation_hook( __FILE__, 'NLURL_activate' ); function NLURL_activate() { if (! get_option('permalink_structure') ){ add_action('admin_notices', 'permalink_structure_admin_notice'); } NLURL_rewrite(); flush_rewrite_rules(); } // Flush on plugin deactivation register_deactivation_hook( __FILE__, 'NLURL_deactivate' ); function NLURL_deactivate() { flush_rewrite_rules(); } // Create new rewrite rule add_action( 'init', 'NLURL_rewrite' ); function NLURL_rewrite() { add_rewrite_rule( 'login/?$', 'wp-login.php', 'top' ); add_rewrite_rule( 'register/?$', 'wp-login.php?action=register', 'top' ); add_rewrite_rule( 'forgot/?$', 'wp-login.php?action=lostpassword', 'top' ); } //register url fix add_filter('register','fix_register_url'); function fix_register_url($link){ return str_replace(site_url('wp-login.php?action=register', 'login'),site_url('register', 'login'),$link); } //login url fix add_filter('login_url','fix_login_url'); function fix_login_url($link){ return str_replace(site_url('wp-login.php', 'login'),site_url('login', 'login'),$link); } //forgot password url fix add_filter('lostpassword_url','fix_lostpass_url'); function fix_lostpass_url($link){ return str_replace('?action=lostpassword','',str_replace(network_site_url('wp-login.php', 'login'),site_url('forgot', 'login'),$link)); } //Site URL hack to overwrite register url add_filter('site_url','fix_urls',10,3); function fix_urls($url, $path, $orig_scheme){ if ($orig_scheme !== 'login') return $url; if ($path == 'wp-login.php?action=register') return site_url('register', 'login'); return $url; } //notice if user needs to enable permalinks function permalink_structure_admin_notice(){ echo '<div id="message" class="error"><p>Please Make sure to enable <a href="options-permalink.php">Permalinks</a>.</p></div>'; }
and that is it, I only hope this will save someone the few hours that it took me to get this simple (as it should be) task done.