I am using the following code to login a user.
$UserName and $Password is coming in via a Form. The following if statement is showing TRUE, so my user is getting logged in.
$user = get_user_by( 'login', $UserName );
if ( $user && wp_check_password( $Password, $user->data->user_pass, $user->ID) )
Now as long as the User is on the website I want his $UserName to be known by all the PHP programs he invokes.
Whenever I log someone in using the wp-admin screen the $UserName is brought in to all PHP programs by executing the following code:
global $current_user;
if ( isset($current_user) ) {
$UserName=$current_user->user_login;
}
My question is.... using the login at the top of this post how do I save the $Username in such a way that it is brought into other PHP programs using the above isset($current_user) if statement?
I tried the following code but it does not bring the $current_user field into other PHP programs by executing the IF (isset($current_user) statement. The $user->user_login field contents is equal to $UserName.:
$user_id = $UserName;
$user = get_user_by( 'id', $user_id );
$current_user= new WP_User( $user_id , $user->user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user->user_login );
global $current_user;
What am I missing? Any assistance will be greatly appreciated. Thank you
Related
I am using Gravityforms along with User registration add-on and have a form which when submitted should change the role of the current user to a new role without any underlying conditions.
Using the gravity\forms docs https://docs.gravityforms.com/gform_user_updated/#1-update-user-role and trying this:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // update 'role' to the name of the desired role
}
But its not working! Does anyone have any idea why this is incorrect or any other modifications to the code?
There's a couple things I see with your code when comparing it to the Gravity Forms doc.
Here's your code with some added comments:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
global $current_user; // you probably don't need this
get_currentuserinfo(); // you probably don't need this
$user_id = $current_user->ID; // $user_id should already be a numeric value passed in to the function containing the logged in user's ID so you shouldn't need to do this. You're resetting the $user_id variable here to whatever is being pulled out of the get_currentuserinfo() function, and I'm guessing that's the problem
//I would get rid of this echo and if statement
echo $user_id;
if( ! $user_id ) {
return;
}
$user = new WP_User( $user_id );
$user->set_role( 'role' ); // the word "role" here needs to be the role name
}
I think you can simplify it some. Try this instead:
add_action( 'gform_user_updated_3', 'change_role', 10, 3 );
function change_role( $user_id, $feed, $entry, $user_pass ) {
$user = new WP_User( $user_id );
$user->set_role( 'new_role_name_here' ); // Add an existing role here to update the user too
}
i am trying to redirect the user after login/signup to profile page but not sure why it is not putting user nickname/username in url.
$current_user = wp_get_current_user();
function
aloginuser( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
wp_redirect( "/profile/$current_user->user_login" );
exit;
}
add_action( 'user_register', 'aloginuser' );
It need to start a redirect to: site.com/profile/username
You are passing $current_user->user_login as string "$current_user->user_login" and not the value stored in the variable. You have to write it like this:
wp_redirect("/profile/".$current_user->user_login);
Also, it is necessary to move the $current_user = wp_get_current_user(); inside the function.
<?php
global $current_user;
get_currentuserinfo();
$user_id = $current_user->ID;
$user_fields = get_user_meta( $user_id, , 'true' );
echo $user_fields;
?>
It showing 'Array'. I would want to know the field to be used on counting the number of times the user logins.
use:
var_dump($user_fields)
It would show you the variable content !
use
$user_fields = get_user_meta( $user_id );
print_r($user_fields);
it will showing all meta field regarding $user_id
In wordpress there is no any pre-defined functionality is available
for get user login count.. You have to options for doing this..
either create a plugin or write the follwing custom code on
functions.php file..
Here I have create a variable user_login_count in user meta key in regards to user id, so everytime user successfully login, i m increasing the user count by 1..
function ulc_my_login_redirect($redirect_to, $request, $user) {
//is there a user to check?
global $user, $wpdb;
if (isset($user->ID)) {
if ( get_user_meta($user->ID, 'user_login_count', true) ){
$user_login_count = get_user_meta($user->ID, 'user_login_count', true);
$user_login_count++;
update_user_meta($user->ID, 'user_login_count', $user_login_count);
}else{
add_user_meta($user->ID, 'user_login_count', '1');
}
}
return $redirect_to;
}
add_filter('login_redirect', 'ulc_my_login_redirect', 10, 3);
By using this filter you can get the key value of 'user_login_count' user get_post_meta function..
I have been trying to enable the user login by email. I found at wordpress codex and some few blogs the way to write the following code in functions.php
add_action( 'wp_authenticate', 'wp_authenticate_by_email' );
function wp_authenticate_by_email( $username ) {
$user = get_user_by( 'email', $username );
if ( empty( $user ) ) {
return;
}
return $user->user_login;
}
But it does not work for me. I found it is remained as earlier.
I had a similar issue but got this to work:
function my_authenticate_by_email( $user, $username, $password ) {
//try to get user by email
$user = get_user_by( 'email', $username );
//validate we got a user.
if ($user && !is_wp_error($user))
{
//use normal auth
return wp_authenticate_username_password(null, $user->user_login, $password);
}
//continue to next filter, like normal.
return null;
}
add_filter( 'authenticate', 'my_authenticate_by_email', 0, 3);
How or where I must edit wordpress to make user autentication with email and not with username (as it is default)
This should work, add this to the functions.php file:
// remove the default filter
remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
// add custom filter
add_filter( 'authenticate', 'fb_authenticate_username_password', 20, 3 );
function fb_authenticate_username_password( $user, $username, $password ) {
// If an email address is entered in the username box,
// then look up the matching username and authenticate as per normal, using that.
if ( ! empty( $username ) )
$user = get_user_by( 'email', $username );
if ( isset( $user->user_login, $user ) )
$username = $user->user_login;
// using the username found when looking up via email
return wp_authenticate_username_password( NULL, $username, $password );
}
(The above was found here and I tested it and worked for me)
EDIT: This plugin works as well if you don't want to modify the functions file.