Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log a user in to Wordpress using only their user_id

Is there any way I can log a Wordpress user in given only their wp user_ID?

I'm sending users emails to confirm something and when they click on the given link to come to the website they need to be logged in to see the page I'm taking them to, so I have to log the user in and then do a header redirect.

I need a php function provided by wordpress, one that I can use in php, could you also give me any extra details as to how I can implement it (if any)

like image 512
pythonian29033 Avatar asked Apr 06 '12 09:04

pythonian29033


People also ask

How do I log in to a WordPress user?

Getting all the information of current user in wordpress using the predefined function wp_get_current_user(); <? php $current_user = wp_get_current_user(); echo "Username :". $current_user->user_login; echo "Username :".

How do I find the email ID of a WordPress user?

First of all you can get user email with the same get_user_by() function. $the_user = get_user_by( 'id', 54 ); // 54 is a user ID echo $the_user->user_email; The next method is just get_userdata() function, which is equal to get_user_by( 'id', ... )


2 Answers

You have to pass 2 parameters in wp_login hook. See Wp codex

wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login, $user );
like image 114
Vadym Avatar answered Oct 19 '22 01:10

Vadym


Create separate table to store all the links you sent and respective temp authentication code, which may be valid only for some time, then pass that temp auth code and email as a url param -

Write a code to validate user based on temp auth code, so that as soon as user clicks on email you can redirect him.

like image 33
Sandeep Manne Avatar answered Oct 19 '22 02:10

Sandeep Manne