I want to get the the new registrant first name and display it on my email template:
$firstname = get_user_meta($userid,'first_name',true);
echo '<pre>';
print_r($firstname);
echo '</pre>'
But it is not returning the value. Any ideas?
Thanks
You could try get_userdata() wordpress function with the User ID, to get the user object. With this user object you can get all other data this way:
$user_data = get_userdata( $userid );
echo 'Username: ' . $user_data->user_login . '<br>';
echo 'First name: ' . $user_data->first_name . '<br>';
echo 'Last name: ' . $user_data->last_name . '<br>';
echo 'User roles: ' . implode(', ', $user_data->roles) . '<br>';
echo 'User ID: ' . $user_data->ID . '<br>';
// To look at the available User raw data:
echo '<pre>'; print_r($user_data); echo '</pre>';
// To look at the available User raw Meta data
echo '<pre>'; print_r(get_post_meta( $userid )); echo '</pre>';
To get the First name and last name, your registering form need to have additionally that 2 fields
For other emails notification, once an order has been created one time, you can get easily from the Order ID this data:
// In case that you have only the $order object
if(empty($order_id))
$order_id = $order->id;
$customer_id = get_post_meta( $order_id, '_customer_user', true);
$billing_first_name = get_post_meta( $order_id, '_billing_first_name', true);
$billing_last_name = get_post_meta( $order_id, '_billing_last_name', true);
// Displaying "First name"
echo 'First name: '. $billing_first_name;
The issue was how the user is created and this is my code for creating the user:
$user = wc_create_new_customer($email,$email,$password);
In order to fix that I change the code to:
$new_customer_data = apply_filters( 'woocommerce_new_customer_data', array(
'user_login' => $email,
'user_pass' => $password ,
'first_name' => $firstname ,
'last_name' => $lastname ,
'user_email' =>$email ,
'display_name' => $firstname . ' ' . $lastname ,
'nickname' => $firstname . ' ' . $lastname ,
'role' => 'customer'
) );
$user_id = wp_insert_user( $new_customer_data );
Related
I have tried various solutions for this and while the code below works and do not generate any errors in the log or anything like that, I am struggling with understanding how to get the name of the user role and not the actual account username.
Here's the code I am using:
global $current_user;
wp_get_current_user();
if ( is_user_logged_in()) {
echo 'Username: ' . $current_user->user_login .'';
echo '<br />';
echo 'User display name: ' . $current_user->display_name .'';
}
else { wp_loginout(); }
echo '<br>';
I want it to display like this:
Username: username
Account type: name of role
The username and account type should be on two separate rows.
The point of the account type is because I added this:
add_role ( 'business', __( 'Business', 'woocommerce' ), array( 'read' => true, ));
Try the following based on your custom role "business":
global $current_user;
if ( $current_user > 0 ) {
echo __('Username:') . ' ' . $current_user->user_login . '<br />';
$account_type = in_array('business', $current_user->roles) ? __('Business') : __('Customer');
echo __('Account type') . ' ' . $account_type . '<br />';
}
It should work as you wish.
To get the role name from a user role slug:
global $wp_roles;
$role = 'business';
$role_name = $wp_roles->roles[$role]['name'];
If each user has a unique user role, you can use the following:
global $current_user, $wp_roles;
if ( $current_user > 0 ) {
$role_name = $wp_roles->roles[reset($current_user->roles)]['name'];
echo __('Username:') . ' ' . $current_user->user_login . '<br />';
echo __('Account type') . ' ' . $role_name . '<br />';
}
Or you can get the last user role with:
$role_name = $wp_roles->roles[end($current_user->roles)]['name'];
I have this line I need to replace/edit to fit my needs. This code is extracting first name and last name from wordpress database:
'<strong>' . get_the_author_meta( 'display_name', $freelancer ) . '</strong>',
What I need is to show on website only first name and initial of the last name.
I have used bellow code in other context and working fine but I don't know how to adapt it to above code:
<?php
$first_name = get_the_author_meta( 'first_name', $freelancer );
$last_name = substr( get_the_author_meta( 'last_name', $freelancer ), 0, 1 ) . '.';
$display_name = $first_name . ' ' . $last_name;
echo $display_name;
?>
So my second code is working perfect but I need to replace first code with proper code to get the same result , I don't know how, anyway this is more of context code I need to alter:
$message = sprintf( __( '%s cancelled his project %s', ET_DOMAIN ),
'<strong>' . get_the_author_meta( 'first_name', $freelancer ) . '</strong>',
'<strong>' . get_the_title( $project ) . '</strong>'
);
On the admin side of Gravity form, I want to add a note if an admin user changes the status of the form entry. I have an admin only field so that bit is working and I know how to add note.
But can't work out, how to add note only if a certain field is changed. I need it to say something like 'status was updated from "approved" to "closed" by xxx'
Any help would be much appreciated.
Thanks.
add_action( 'gform_after_update_entry', function ( $form, $entry_id ) {
$current_user = wp_get_current_user();
$note = 'status updated from' . $status_from . ' to ' . $status_to . ' by ' . $current_user;
RGFormsModel::add_note( $entry_id, $current_user->ID, $current_user->display_name, $not );
}, 10, 2 );
Worked it out. For anyone who may need it. Answer below :)
add_action( 'gform_after_update_entry', 'update_entry', 10, 3 );
function update_entry( $form, $id, $original ) {
$entry = GFAPI::get_entry( $id );
$status_from = $original[ID_OF_THE_FIELD];
$status_to = $entry[ID_OF_THE_FIELD];
if($status_from != $status_to) {
$current_user = wp_get_current_user();
$message = 'Status updated from ' . $status_from . ' to ' . $status_to . ' by ' . $current_user->display_name;
RGFormsModel::add_note( $entry_id, $current_user->ID, $current_user->display_name, $message );
}
}
I want to display all my users info in my index.php
ie : User Name / Full Name / Social Links / Profile Picture etc etc.
Can any one tell me please how to display all wordpress users list in index.php without admin?
Code:
<?php
$blogusers = get_users( 'blog_id=1&orderby=nicename&role=subscriber' );
foreach ( $blogusers as $user ) { ?>
<?php
echo 'Username: ' . $user->user_login . '';
echo 'User email: ' . $user->user_email . '';
echo 'User first name: ' . $user->user_firstname . '';
echo 'User last name: ' . $user->user_lastname . '';
echo 'User display name: ' . $user->display_name . '';
?>
<?php } ?> .
I want user output in html div css styling. How to do that? Is there any other option?
If you want to exclude only administrator from user list then you
have to pass role__not_in argument from get_users.
$args = [
'blog_id' => 1,
'role__not_in' => ['administrator'],
'orderby' => 'nicename',
'order' => 'ASC',
'fields' => 'all',
];
$users = get_users($args);
print_r($users);
Hope this helps!
I want to display for users once logged in instead of the default word My Account I want to display the user's name, I tried this code but it doesnt display anything!
It seems it doesn't recognized the variable $current_userin the file located at: wp-content/themes/themeName/framework/functions/woo-account.php
printf( __( '%s', 'wpdance' ),$current_user->user_lastname);
it was:
printf( __( 'My Account', 'wpdance' ));
And I tried also to get every thing using this code:
<?php global $current_user;
get_currentuserinfo();
echo 'Username: ' . $current_user->user_login . "\n";
echo 'User email: ' . $current_user->user_email . "\n";
echo 'User level: ' . $current_user->user_level . "\n";
echo 'User first name: ' . $current_user->user_firstname . "\n";
echo 'User last name: ' . $current_user->user_lastname . "\n";
echo 'User display name: ' . $current_user->display_name . "\n";
echo 'User ID: ' . $current_user->ID . "\n";
?>
But User first name: and User last name: were empty!
Does someone have any suggestion or idea?
Thank you in advanced!
The best way is to use wp_get_current_user() (no need of any global variable) and a conditional to be sure that the user is logged in:
if ( is_user_logged_in() ) {
$user_info = wp_get_current_user();
$user_last_name = $user_info->user_lastname;
printf( __( '%s', 'wpdance' ), $user_last_name );
}
Or with complete name:
if ( is_user_logged_in() ) {
$user_info = wp_get_current_user();
$user_complete_name = $user_info->user_firstname . ' ' . $user_info->user_lastname;
printf( __( '%s', 'wpdance' ), $user_complete_name );
}
References:
Get currentuserinfo firstname and lastname
Function Reference/wp get current user
Try calling
global $current_user;
get_currentuserinfo();
before
printf( __( '%s', 'wpdance' ),$current_user->user_lastname);
See https://codex.wordpress.org/Function_Reference/get_currentuserinfo#Examples
And are you sure the lastname is always set? Probably you can make sure $current_user works, if $current_user->ID at least returns a value.
And enable debugging in your wp_config.php might help as well to display all notices and errors:
define( 'WP_DEBUG', true );
See https://codex.wordpress.org/Debugging_in_WordPress