Wordpress - How to login user to frontend programmatically

13,751

Solution 1

The simplest fix is to use wp_login_form() see reference: http://codex.wordpress.org/Function_Reference/wp_login_form If i understand correctly this should handle everything you want. This will set the auth cookie and can redirect to the page you want, the login works globally

Solution 2

On codex wp_set_current_user there is an example to set the current user and log them in.

$user_id = 12345;
$user = get_user_by( 'id', $user_id ); 
if( $user ) {
    wp_set_current_user( $user_id, $user->user_login );
    wp_set_auth_cookie( $user_id );
    do_action( 'wp_login', $user->user_login );
}
Share:
13,751
Alen
Author by

Alen

Updated on November 28, 2022

Comments

  • Alen
    Alen over 1 year

    I created function for loging in users on frontend using this example: https://gist.github.com/iandunn/8162246

    After user logs in is_user_logged_in() function returns true only inside this function where I placed code for login part.

    How do I login users globally? This is my code:

    function programmatic_login( $username ) {
        if ( is_user_logged_in() ) {
            wp_logout();
        }
        add_filter( 'authenticate', 'allow_programmatic_login', 10, 3 );
        $user = wp_signon( array( 'user_login' => $username ) );
        remove_filter( 'authenticate', 'allow_programmatic_login', 10, 3 );
        if ( is_a( $user, 'WP_User' ) ) {
            $user_id = $user->ID;
    
            if( $user ) {
                wp_set_current_user( $user_id, $user->user_login );
                wp_set_auth_cookie( $user_id );
                do_action( 'wp_login', $user->user_login );
            }
            if ( is_user_logged_in() ) {
                return true;
            }
        }
    
        return false;
    }
    
    function allow_programmatic_login( $user, $username, $password ) {
        return get_user_by( 'login', $username );
    }
    
    function process_login(){
    
        // this comes from login form
        $username = $_POST["login_username"];
    
        programmatic_login( $username );
    
        // it returns true only here, on any other function it returns false
        if(is_user_logged_in()){
            echo "ok";
        }else{
            echo "not ok";
        }
    
    }
    

    This is one example where I try to check if user is logged in, outside previous function:

    add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
    
    function add_login_logout_link($items, $args) {
    
        $loginPage = get_page_by_title("Login");
        $registerPage = get_page_by_title("Register");
    
        if(is_user_logged_in()){
            $items .= "<li><a href='" . wp_logout_url('index.php') . "' title='Logout'>Logout</a></li>";
        }else{
            $items .= "<li><a href='". site_url() . '/' . '?page_id=' . $loginPage->ID ."'>Login</a></li><li><a a href='". site_url() . '/' . '?page_id=' . $registerPage->ID ."'>Register</a></li>";
        }
    
        return $items;
    }
    
  • Alen
    Alen about 9 years
    That was so easy. Thank you a lot