9

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

0

3 Answers 3

6

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.
6

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

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).

2
  • 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
    Commented Jan 24, 2012 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.
    – Brooke.
    Commented Jan 25, 2012 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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