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'];
Related
I am trying to display a grid list of registered users under specific roles such as subscriber, editor, etc with custom metadata. I've started with this below code but it only displays the avatar and the name when need to display some more data of each user such as website link, description, email.
Here is the code snippet I am using-
function wpb_recently_registered_users() {
global $wpdb;
$recentusers = '<ul class="recently-user">';
$usernames = $wpdb->get_results("SELECT user_nicename, user_url, user_email FROM $wpdb->users ORDER BY ID DESC LIMIT 5");
foreach ($usernames as $username) {
if (!$username->user_url) :
$recentusers .= '<li>' . get_avatar($username->user_email, 45) . '' . $username->user_nicename . "</li>";
else :
$recentusers .= '<li>' . get_avatar($username->user_email, 45) . '' . $username->user_nicename . "</li>";
endif;
}
$recentusers .= '</ul>';
return $recentusers;
}
add_shortcode('wpb_newusers', 'wpb_recently_registered_users');
I need to get these user meta key data also -
mepr_email, mepr_your_bio, mepr_youtube_channel
but not sure how to integrate with the above code snippets. Can anyone help me find a better solution, please?
So I am using this code snippet and getting all the required data but I am not sure how to run this as a loop so as soon as a new user register it will get the data like posts query? Sorry I have limited knowledge in this.
'function user_data_code(){
$blogusers = get_users( array( 'search' => 'admin' ) );
// Array of WP_User objects.
foreach ( $blogusers as $user ) {
echo '<span>' . get_avatar($user->user_email, 45) . '</span> <br>';
echo '<span>' . esc_html( $user->user_email ) . '</span> <br>';
echo '<span>' . esc_html( $user->display_name ) . '</span> <br>';
echo '<span>' . esc_html( $user->first_name ) . '</span> <br>';
echo '<span>' . esc_html( $user->mepr_bio ) . '</span> <br>';
echo '<span>' . esc_html( $user->mepr_website_link) . '</span>';
}
}
add_shortcode('wpb_newusers', 'user_data_code');'
Is it possible to use php variable in menu URL, that I defined in function.php?
This is actually not working:
For example, in variable $path I store username of every user so they can visit their own profile.
You can use a hook and add the PHP programmatically.
add_filter( 'wp_get_nav_menu_items','nav_items', 11, 3 );
function nav_items( $items, $menu, $args ) {
if( is_admin() )
return $items;
foreach( $items as $item ) {
if( 'profil' == $item->post_title ) {
$current_user = wp_get_current_user();
$username = $current_user->user_login;
$item->url .= '/' . $username;
}
}
return $items;
}
I found this code from another answer on this community.
In Wordpress You Can Do Direct Like this for get the current user details:
<?php
$current_user = wp_get_current_user();
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
?>
Reference: https://codex.wordpress.org/Function_Reference/wp_get_current_user
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 );
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