Activate and Login user after registration Gravity Form - wordpress

I wanted to automatically activate and login user after registration on my site using gravityform is there a way?
.I did some research and currently some of the guides are outdated.I tried this code,currently its not working
function autologin($user_id, $config, $entry, $password) {
wp_set_auth_cookie($user_id, false, '');
}
add_action("gform_user_registered", "autologin", 10, 4);

This checks if the user is logged in or not. If not, they will auto-login upon form submission.
/**
* Auto login after registration if not logged in.
*/
function ip_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
// Only automatically login if we aren't *already* logged in
if ( ! is_user_logged_in() ) {
// Get the user data (for the login)
$user = get_userdata( $user_id );
// Sign the user in
wp_signon( array(
'user_login' => $user->user_login,
'user_password' => $password,
'remember' => false, // Don't set the remember cookie
));
}
}
add_action( 'gform_user_registered', 'ip_gravity_registration_autologin', 10, 4 );

Did you try this?
function pi_gravity_registration_autologin( $user_id, $user_config, $entry, $password ) {
$user = get_userdata( $user_id );
$user_login = $user->user_login;
$user_password = $password;
wp_signon( array(
'user_login' => $user_login,
'user_password' => $user_password,
'remember' => false
), false );
wp_set_current_user( $user_id, $user_login );
wp_set_auth_cookie( $user_id, true, false );
do_action( 'wp_login', $user_login );
}

Here's a modified version of the code from my Gravity Forms Auto Login plugin that handles this.
add_action( 'gform_user_registered', 'gw_auto_login', 10, 4 );
function gw_auto_login( $user_id, $feed, $entry, $password ) {
$user = new WP_User( $user_id );
$user_data = array(
'user_login' => $user->user_login,
'user_password' => $password,
'remember' => false
);
$result = wp_signon( $user_data );
if( ! is_wp_error( $result ) ) {
global $current_user;
$current_user = $result;
}
}
The plugin accounts for many other Gravity Forms scenarios that might interfere with automatically logging the user in.

Related

Automatically assign new user_email and restrict update after first login

How can I automatically assign a new email to a user in WordPress AND restrict any further update after the first login ?
For example setting the email to the following structure:
{user_id}#example.com
I've tried a few things but couldn't figure it out, any help is appreciated.
I wrote this code, but only users whose email has been registered, their email has been edited, other people who have not registered email, no email has been registered for them. How do I solve this?
my code:
add_action ('admin_head','set_auto_mail');
function set_auto_mail() {
$users = get_users(array('fields'=>'all'));
foreach($users as $user){
$user = get_userdata($user->ID);
if ( in_array( 'subscriber', (array) $user->roles ) ) {
$mail = ($user->ID)."#example.com";
if($mail!=' ') wp_update_user( array ('ID' => $user->ID, 'user_email' => $mail) );
else wp_update_user( array ('ID' => $user->ID, 'user_email' => $user->user_email) );
if($user->user_email == '')
wp_update_user( array ('ID' => $user->ID, 'user_email' => $user->user_email) );
}
}
}
This should do exactly what you wanted.
We set a new user meta data called is_new_user which we will use later on to detect if this is the first ever login for a specific user.
We set a hook to update the user_email on first time login and we update is_new_user to specify that this isn't a new user anymore. Like that we prevent permanent update on login.
Then we fire up the necessary functions to disable email updates. Visually and from the backend for all non-new users.
I'm using first_name, las_name and ID for the email structure. Output:
$first_name.$last_name$ID#bogus.com
<?php
/**
* Adds meta data is_new_user to a user, which let us detect new users on first login.
*
* Fires immediately after a new user is registered.
*
* #link https://developer.wordpress.org/reference/hooks/wp_login/
*/
add_action( 'user_register', 'wpso66983605_add_user_meta_is_new_user' );
function wpso66983605_add_user_meta_is_new_user( $user_id ) {
add_user_meta( $user_id, 'is_new_user', 1, true );
};
/**
* Update user email on first login.
*
* Fires after the user has successfully logged in.
*
* #link https://developer.wordpress.org/reference/hooks/wp_login/
*/
add_action( 'wp_login', 'wpso66983605_set_user_email_on_first_login', 10, 2 );
function wpso66983605_set_user_email_on_first_login( $user_login, $user ) {
if ( ! get_user_meta( $user->ID, 'is_new_user', true ) ) {
return;
};
if ( get_user_meta( $user->ID, 'is_new_user', true ) ) {
update_user_meta( $user->ID, 'is_new_user', 0, 1 );
$userdata = array (
'ID' => $user->ID,
'user_email' => sanitize_email( sanitize_title_with_dashes( $user->first_name . '.' . $user->last_name . $user->ID) . '#bogus.com' ),
);
wp_update_user( $userdata );
};
};
/**
* Prevent users from changing their email address
*
* Modified to ONLY fire when a user is considered as non-new user (user has already made at least 1 login).
*
* #link https://wordpress.stackexchange.com/a/363376/190376
*/
if ( ! class_exists( 'DisableMailChange' ) ) {
class DisableMailChange {
public function __construct() {
add_action( 'personal_options_update', array( $this, 'disable_mail_change_BACKEND' ), 5 );
add_action( 'show_user_profile', array( $this, 'disable_mail_change_HTML' ) );
}
public function disable_mail_change_BACKEND( $user_id ) {
if ( ! get_user_meta( get_current_user_id(), 'is_new_user', true ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
$user = get_user_by( 'id', $user_id );
$_POST['email'] = $user->user_email;
};
};
}
public function disable_mail_change_HTML( $user ) {
if ( ! get_user_meta( get_current_user_id(), 'is_new_user', true ) ) {
if ( ! current_user_can( 'manage_options' ) ) {
echo '<script>document.getElementById("email").setAttribute("disabled","disabled");</script>';
};
};
}
};
new DisableMailChange();
};

Update Username (User_Login) on Email Change (WooCommerce)

A number of questions have been raised on StackOverflow in regards to using email address as username when registering via My Account. I have the working code included below.
$this->loader->add_filter('pre_user_login', $plugin_public, 'audp_wc_register_email_as_username' );
public function audp_wc_register_email_as_username( $user_login ) {
if ( isset ( $_POST['email'] ) ) {
$user_login = $_POST['email'];
}
return $user_login;
}
I've updated my own question with the following code to update the user_login details.
$this->loader->add_action( 'woocommerce_save_account_details', $plugin_public, 'audp_wc_update_email_and_username', 20, 1 );
public function audp_wc_update_email_and_username() {
if ( isset( $_POST['account_email'] ) ) {
global $wpdb;
$user_id = get_current_user_id();
$new_login = $_POST['account_email'];
// Update user user_login
$wpdb -> update( $wpdb -> users,
array( 'user_login' => $new_login, 'user_nicename' => $new_nicename ),
array( 'ID' => $user_id )
);
}
// Update nickname
update_user_meta( $user_id, 'nickname', $new_login );
}
I've updated the question to include the full code. Because we are dealing with user_login we cannot use wp_update_user(), I have used the class description above, but the below code is standard (non-class) functions.
add_action( 'woocommerce_save_account_details', 'audp_wc_update_email_and_username', 20, 1 );
function audp_wc_update_email_and_username() {
if ( isset( $_POST['account_email'] ) ) {
global $wpdb;
$user_id = get_current_user_id();
$new_login = $_POST['account_email'];
// Update user user_login
$wpdb -> update( $wpdb -> users,
array( 'user_login' => $new_login, 'user_nicename' => $new_nicename ),
array( 'ID' => $user_id )
);
// Update nickname
update_user_meta( $user_id, 'nickname', $new_login );
}

I can registration user without login using wp_insert_user?

I use wp_insert_user to add a user, but there you need to have a username and password, and I only need mail without a username and password. Just use this function, because there is not a bad functionality, which I then need.
$user_company = $array_jsons['company'];
$user_email = $array_jsons['email'];
$role = $array_jsons['role'];
$info = array(
'user_login' => '', // how i do without login
'user__pass' => '', // how i do without login
'user_company' => $user_company,
'user_email' => $user_email,
'display_name' => $user_email,
'role' => $role
);
$result = wp_insert_user( $info );
if(is_wp_error( $result)) {
echo $result->get_error_message();
} else {
echo "Added user {$result}";
}
Please try to add the emails as username and password, because username and password are the mandatory fields
$user_company = $array_jsons['company'];
$user_email = $array_jsons['email'];
$role = $array_jsons['role'];
$info = array(
'user_login' => $user_email, // how i do without login
'user__pass' => $user_email, // how i do without login
'user_company' => $user_company,
'user_email' => $user_email,
'display_name' => $user_email,
'role' => $role
);
$result = wp_insert_user( $info );
if(is_wp_error( $result)) {
echo $result->get_error_message();
} else {
echo "Added user {$result}";
}

Wordpress - How to login user to frontend programmatically

I created function for loging in users on frontend using this example: https://gist.github.com/iandunn/8162246
After user logs in is_user_logged_in() function returns true only inside this function where I placed code for login part.
How do I login users globally?
This is my code:
function programmatic_login( $username ) {
if ( is_user_logged_in() ) {
wp_logout();
}
add_filter( 'authenticate', 'allow_programmatic_login', 10, 3 );
$user = wp_signon( array( 'user_login' => $username ) );
remove_filter( 'authenticate', 'allow_programmatic_login', 10, 3 );
if ( is_a( $user, 'WP_User' ) ) {
$user_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 );
}
if ( is_user_logged_in() ) {
return true;
}
}
return false;
}
function allow_programmatic_login( $user, $username, $password ) {
return get_user_by( 'login', $username );
}
function process_login(){
// this comes from login form
$username = $_POST["login_username"];
programmatic_login( $username );
// it returns true only here, on any other function it returns false
if(is_user_logged_in()){
echo "ok";
}else{
echo "not ok";
}
}
This is one example where I try to check if user is logged in, outside previous function:
add_filter('wp_nav_menu_items', 'add_login_logout_link', 10, 2);
function add_login_logout_link($items, $args) {
$loginPage = get_page_by_title("Login");
$registerPage = get_page_by_title("Register");
if(is_user_logged_in()){
$items .= "<li><a href='" . wp_logout_url('index.php') . "' title='Logout'>Logout</a></li>";
}else{
$items .= "<li><a href='". site_url() . '/' . '?page_id=' . $loginPage->ID ."'>Login</a></li><li><a a href='". site_url() . '/' . '?page_id=' . $registerPage->ID ."'>Register</a></li>";
}
return $items;
}
On codex wp_set_current_user there is an example to set the current user and log them in.
$user_id = 12345;
$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 );
}
The simplest fix is to use wp_login_form()
see reference: http://codex.wordpress.org/Function_Reference/wp_login_form
If i understand correctly this should handle everything you want.
This will set the auth cookie and can redirect to the page you want, the login works globally

Run plugin code from "functions.php" in WordPress

I have a plugin for allowing duplicate emails on registration and that is working fine if I have used the plugin. I want to use plugin code in functions.php (meaning to say run the plugin code functionality with themes).
Below is my plugin code:
/**
* Plugin Name: Allow duplicate emails on registration
* Plugin URI: http://wordpress.stackexchange.com/a/125129/26350
*/
add_action( 'plugins_loaded', array( 'Allow_Duplicate_Emails_Registration', 'get_instance' ) );
class Allow_Duplicate_Emails_Registratn
{
private $rand = '';
static private $instance = NULL;
static public function get_instance()
{
if ( NULL === self::$instance )
self::$instance = new self;
return self::$instance;
}
public function __construct()
{
// Generate some random string:
$this->rand = '_remove_' . rand( 0, 99999);
add_filter( 'user_registration_email', array( $this, 'user_registration_email' ) );
add_action( 'user_register', array( $this, 'user_register' ) );
}
public function user_registration_email( $user_email )
{
// inject random string into the email
$user_email = str_replace( '#', $this->rand . '#', $user_email );
return $user_email;
}
public function user_register( $user_id )
{
// retrieve the newly registered user object
$user = get_user_by( 'id', $user_id );
// remove the random string
$user_email = str_replace( $this->rand . '#', '#', $user->user_email );
// update the email of the newly registered user
$args = array(
'ID' => $user_id,
'user_email' => $user_email,
);
$result = wp_update_user( $args );
}
}
And I have changed the above code below and put in functions.php, but it is not working:
add_action( 'init', array( 'Allow_Duplicate_Emails_Registration', 'get_instance' ) );
I have solved the problem. Add the below code in the wp-content/themes/my-themes/functions.php file:
<?php
add_filter( 'user_registration_email', array( $this, 'user_registration_email' ) );
add_action( 'user_register', array( $this, 'user_register' ) );
function user_registration_email( $user_email )
{
// Inject a random string into the email
$user_email = str_replace( '#', $this->rand . '#', $user_email );
return $user_email;
}
function user_register( $user_id )
{
// Retrieve the newly registered user object
$user = get_user_by( 'id', $user_id );
// Remove the random string
$user_email = str_replace( $this->rand . '#', '#', $user->user_email );
// Update the email of the newly registered user
$args = array(
'ID' => $user_id,
'user_email' => $user_email,
);
$result = wp_update_user( $args );
}
?>

Resources