- This topic has 11 replies, 3 voices, and was last updated 7 years, 7 months ago by Tom.
-
AuthorPosts
-
July 1, 2015 at 8:18 pm #118091Sebastien
Hello,
I would like one of my menu item to display “sign in/sign up” when not connected and display the user name when connected.
Can we add shortcode to the menu items? Another method is to use two different menus which will be displayed based on the connection status. All of this requires a modification in the theme, as far as I understand.
Thank you for your advice.
Sebastien
July 2, 2015 at 12:27 am #118110TomLead DeveloperLead DeveloperI actually do this on this site – but it involves using a function:
add_filter( 'wp_nav_menu_objects', 'generate_navigation_shortcode' ); function generate_navigation_shortcode( $menu_items ) { foreach ( $menu_items as $menu_item ) { if ( '#download_link#' == $menu_item->url ) { global $shortcode_tags; if ( isset( $shortcode_tags['download_link'] ) ) { // Or do_shortcode(), if you must. $menu_item->url = call_user_func( $shortcode_tags['download_link'] ); } } } return $menu_items; }
With the above, I can type #download_link# into the menu item URL, and it will display the
download_link
shortcode.Let me know if you need more info π
July 2, 2015 at 4:20 am #118141SebastienThank you Tom.
I am new to WordPress and noob in PHP. I apologize if the questions are obvious or if you did answer it previously π
Yesterday I did some research and came up with this code:
add_shortcode( 'current-username' , 'username_on_menu' ); function username_on_menu(){ $user = wp_get_current_user(); $HTMLEntityHexadecimal = '👤'; // bust in silhouette http://graphemica.com/%F0%9F%91%A4 if (empty($user->display_name)) { $username_on_menu_label = "Sign In"; } else { $username_on_menu_label = html_entity_decode($HTMLEntityHexadecimal)." ".$user->display_name; } return $username_on_menu_label; }
Then I just need to use [current-username] shortcode in a Menu label. It displays “Sign In” if not connected and the user name if connected.
Regarding your answer, where is the best place to save this function? (I did it in the code of a plugin, not sure it’s the best).
Thank you your help.
Sebastien
July 2, 2015 at 11:56 am #118231TomLead DeveloperLead DeveloperYou can see how to add the PHP using this article: http://generatepress.com/knowledgebase/adding-php-functions/
Then you would replace download_link in my function to your shortcode name: current-username
July 6, 2015 at 7:55 pm #119089SebastienHello Tom,
Sorry for the delay, I am back on this topic.
First of all, thank you for your suggestions. I love Pluginception π This way I can safely upgrade GeneratePress without impacting my mods… hopefully.
Now back to your original suggestion, I am afraid I don’t understand it.
What is
download_link
?Where are you testing if the user is logged in?
Also I did other research and I found the following piece of code:
function my_wp_nav_menu_args( $args = '' ) { if ($args['theme_location'] == 'secondary-navigation') { if( is_user_logged_in()) { $args['menu'] = 'NameSecondaryMenuForConnectedUsers'; } else { $args['menu'] = 'NameSecondaryMenuForVisitors'; } }else{ $args['menu'] = 'NameMainMenu'; } return $args; } add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
But I can’t make it work, it always return the main menu in my secondary nav. I think I don’t have the right test to identify the secondary navigation (
$args['theme_location'] == 'secondary-navigation'
). Would you have any idea?Thank you for your help.
July 7, 2015 at 12:17 am #119123TomLead DeveloperLead DeveloperThe testing comes from your function – my function only allows you to use your shortcode in “Appearance > Menus”.
For example:
add_filter( 'wp_nav_menu_objects', 'generate_navigation_shortcode' ); function generate_navigation_shortcode( $menu_items ) { foreach ( $menu_items as $menu_item ) { if ( '#user_login#' == $menu_item->url ) { global $shortcode_tags; if ( isset( $shortcode_tags['current-username'] ) ) { // Or do_shortcode(), if you must. $menu_item->url = call_user_func( $shortcode_tags['current-username'] ); } } } return $menu_items; }
Now if you add
#user_login#
as the URL in “Appearance > Menus”, it should use your current-username shortcode.July 7, 2015 at 4:22 am #119162SebastienI see. Thank you for the clarification. So I am now able to display elements in the menu that can change based on a context (user logged in…).
I was also able to to combine this with a menu structure which can also change based on the context. In my previous post I was struggling with the test of the location, but I was able to fix it. For those interested, my code is the following:
function my_wp_nav_menu_args( $args = '' ) { if ($args['theme_location'] == 'secondary') { if( is_user_logged_in()) { $args['menu'] = 'SecondaryMenuNameForConnectedUser'; } else { $args['menu'] = 'SecondaryMenuNameForNonConnectedUser'; } } return $args; } add_filter( 'wp_nav_menu_args', 'my_wp_nav_menu_args' );
Thank you Tom!
July 7, 2015 at 9:05 am #119227TomLead DeveloperLead DeveloperAwesome! Thanks for sharing your code π
February 13, 2017 at 10:00 pm #278469RoyI tried to using this code, but the short code: #user_login# does not get replaced in the URL associated with the menu item. The callback function does get executed and $args[‘menu’] does get set properly based on the is_user_logged_in() function return. Are the keywords: SecondaryMenuNameForNonConnectedUser and SecondaryMenuNameForConnectedUser the correct names for the menu elements?
February 13, 2017 at 11:03 pm #278480TomLead DeveloperLead DeveloperThe code above with $args[‘menu’] looks like it’s meant for the secondary menu, and set to display a different menu item for logged in and out users.
For displaying shortcodes, you should use the code I posted above: https://generatepress.com/forums/topic/shortcode-in-menu-title/#post-119123
Alternatively, this plugin might help: https://en-ca.wordpress.org/plugins/shortcode-in-menus/
February 14, 2017 at 6:50 am #278612Roysorry I wasn’t clear. I did include the code to add the filter in functions.php. And they clearly did get execute correctly since the callback function does get executed. I put a few echos in the code and it does display that $args[‘menu’] is correctly set. I am putting the shortcode in the URL of the menu item but it does not get replace with the login/logout URL.
February 14, 2017 at 11:47 am #278765TomLead DeveloperLead DeveloperIn my example above you would put
#user_login#
as your URL – not the shortcode.The function then looks for
#user_login#
and converts it to the shortcode. -
AuthorPosts
- You must be logged in to reply to this topic.