Example 1 — A simple login link
Print a link to the login page.
<a href="<?php echo esc_url( wp_login_url() ); ?>">Log in</a> How It Works
wp_login_url()returns the address.esc_url()makes it safe.- We wrote the link tag ourselves.

In WordPress, wp_login_url() returns the URL of the login page, which is usually https://example.com/wp-login.php.
You can add a $redirect address so the visitor lands on that page after logging in. Use it to build your own "Log in" link.
It returns a string, so echo it inside an escape function.
<?php
echo esc_url( wp_login_url() ); | Call | What it does |
|---|---|
wp_login_url() | The login page address. |
wp_login_url( get_permalink() ) | Log in, then return to this post. |
wp_login_url( '', true ) | Ask the user to log in again. |
Always wrap the result in esc_url() when you print it.
wp_login_url( string $redirect = '', bool $force_reauth = false ): string | Parameter | Description |
|---|---|
$redirect | string. The address to open after login. Default is empty. |
$force_reauth | bool. If true, the user must log in again, even if already logged in. Default is false. |
| Return Value | Description |
|---|---|
| string | The login URL, with redirect_to and reauth added when needed. |
Three short theme examples. Each one shows the code and what it prints.
Print a link to the login page.
<a href="<?php echo esc_url( wp_login_url() ); ?>">Log in</a> wp_login_url() returns the address.esc_url() makes it safe.Send the visitor back here after login.
<?php if ( ! is_user_logged_in() ) : ?>
<a href="<?php echo esc_url( wp_login_url( get_permalink() ) ); ?>">
Log in to comment
</a>
<?php endif; ?> get_permalink() is the address of this post.redirect_to.Ask for the password again before a sensitive page.
<?php
$url = wp_login_url( home_url( '/billing/' ), true );
echo esc_url( $url ); true.reauth=1 to the URL.Print a Log in or Log out link, depending on the visitor.
7 people found this page helpful