0

I've created a custom login page login.php inside the root. (e.g.: http://www.example.com/login.php)

/*
    LOGIN PAGE.
*/

// Login URL.
function custom_login_url($login_url, $redirect, $force_reauth ) {
    $login_url = home_url( 'login.php', 'login' );
    if ( ! empty( $redirect ) ) {
        $login_url = add_query_arg( 'redirect_to', urlencode( $redirect ), $login_url );
    }
    if ( $force_reauth ) {
        $login_url = add_query_arg( 'reauth', '1', $login_url );
    }
    return $login_url;
}

// Lostpassword URL.
function custom_lostpassword_url( $lostpassword_url, $redirect = '') {
    $args = array(
        'action' => 'lostpassword',
    );
    if ( ! empty( $redirect ) ) {
        $args['redirect_to'] = urlencode( $redirect );
    }
    if ( is_multisite() ) {
        $blog_details  = get_blog_details();
        $wp_login_path = $blog_details->path . 'login.php';
    } else {
        $wp_login_path = 'login.php';
    }
    $lostpassword_url = add_query_arg( $args, network_home_url( $wp_login_path, 'login' ) );
    return $lostpassword_url;
}
add_filter( 'lostpassword_url', 'custom_lostpassword_url', 20);

// Registration URL.
function custom_registration_url() {
    return home_url( 'login.php' . '?action=register', 'login' );
}

// Custom redirect admin locations.
function custom_redirect_admin_locations() {
    global $wp_rewrite;
    if ( ! ( is_404() && $wp_rewrite->using_permalinks() ) ) {
        return;
    }
    $admins = array(
        home_url( 'admin', 'relative' ),
    );
    if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $admins, true ) ) {
        wp_redirect( admin_url() );
        // wp_redirect( home_url( ADASTRA_ADMIN_PAGE) );
        exit;
    }
    $logins = array(
        home_url( 'login', 'relative' ),
    );
    if ( in_array( untrailingslashit( $_SERVER['REQUEST_URI'] ), $logins, true ) ) {
        wp_redirect( wp_login_url() );
        exit;
    }
}

// Custom Logout URL.
function custom_logout_url($logout_url, $redirect = '') {
    $args = array();
    if ( ! empty( $redirect ) ) {
        $args['redirect_to'] = urlencode( $redirect );
    }
    $logout_url = add_query_arg( $args, home_url( 'login.php' . '?action=logout', 'login' ) );
    $logout_url = wp_nonce_url( $logout_url, 'log-out' );
    return $logout_url ;
}

function custom_set_login_url() {
    add_filter( 'login_url', 'custom_login_url', 10, 3 );
    add_filter( 'logout_url', 'custom_logout_url', 15, 2 );
    add_filter( 'register_url', 'custom_registration_url', 30 );
    remove_action( 'template_redirect', 'wp_redirect_admin_locations', 1000 );
    add_action( 'template_redirect', 'custom_redirect_admin_locations', 1100 );
}
add_action('init', 'custom_set_login_url', 9999);

Now, all the URLs are working fine BUT everytime there's a new user registers or forgots password he receives an email with two links:

  1. the confirmation link leading to the "wrong" (aka default) login page:
http://www.example.com/wp-login.php?action=rp&key=rMrHUdWY1NC1eDdHAc4e&login=averagejoe
  1. A link to the correct login page:
http://www.example.com/login.php

How is it possible that the system doen't uses the custom URL I gave it, I need to have just the confrmation/activation link leading to the custom login page, like:

http://www.example.com/login.php?action=rp&key=rMrHUdWY1NC1eDdHAc4e&login=averagejoe

and not using a plugin.

Can someone help me?

Thanks.

1 Answer 1

0

It seems to use (src):

$message .= network_site_url( "wp-login.php?action=rp&key=$key&login=" . 
            rawurlencode( $user_login ), 'login' ) . '&wp_lang=' . $locale . "\r\n\r\n";

but not wp_lostpassword_url(), most likely because of the extra arguments, like key and wp_lang and a different action value.

You could look into the network_site_url filter during the lostpassword_post action, that is fired within the retrieve_password() function.

You might also consider creating a core ticket, to get a valuation if this should be modified in core.

Note that there are better ways than adding php files into the root directory of the WordPress install, like content filters, shortcodes and url rewrites.

In general all PHP modifications should be in a theme/plugin/mu-plugin.

1
  • Thank you for your answer, following your advice I'm creating a core ticket. Ah, by the way, my custom login page is outside of the root of WordPress install because I don't want to alter the WP structure. Jan 2, 2022 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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