I have a plugin for registration process in wordpress. There was no password field but I created a password field and inserted the password instead of sending random password in emails. It is using wp_create_user function to create the user.
Now, I am trying to make the automatic login after registration but failed in it.
I tried the following function but failed in it. Please someone help.
wp_set_current_user($user_id); // set the current wp user
wp_set_auth_cookie($user_id); // start the cookie for the current registered user
wp_redirect(home_url());
Here is the process that I have been using, it's not complete but how I think that it must work.
$status = wp_create_user($username, $user_pass, $email);
$user_data = $wpdb->get_row("SELECT * FROM $users_table where user_login='$username' ");
$user_id = isset($user_data) ? $user_data->ID : 0;
if (is_wp_error($status)) {
$errors[] = language_code('USER_NAME_ALREADY_EXIST_PLEASE_TRY_ANOTHER_ONE');
} else {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
wp_redirect( home_url() );
}
Try following code into your theme functions.php file,It will work
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
wp_redirect( home_url() );
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
Related
I want to redirect the users of my site to a custom thank you page after they create a page and send it for review.
I found this snippet, but this one is for posts, and it's for publishing, not for pending review. The role that I want to use for this snippet is Tutor Instructor.
Can somebody help me to edit this snippet? It's a WordPress site.
add_action( 'save_post', 'redirect_user_page_list', 10, 3 );
function redirect_user_page_list( $post_ID, $post, $update ) {
if( is_user_logged_in() ) {
$user = wp_get_current_user();
$role = ( array ) $user->roles;
if ( 'user_role' == $role[0] ) {
$url = '';
wp_redirect($url);
exit;
}
}
}
//replace user_role with your actual role the one for which you need to implement this functionality. Also place the proper url in $url.
You can execute this code after your code
window.location.href = 'https://yoursite.com/thank-you';
I'm using the code below to redirect users to a welcome page upon their first login. It works a treat, but I need to apply it to the 'subscriber' user role only.
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
// insert meta that user not logged in first time
update_user_meta($user_id, 'prefix_first_login', '1');
}
// hook when user logs in
add_action('wp_login', 'your_function', 10, 2);
function your_function($user_login, $user) {
$user_id = $user->ID ;
// getting prev. saved meta
$first_login = get_user_meta($user_id, 'prefix_first_login', true);
// if first time login
if( $first_login == '1' ) {
// update meta after first login
update_user_meta($user_id, 'prefix_first_login', '0');
// redirect to given URL
wp_redirect( 'http://www.example-url.com' );
exit;
}
}
Bearing in mind I don't know anything, to me it seems like I need to add another 'if' element. I.e. If 'it's the user's first-time login' and 'the user has a subscriber role'. But I've no idea!
Please could someone help me amend this?
I created a new user and i gave him “author” role. I want him to only can write posts in wordpress. But when he logs in, he enters the user profile but not wordpress dashboard which is where i want. How can i fix this? I want him to go to wordpress dashboard so he can write posts only.
Try this,
<?php
// check if current user is the post author
global $current_user;
get_currentuserinfo();
if (is_user_logged_in() && $current_user->ID == $post->post_author) {
wp_redirect( admin_url( 'the url you need to redirect' ) );
exit;
}
?>
Add this code
function check_custom_authentication () {
$user_id = get_current_user_id();
$user_meta = get_userdata($user_id);
$user_roles = $user_meta->roles;
if ( in_array('author', $user_roles, true ) ) {
wp_redirect(admin_url());
exit;
}
}
add_action( 'wp_login' , 'check_custom_authentication' );
I want to insert some user meta data to determine if a user has logged in before or not - with the intention of showing them a welcome screen if they havent.
I've google searched all these functions but they just don't seem to be working for me.
Here is my code...
add_action('init', function() {
$user_ID = get_current_user_id();
$has_visited = get_user_meta($userID, 'has_visited');
//check if has_visited key exists - if it doesn't, user's first visit
if(!$has_visited) {
//set the has_visited key
update_user_meta($user_ID, 'has_visited', 1);
echo 'user has not logged in before';
exit;
}
else { echo 'user has logged in before'; exit; }
});
Do these functions work on the init hook. I need them to so I can redirect the user to another page if it is their first visit.
Thanks in advance for your help and wisdom.
Because you have a typo here:
$has_visited = get_user_meta($userID, 'has_visited');
But, you sad this:
$user_ID = get_current_user_id();
So $user_ID and $userID is not the same.
Anyway, first you need to create another condition, to check, is the user logged in, because if not, then there will be no user ID.
So your complete code should be like this:
add_action('init', function() {
if (is_user_logged_in()) { //Added this
$user_ID = get_current_user_id();
$has_visited = get_user_meta($user_ID, 'has_visited', true); //Fixed this
//check if has_visited key exists - if it doesn't, user's first visit
if (!$has_visited) {
//set the has_visited key
update_user_meta($user_ID, 'has_visited', 1);
echo 'user has not logged in before';
exit;
} else {
echo 'user has logged in before';
exit;
}
}
echo "User not logged in";
});
I am trying to autologin a user after registration.
I am trying this in functions.php file:
add_action( 'user_register', 'auto_login_user' );
function auto_login_user($user_id) {
$user = new WP_User($user_id);
$user_login_var = $user->user_login;
$user_email_var = stripslashes($user->user_email);
$user_pass_var = $user->user_pass;
$creds = array();
$creds['user_login'] = $user_login_var;
$creds['user_password'] = $user_pass_var;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) )
echo $user->get_error_message();
}
I am getting ERROR:
The password you entered for the username "TheNewUserCreated" is incorrect. Lost your password?
How Can I take the password from the User object?
Also because this is a custom registration process in template registration.php I tried to take it with $_POST and run the function in that file but I didnt have any success that way too...
EDIT:
Ok I am getting the encrypted password, so what is the solution here, how can I autologin the user? Maybe I can do this in registration.php page?
Add below function to functions.php file
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
// You can change home_url() to the specific URL,such as
//wp_redirect( 'http://www.wpcoke.com' );
wp_redirect( home_url() );
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
If you are using wp_insert_user(); to register your users then to auto-login them it's simple.
This function returns the user ID if success, so use it to login that user.
$id = wp_insert_user($data);
//so if the return is not an wp error object then continue with login
if(!is_wp_error($id)){
wp_set_current_user($id); // set the current wp user
wp_set_auth_cookie($id); // start the cookie for the current registered user
}
but to follow what you have already it can be like this:
add_action( 'user_register', 'auto_login_user' );
function auto_login_user($user_id) {
wp_set_current_user($user_id); // set the current wp user
wp_set_auth_cookie($user_id); // start the cookie for the current registered user
}
//this code is a bit tricky, if you are admin and you want to create a user then your admin session will be replaced with the new user you created :)
function auto_login() {
$getuserdata=get_user_by('login',$_GET['login']);
$tuserid=$getuserdata->ID;
$user_id = $tuserid;
$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 );
}
}
add_action('init', 'auto_login');
i had have the same problem as your and found this best solution. try this in your functions.php file. and one thing more use submit your reset form in a function in functions.php file