Using WordPress ‘login_url’ PHP filter

The login_url WordPress PHP filter allows you to modify the login URL before it’s returned by the wp_login_url() function.

Usage

add_filter('login_url', 'your_function_name', 10, 3);

function your_function_name($login_url, $redirect, $force_reauth) {
    // your custom code here
    return $login_url;
}

Parameters

  • $login_url (string) – The login URL, not HTML-encoded.
  • $redirect (string) – The path to redirect to on login, if supplied.
  • $force_reauth (bool) – Whether to force reauthorization, even if a cookie is present.

More information

See WordPress Developer Resources: login_url

Examples

Add a query parameter to the login URL

This example adds a custom query parameter to the login URL:

add_filter('login_url', 'add_query_parameter', 10, 3);

function add_query_parameter($login_url, $redirect, $force_reauth) {
    $login_url = add_query_arg('custom_param', 'value', $login_url);
    return $login_url;
}

Change the login URL path

This example changes the login URL path to a custom path:

add_filter('login_url', 'change_login_path', 10, 3);

function change_login_path($login_url, $redirect, $force_reauth) {
    $login_url = str_replace('wp-login.php', 'custom-login', $login_url);
    return $login_url;
}

Force reauthorization for specific user roles

This example forces reauthorization for users with the “editor” role:

add_filter('login_url', 'force_reauth_editors', 10, 3);

function force_reauth_editors($login_url, $redirect, $force_reauth) {
    if (current_user_can('editor')) {
        $force_reauth = true;
    }
    return $login_url;
}

Add a custom redirect URL after login

This example sets a custom redirect URL after a successful login:

add_filter('login_url', 'custom_login_redirect', 10, 3);

function custom_login_redirect($login_url, $redirect, $force_reauth) {
    $redirect_url = 'https://example.com/after-login';
    $login_url = add_query_arg('redirect_to', urlencode($redirect_url), $login_url);
    return $login_url;
}

Modify the login URL based on user agent

This example changes the login URL if the user agent is a mobile device:

add_filter('login_url', 'mobile_login_url', 10, 3);

function mobile_login_url($login_url, $redirect, $force_reauth) {
    if (wp_is_mobile()) {
        $login_url = str_replace('wp-login.php', 'mobile-login', $login_url);
    }
    return $login_url;
}