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)
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 :".
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', ... )
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 );
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With