wordpress login user programmatically using password code example

Example 1: wordpress login user programmatically

<?php

function auto_login( $user ) {
    $username   = $user;
    // log in automatically
    if ( !is_user_logged_in() ) {
        $user = get_userdatabylogin( $username );
        $user_id = $user->ID;
        wp_set_current_user( $user_id, $user_login );
        wp_set_auth_cookie( $user_id );
        do_action( 'wp_login', $user_login );
    }     
}
?>

Example 2: wordpress login programmatically

<?php
 
  clean_user_cache($user_id);
  wp_clear_auth_cookie();
  wp_set_current_user($user_id);
  wp_set_auth_cookie($user_id, true, false);

  $user = get_user_by('id', $user_id);
  update_user_caches($user);

 ?>

Tags:

Php Example