0

I'm trying to change the login url from wp-login.php to simply 'login'. I'd been looking through another plugins source to find ways of doing it.

Some of the applicable parts that I've got so far:

register_activation_hook( __FILE__, 'my_plugin_activate' );
register_deactivation_hook( __FILE__, 'my_plugin_deactivate' );

add_action( 'init', 'my_plugin_rewrite' );
add_action( 'login_init', 'my_plugin_login_init' );
add_action( 'template_redirect', 'my_plugin_display' );

function my_plugin_activate() {
    add_rewrite_tag( '%login%', '([^/]+)');
    add_permastruct( 'login', '/%login%' );
    flush_rewrite_rules();
}

function my_plugin_login_init() {
    if ( '/wp-login.php' == $_SERVER['PHP_SELF'] ) {
        header( 'HTTP/1.0 404 Not Found' );
        global $wp_query;
        $wp_query->set_404();
        require TEMPLATEPATH . '/404.php';
        exit;
    }
}

function my_plugin_display() {
    global $wp_query;
    switch( $wp_query->query_vars['pagename'] ) {
        case 'login' :
            include( ABSPATH . '/wp-login.php' );
            exit;
            break;
    }
}

The problem I'm having is in the "my_plugin_display" function.

$wp_query->query_vars['pagename'] happens to be empty. How can I test if the url is at /login? Is this the right hook to use?

  • Check out Bainternet's answer to this question, it has everything you need and more. wordpress.stackexchange.com/a/78258 – Alex Salomon Mar 30 '18 at 12:07
  • Where is my_plugin_rewrite function? The rewrite tag and permastruct you add on activation will disappear as soon as some other code flushes rewrite rules, or you visit the permalinks settings page. – Milo Mar 30 '18 at 14:54
0

You can try adding a query var using the 'query_vars' filter like so:

add_filter('query_vars', 'my_plugin_query_vars');
function my_plugin_query_vars($vars) {
    $vars[] = 'login';
    return $vars;
}

That may populate $wp_query->query_vars, if not I have successfully used the get_query_var function in one of my projects, try something like:

function my_plugin_display() {
    if(get_query_var('login')) {
        include( ABSPATH . '/wp-login.php' );
        exit;
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.