Replace Default WordPress Login URL by a Custom URL

WordPress themes and plugins can call default login URL in many parts of your website like contents, comments, widgets etc. If your WordPress site has a custom login URL other than the default wp-login.php, you might want to replace the default login URL that WordPress calls different parts of your website with the custom one.

There’s a filter available in WordPress core called login_url to achieve this. By modifying the filter, you can change your website’s front-end or back-end login URL output easily. Let’s know, how to modify the login_url filter and customize the login URL output for your WordPress website.

Replace Default WordPress Login URL

Suppose, slug of your website’s login page is “login” (example.com/login) and you want to output this in your website’s front-end. To change the login URL output in front-end, use the code snippet –

// Replace front-end login url with a custom page
add_filter( 'login_url', 'bydik_frontend_login_link_replace', 10, 1 );
function bydik_frontend_login_link_replace( $link ) {
    if (!is_admin()){
        return home_url('/login/?redirect_to=') . get_permalink();
    }
    return $link;
}

And if you want to change this for your website’s back-end only, use the code snippet –

// Replace back-end login url with a custom page
add_filter( 'login_url', 'bydik_backend_login_link_replace', 10, 2 );
function bydik_backend_login_link_replace( $link ) {
    if (is_admin()){
        return home_url('/login/?redirect_to=') . get_permalink();
    }
    return $link;
}

You can add the function in a site specific plugin or Functionality plugin. After this, you’ll no longer see default login URL output within your website. And that’s it, let’s discuss more through comments.

Leave a Reply

Email address will not be published. Remember to keep comments follow our guidelines.