3
27/11/2024 4:44 рдкреВрд░реНрд╡рд╛рд╣реНрди
рд╡рд┐рд╖рдп рдкреНрд░рд╛рд░рдВрднрдХрд░реНрддрд╛
Hi, I have a WooCommerce site and in the menu I want to show the username - instread of the My Profile / My Account for logged-in users.┬а
How can I do it?┬а
Maybe some code snippet in functions.php file?
рдзрдиреНрдпрд╡рд╛рдж
2 рдЙрддреНрддрд░
3
27/11/2024 4:51 рдкреВрд░реНрд╡рд╛рд╣реНрди
In your functions.php (in your Child theme) add this code:
/* Use shortcode: [current_user_display_name] for menu and My account page */ function display_current_user_display_name() { // Get current user $user = wp_get_current_user(); // Check if the user is logged in if ( $user->exists() ) { return $user->display_name; } // Return a fallback for non-logged-in users return 'Profile'; } add_shortcode( 'current_user_display_name', 'display_current_user_display_name' );
your shortcode will be: [current_user_display_name]
Now you have to add the option to show shortodes in your WordPress menu.┬а
So, in the functions.php again add this line of code:
add_filter('wp_nav_menu_items', 'do_shortcode');
Now you can use everywhere in your site the shortcode: [current_user_display_name]
cheers ЁЯЩВ
2
27/11/2024 4:56 рдкреВрд░реНрд╡рд╛рд╣реНрди
If you are using Max Mega Menu, you do not need to add the option for adding the shortcode for your WP menu. Just use the Replacement (HTML) option.
Read THIS ARTICLE to learn more.