# Programmatically Login by using wp_set_auth_cookie

**URL:** https://discourse.roots.io/t/programmatically-login-by-using-wp-set-auth-cookie/26313
**Category:** bedrock
**Created:** 2023-11-23T04:03:38Z
**Posts:** 1

## Post 1 by @cloud_webexpert — 2023-11-23T04:03:38Z

I have user id and need to login that user in WordPress but it will not Work

$user\_id= intval($\_POST[‘user\_id’]);  
$user = get\_user\_by(‘id’, $user\_id);

```
if ( ! is_wp_error( $user ) ) {

        wp_clear_auth_cookie();
        wp_set_auth_cookie( $user_id, false, is_ssl() );
        wp_set_current_user( $user_id );
        do_action('wp_login', $user->user_login, $user);
        ob_end_flush();
        wp_safe_redirect(home_url('/my-orders'));    
        exit();
    }
```

Please review the above code and let me know what is wrong i did that is not work with bedrock, Please help me why this code not works and possible solution in bedrock.
