wp_login_url( string $redirect = '', bool $force_reauth = false )

Retrieves the login URL.


Parameters Parameters

$redirect

(string) (Optional) Path to redirect to on log in.

Default value: ''

$force_reauth

(bool) (Optional) Whether to force reauthorization, even if a cookie is present.

Default value: false


Top ↑

Return Return

(string) The login URL. Not HTML-encoded.


Top ↑

More Information More Information

$redirect argument must be absolute, such as http://example.com/mypage/. For best results, use site_url( ‘/mypage/ ‘ ).


Top ↑

Source Source

File: wp-includes/general-template.php

function wp_login_url( $redirect = '', $force_reauth = false ) {
	$login_url = site_url( 'wp-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 );
	}

	/**
	 * Filters the login URL.
	 *
	 * @since 2.8.0
	 * @since 4.2.0 The `$force_reauth` parameter was added.
	 *
	 * @param string $login_url    The login URL. Not HTML-encoded.
	 * @param string $redirect     The path to redirect to on login, if supplied.
	 * @param bool   $force_reauth Whether to force reauthorization, even if a cookie is present.
	 */
	return apply_filters( 'login_url', $login_url, $redirect, $force_reauth );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 2 content

    For the redirect example to the current URL via get_permalink() above, note that if the request is a 404, get_permalink() will return false, so you may want to grab the actual URL instead.

    A scenario where it could be useful to still redirect to your current URL even if it was a 404, would be if the current URL was a private post, and your user was not yet logged-in – hence ended up on a 404.

    In that case, if you show them a login link, they should be redirected back to the current URL (i.e. the private post), so that they finally can access it.

    <?php $current_url = home_url( add_query_arg( [], $GLOBALS['wp']->request ) ); ?>
    <a href="<?php echo esc_url( wp_login_url( $current_url )  ); ?>"><?php _e( 'Log in' ) ?></a>

You must log in before being able to contribute a note or feedback.