Replace Default WordPress Login URL by 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 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.

You must have the slug of your login URL to apply this filter. If your login URL is YourSite.com/login/, then “login” is the page slug. Let’s output the login link instead of default one.

Replace Default WordPress Login URL

To 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 change 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;
}

Where to add this code snippet? You can add the function in your own plugin, theme’s functions.php, or code snippet plugin. This will change the login link output of your WordPress site.

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

Leave a Reply

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