Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

For a demo environment, I would like to pre-fill the username and password fields of the wp-login form with respectively "demo" and "demo" string.

Thus users will just need to click on the "Log-in" button to connect to the admin area.

There is a way to do that ? Maybe with a hook ?

Thanks for your advises

share|improve this question
    
Good Question +1 –  kaiser Jan 24 '12 at 17:24

3 Answers 3

up vote 5 down vote accepted

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.
share|improve this answer

Here are 3 ways to do it

  1. 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();

  2. Use jQuery Val and add it to your login page

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

  3. Modify wp-login.php (Note, this is not recommended although it would work)

share|improve this answer
    
1) would need a replacement for the whole wp-login.php file - How would you do that (serious Q - I'm interested)? Rewrite rules so your template would get triggered instead? 2) is valid, but would need jQuery and be wrapped into a jQuery( document ).ready( function( $ ) call to make $ available and the function working. You'd also need to call jQuery only for this. 3) Ok, well... Let's just say nothing about it :) –  kaiser Jan 24 '12 at 17:30
    
1) I'm not sure, I was mostly brain storming never done it. as for 2) it looks like jquery is already loaded in my login page. I was also being lazy and not wrapping my example in jQuery(document... tags. 3) It never happened. –  BandonRandon Jan 25 '12 at 9:22

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);
    }
}
share|improve this answer
    
Would be even cooler if you wouldn't take the ID, but instead get the user by name. +1 anyway. –  kaiser Jan 24 '12 at 19:32
2  
You can! get_user_by( 'login', 'demo' ); –  SickHippie May 17 '12 at 16:23
    
Wouldn't the init hook be enough? It is present everywhere... –  kaiser Sep 23 '12 at 13:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.