[Resolved] Shortcode in Menu Title

Home Forums Support Shortcode in Menu Title

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #118091
    Sebastien

    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

    #118110
    Tom
    Lead Developer
    Lead Developer

    I 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 🙂

    #118141
    Sebastien

    Thank 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

    #118231
    Tom
    Lead Developer
    Lead Developer

    You 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

    #119089
    Sebastien

    Hello 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.

    #119123
    Tom
    Lead Developer
    Lead Developer

    The 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.

    #119162
    Sebastien

    I 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!

    #119227
    Tom
    Lead Developer
    Lead Developer

    Awesome! Thanks for sharing your code 🙂

    #278469
    Roy

    I 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?

    #278480
    Tom
    Lead Developer
    Lead Developer

    The 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/

    #278612
    Roy

    sorry 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.

    #278765
    Tom
    Lead Developer
    Lead Developer

    In 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.

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.