Replace Default WordPress Login URL with a Custom URL

WordPress themes and plugins output default login URL wp-login.php in many parts of your website like contents, comments, widgets, etc. You might want to replace or customize the output of the default login URL, especially if you have a custom login URL for your WordPress site.

There’s a filter available in WordPress core called login_url to make this possible. You can change your website’s front-end or back-end login URL output easily by modifying the filter.

HEY! You must have the slug of your custom login page URL to apply this filter. If your custom login URL is “YourSite.com/login/”, then “login” is the page slug.

Customize Default WordPress Login URL

To customize or replace default WordPress login link output everywhere, use the code snippet:

// Replace WordPress login url with a custom link
add_filter( 'login_url', 'bydik_wp_login_link_replace', 10, 1 );
function bydik_wp_login_link_replace( $link ) {
    return home_url( '/login/?redirect_to=' ) . get_permalink();
}

You might need to change “/login/” in this code if your login page slug is different. After this, default login links of your site (both front-end & back-end) will be changed to the new one.

If you just want to customize the URL on the front-end of your site, use the code snippet:

// Replace WordPress front-end login links
add_filter( 'login_url', 'bydik_frontend_login_link_replace', 10, 2 );
function bydik_frontend_login_link_replace( $link ) {
    if ( ! is_admin() ) {
        return home_url( '/login/?redirect_to=' ) . get_permalink();
    }
    return $link;
}

HEY! Both functions will redirect users to the page they were in. If you want a homepage redirection, then change get_permalink() to get_home_url() in the code.

Adding code snippets for the first time? Don’t know where and how to add code snippets? We have an in-depth article on how to add code snippets into a WordPress site.

Enjoy this article? Please join in the comments and share this article with others.

Leave a Reply

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