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
Related
I wonder if is there some method from WC_Memberships_User_Membership class to get a link to
cancel membership.
The way I'm getting the url takes me to the woocommerce my-account/memberships page, not to the real action of cancel subscription.
<?php
$memberships = wc_memberships_get_user_memberships();
if ( $memberships ) {
foreach( $memberships as $membership ) {
echo '<p>Plan: ' . $membership->plan->name . '</p>';
echo '<p>Since: ' . $membership->get_start_date('d-m-Y') . '</p>';
echo '<p>Until: ' . $membership->get_end_date('d-m-Y') . '</p>';
$url = 'https://poesicilina.com/?cancel_membership=' . $membership->id;
echo '<p>Cancel membership</p>';
}
}
<?php
function cancelbutton(){
$user_id = get_current_user_id();
$memberships = wc_memberships_get_user_active_memberships($user_id);
foreach ($memberships as $membership => $val) {
$url = $val->get_cancel_membership_url();
echo "<a href='$url'>Cancel</a>";
};
add_shortcode( 'cancelbutton', 'cancelbutton' ); ?>
Use shortcode [cancelbutton]
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');'
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 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
I'm trying to do this:
<?php
global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) {
echo '<span class="ciao">HELLO ' . $current_user->user_login . '</span>'; "\n";
echo 'Logout';
}
else {
echo 'Login';
}
?>
The problem is that href gives me back the empty value: wp_logout_url( home_url() );
When I use this WORDPRESS call outside the echo it works good, like this eg:
LOGOUT
How can i write this ??
echo 'Logout';
None of those was, but I appreciated.
This works well:
'text
echo 'Logout';
Needs to be changed to
echo 'Logout';
String started with single quote needs to be closed with single quote.
I think what you want is this:
<?php global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) { echo '<span class="ciao">HELLO ' . $current_user->user_login . '</span>'; "\n";
echo 'Logout'; } else { echo 'Login'; } ?>
The different is the string is closed before the result of wp_logout_url() is concatenated with it
It's because you've not ended you string
You'd want to use the following:
echo "<a href=\"" . wp_login_url( get_permalink() ); . "\"/>;
Here the correct option
<?php
global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) {
echo '<span class="ciao">HELLO ' . $current_user->user_login . '</span>\n';
echo 'Logout';
} else {
echo 'Login';
}
?>