Wordpress - How to prefill the username/password fields on the login page

The Result

enter image description here

Username

You can declare the form field value global and prefill it.

This will override any setting done via the "Remember me"-Checkbox.

/**
 * Changes the default user name to "DEMO"
 * 
 * @return string $user_login
 */
function login_form_username()
{
    global $user_login;
    return $user_login = 'DEMO';
}
add_action( 'login_head', 'login_form_username' );

Password

I found nothing that you can use to prefill it, so I'd add a note. Gladly/Sadly there's the whole set of stylesheets for the admin UI available. So .wrap in connection with h2 gives a nice styling. The function is hooked to the additional fields hook for the admin hook. The priority is 0 to set it above additional fields.

/**
 * Adds a note beyond the user login name & password field
 * 
 * @return string
 */
function login_form_note()
{
    print '<div class="wrap"><h2 style="text-align: center;">The Password is "DEMO"</h2></div>';
}
add_action( 'login_form', 'login_form_note', 0 );

Security: Hide Errors

As someone would get a visual proof that he entered an existing user name by default, I'd add the following to your functions.php file, to avoid telling if the username was guessed right:

/**
 * Hide wrong login names
 * 
 * @return string
 */
function no_login_error() 
{
    return __( 'Wrong Credentials.' );
}
add_filter( 'login_errors', 'no_login_error' );

Other filters

Notes:

  • If you want to replace other stuff like styles, then you can use the login_enqueue_scripts hook.
  • You can also replace the link behind the logo using the login_headerurl filter that filters the url. The link title can be replaced using login_headertitle. Both trigger for multisite as well a single site setups.
  • The login message can be changed using the login_message filter.

you could use this code to auto login the user:

add_action('init', 'auto_login');
add_action('admin_init', 'auto_login');
function auto_login() {
    if (!is_user_logged_in()) {
      //by user name
      $user = get_user_by( 'login', 'demo' );
      //Or by user id, 2 being the ID of the demo user
      //$user = get_userdata(2);
      wp_set_current_user($user->ID, $user->user_login);
      wp_set_auth_cookie($user->ID);
      do_action('wp_login', $user->user_login);
    }
}

Here are 3 ways to do it:

The first way is to use the wp_signon hook

$creds = array();    
$creds['user_login'] = 'example';   
$creds['user_password'] = 'plaintextpw';   
$creds['remember'] = true;   
$user = wp_signon( $creds, false );  
if ( is_wp_error($user) ) {  
   echo $user->get_error_message();
}

The second way, is to use jQuery .val() and add it to your login page

$("#user_login").val('username');    
$("#user_pass").val('password');

The last way is to edit your wp-login.php file (Note, this is not recommended although it would work).

Tags:

Login