wp_login_url()
Gets the login/authentication page URL: /wp-login.php
In the $redirect parameter, you can specify the URL to which you should return after authentication.
Used By: wp_loginout()
Hooks from the function
Returns
String. URL to the authentication page.
Usage
<?php echo wp_login_url( $redirect ); ?>
- $redirect(string)
- URL of the page to which you need to redirect after authentication.
Default: '' - $force_reauth(boolean)
- Enables forced re-authentication, even if the authentication cookies are already set.
Default: false
Examples
#1 Basic usage
echo wp_login_url(); // https://wp-kama.com/wp-login.php
<a href="<?= wp_login_url() ?>" title="Login">Log in</a>
#2 Authorize and Redirect to Current Page
<a href="<?= wp_login_url( get_permalink() ); ?>">Login</a>
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( $GLOBALS['wp']->request ); ?> <a href="<?= esc_url( wp_login_url( $current_url ) ); ?>"><?php _e( 'Log in' ) ?></a>
#3 Authorize and go to the home page:
<a href="<?= wp_login_url( home_url() ) ?>" title="Login">Log in</a>
Changelog
| Since 2.7.0 | Introduced. |