Home WordPress General Functions wp_login_form() Definition In WordPress, wp_login_form() outputs a login form that you can place on any page, such as a sidebar or a custom "My account" page.
After a successful login, the visitor goes to the address you set with redirect. If you do not set it, they return to the current page.
Syntax Show the form only to visitors who are not logged in.
<?php
if ( ! is_user_logged_in() ) {
wp_login_form();
} Call What it does wp_login_form()Prints the login form. wp_login_form( array( 'redirect' => home_url() ) )Sends the user to the home page after login. wp_login_form( array( 'echo' => false ) )Returns the form as a string.
Use is_user_logged_in() to hide the form from logged-in users.
Full declaration wp_login_form( array $args = [] ): void|string Parameters Parameter Description $argsarray. Options include echo, redirect, form_id, label_username, label_password, label_remember, label_log_in, remember, and value_username. Default is an empty array.
Return Value Return Value Description void When echo is true (default), the form is printed. string When echo is false, the form HTML is returned.
WordPress Examples Three short theme examples. Each one shows the code and what it prints.
1. Show a login form 2. Redirect after login 3. Change the labels Example 1 — Show a login form Put a form in a sidebar for guests.
<?php if ( ! is_user_logged_in() ) : ?>
<h3>Member login</h3>
<?php wp_login_form(); ?>
<?php endif; ?> <h3>Member login</h3>
<form name="loginform" id="loginform" action="https://example.com/wp-login.php" method="post">
<p class="login-username">
<label for="user_login">Username or Email Address</label>
<input type="text" name="log" id="user_login" ...>
</p>
<p class="login-password">
<label for="user_pass">Password</label>
<input type="password" name="pwd" id="user_pass" ...>
</p>
<p class="login-remember"><label><input name="rememberme" type="checkbox" id="rememberme" value="forever"> Remember Me</label></p>
<p class="login-submit"><input type="submit" name="wp-submit" id="wp-submit" class="button button-primary" value="Log In"></p>
</form> How It Works is_user_logged_in() is false for guests.The form posts to wp-login.php. WordPress handles the login for you. Example 2 — Redirect after login Send members to their account page.
<?php
wp_login_form( array(
'redirect' => home_url( '/my-account/' ),
) ); <form name="loginform" ...>
...
<input type="hidden" name="redirect_to" value="https://example.com/my-account/">
</form> How It Works redirect sets the page after a good login.home_url() builds the full address.A hidden field carries the address to wp-login.php. Example 3 — Change the labels Use friendlier text.
<?php
wp_login_form( array(
'label_username' => 'Your email',
'label_password' => 'Your password',
'label_log_in' => 'Sign in',
'remember' => false,
) ); <label for="user_login">Your email</label>
<label for="user_pass">Your password</label>
<input type="submit" ... value="Sign in">
(no "Remember Me" checkbox) How It Works The label args change the text. remember is false, so the checkbox is hidden.Everything else stays the same. Frequently Asked Questions What does wp_login_form() do? It prints a ready-made login form that you can place on any page of your site.
Where does the user go after logging in? To the address in the redirect argument. If it is empty, they return to the current page.
How do I hide the form for logged-in users? Wrap it in if ( ! is_user_logged_in() ).
How do I return the form instead of printing it? Pass array( 'echo' => false ) and echo the result yourself.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
6 people found this page helpful
Helpful Share Copy link Suggestion