<?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..
Related
I'm trying to update the core user fields in WooCommerce when I edit a users profile, however, when I save, only the user meta saves.
But the wp_update_user is not saving the distributor name in the billing_company field.
The code is fired in a function called from
add_action('edit_user_profile_update', 'user_profile_update_action');
The code in the function is ...
add_action('edit_user_profile_update', 'user_profile_update_action');
function user_profile_update_action($user_id) {
if(isset($_POST['distributor_id']) AND $_POST['distributor_id'] == "|"){
delete_metadata( $user_id, 'distributor_id', '');
delete_metadata( $user_id, 'distributor_name', '');
}else{
$distributordata = explode("|", $_POST['distributor_id']); // Split the array
update_user_meta($user_id, 'distributor_id', $distributordata[0] );
update_user_meta($user_id, 'distributor_name', $distributordata[1] );
wp_update_user(array('ID' => $user_id, 'billing_company' => $distributordata[1]));
}
}
I've tried update user meta and wp update user but neither want to save billing company.
$customer = new WC_Customer( $user_id );
$customer->set_billing_company($distributordata[1]);
$customer->save();
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 working on a woocommerce project and my requirement is that whenever a new user registration occurs there should an account number generated which should be mailed to him so I am trying to use the update_user_meta function but not understanding how to get the User_id for somebody who has not yet registered
Please check my code and advise
add_action('woocommerce_register_form', 'vmart_add_account_number');
function vmart_add_account_number() {
global $wpdb;
$userdata = array();
$user_id = wp_insert_user($userdata);
$account_number = 1;
update_user_meta($user_id, 'account_number', $account_number);
$account_number++;
}
I have also tried this code
add_action('user_register', 'vmart_add_account_number', 10, 1);
function vmart_add_account_number($user_id)
{
$accountnumber = 1;
update_user_meta($user_id, 'account_number', $accountnumber);
}
For this thing you can use standard wordpress hook:
// Register subcustomer to same customer
add_action('user_register', 'vmart_add_account_number', 10, 1);
function vmart_add_account_number($user_id)
{
update_user_meta($user_id, 'account_number', $account_number);
}
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.
I am trying to redirect users to specific page. It works for a single users but i also want to redirect each users to specific page based on their login information.
How can i edit this code $user_info = get_userdata(9100008); to
get current logged in users user_data?.
add_filter('woocommerce_login_redirect', 'wc_login_redirect');
function wc_login_redirect( ) {
$user_info = get_userdata(9100008);
//$user_info = get_current_user_id();
//$user_id = get_current_user_id();
if ($user_id == 0) {
$redirect_to = 'http://example.com/'.$user_info->user_login.'/';
return $redirect_to;
}
}
There's a function exactly for this purpose called get_current_user_id() (which is commented out in your code, for some reason).
Read more in the Codex.
If you need the current user OBJECT (as your code implies), you can use wp_get_current_user().
With
global $current_user;
//example for id;
$id=$current_user->ID;
or
$data=get_currentuserinfo();
Snippet
add_filter('woocommerce_login_redirect', 'wc_login_redirect');
function wc_login_redirect($redirect_to ) {
global $current_user;
$id=$current_user->ID;
if ($id == 0) // this makes no sense, because this will never be true. Should probably be $id!=0
{
$redirect_to = 'http://example.com/'.$current_user->user_login.'/';
}
return $redirect_to;
}