how to insert shortcode into wordpress menu
Asked Answered
U

4

16

I have made a menu item with this code. The menu item shows up but the shortcode output is not there. Is there something I can add or a different method that will do this. I have added also in hopes this might help.

add_filter('wp_nav_items', 'do_shortcode', 7);

Or maybe someone knows this is not possible and can tell me.

/* Nav Menu */
function add_profile_link_to_nav(){ 
 if ( is_user_logged_in() ) { ?> 

<ul> 
  <li class="menu-item"id="one"> <a href="http://example.com/members/">All  Members</a>
  <ul class="sub-menu"> 
      <li class="menu-item"><?php echo custom_execute_shortcode(); ?> </li>
  </ul> 
 </li>
</ul>    <!--end menu--->
<?php } 
}
add_action( "wp_nav_items","add_profile_link_to_nav" );

function custom_execute_shortcode() {
$myfunction= '[my shortcode"]';
$myfunction_parsed = do_shortcode($myfunction);
return $myfunction_parsed;
}

Thanks

Utoaztecan answered 9/7, 2012 at 21:8 Comment(0)
S
17

You can't use shortcodes directly in the menu URL on the menu page, because the brackets get stripped out. But you can use placeholders like this: #profile_link#.

With the following code in functions.php, you can create a custom menu item with the URL #profile_link#, and it will replace that with your shortcode.

/**
 * Filters all menu item URLs for a #placeholder#.
 *
 * @param WP_Post[] $menu_items All of the nave menu items, sorted for display.
 *
 * @return WP_Post[] The menu items with any placeholders properly filled in.
 */
function my_dynamic_menu_items( $menu_items ) {

    // A list of placeholders to replace.
    // You can add more placeholders to the list as needed.
    $placeholders = array(
        '#profile_link#' => array(
            'shortcode' => 'my_shortcode',
            'atts' => array(), // Shortcode attributes.
            'content' => '', // Content for the shortcode.
        ),
    );

    foreach ( $menu_items as $menu_item ) {

        if ( isset( $placeholders[ $menu_item->url ] ) ) {

            global $shortcode_tags;

            $placeholder = $placeholders[ $menu_item->url ];

            if ( isset( $shortcode_tags[ $placeholder['shortcode'] ] ) ) {

                $menu_item->url = call_user_func( 
                    $shortcode_tags[ $placeholder['shortcode'] ]
                    , $placeholder['atts']
                    , $placeholder['content']
                    , $placeholder['shortcode']
                );
            }
        }
    }

    return $menu_items;
}
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );

You just need to set 'shortcode' in the $placeholders array, and optionally 'atts' and 'content'.

For example, if your shortcode is like this:

[example id="5" other="test"]Shortcode content[/example]

You would update:

'#placeholder#' => array(
    'shortcode' => 'example';
    'atts' => array( 'id' => '5', 'other' => 'test' );
    'content' => 'Shortcode content';
),

Note that I don't use do_shortcode() because it is a resource intensive function and isn't the right tool for the job in this case.

Shumway answered 7/11, 2013 at 21:43 Comment(8)
Wow that actually worked, been looking a long long time for this. Many Thanks J.DUtoaztecan
@Shumway - Hi, I am trying to add this shortcode into your code, but it wont work ( [ip:GB]020 7267 5222[/ip] ) . Can you help me pleaseBrause
@CabLondon — For that you can use do_shortcode( '[ip:GB]020 7267 5222[/ip]' );, or preferably call_user_func( $shortcode_tags['my_shortcode'], array( ':GB', '020 7267 5222', 'ip' );.Shumway
@Shumway hi, how can i add my shortcode to your updated answer, this is my shortcode [easy-pricing-table id="4227"] Can you help me, please ?Roxy
@Roxy Change the line $atts = array() to $atts = array( 'id' => 4227 );. I'll update the answer to make this more clear.Shumway
@Shumway thank your for answer, my shortcode is [easy-pricing-table id="4227"] and i changed your code to $shortcode = 'easy-pricing-table'; $atts = array( 'id' => 4227 ); and I created some custom link which URL is #profile_link# for test, but it doesn't work. There aren't any error or warning but nothing happensRoxy
Btw, this is my question link i asked 2 days ago and stated bounty 50 rep but answers don't working. #39613060Roxy
I don't think this solution works with more recent WP versions. I ended up having to use the Short Codes in Menus plugin.Balneal
P
35

@Tim This code will work

put it in functions.php file

add_filter('wp_nav_menu_items', 'do_shortcode');
Polygnotus answered 13/7, 2013 at 11:10 Comment(5)
@Tim you can add any optional args for that code, I only give you the basic onePolygnotus
Thank you usama I appreciate the responceUtoaztecan
Note to anyone coming here from Google, this totally the menus in modern versions of Wordpress.Funk
@JohnSmith This totally what?Enmesh
Best solution so farMerman
S
17

You can't use shortcodes directly in the menu URL on the menu page, because the brackets get stripped out. But you can use placeholders like this: #profile_link#.

With the following code in functions.php, you can create a custom menu item with the URL #profile_link#, and it will replace that with your shortcode.

/**
 * Filters all menu item URLs for a #placeholder#.
 *
 * @param WP_Post[] $menu_items All of the nave menu items, sorted for display.
 *
 * @return WP_Post[] The menu items with any placeholders properly filled in.
 */
function my_dynamic_menu_items( $menu_items ) {

    // A list of placeholders to replace.
    // You can add more placeholders to the list as needed.
    $placeholders = array(
        '#profile_link#' => array(
            'shortcode' => 'my_shortcode',
            'atts' => array(), // Shortcode attributes.
            'content' => '', // Content for the shortcode.
        ),
    );

    foreach ( $menu_items as $menu_item ) {

        if ( isset( $placeholders[ $menu_item->url ] ) ) {

            global $shortcode_tags;

            $placeholder = $placeholders[ $menu_item->url ];

            if ( isset( $shortcode_tags[ $placeholder['shortcode'] ] ) ) {

                $menu_item->url = call_user_func( 
                    $shortcode_tags[ $placeholder['shortcode'] ]
                    , $placeholder['atts']
                    , $placeholder['content']
                    , $placeholder['shortcode']
                );
            }
        }
    }

    return $menu_items;
}
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );

You just need to set 'shortcode' in the $placeholders array, and optionally 'atts' and 'content'.

For example, if your shortcode is like this:

[example id="5" other="test"]Shortcode content[/example]

You would update:

'#placeholder#' => array(
    'shortcode' => 'example';
    'atts' => array( 'id' => '5', 'other' => 'test' );
    'content' => 'Shortcode content';
),

Note that I don't use do_shortcode() because it is a resource intensive function and isn't the right tool for the job in this case.

Shumway answered 7/11, 2013 at 21:43 Comment(8)
Wow that actually worked, been looking a long long time for this. Many Thanks J.DUtoaztecan
@Shumway - Hi, I am trying to add this shortcode into your code, but it wont work ( [ip:GB]020 7267 5222[/ip] ) . Can you help me pleaseBrause
@CabLondon — For that you can use do_shortcode( '[ip:GB]020 7267 5222[/ip]' );, or preferably call_user_func( $shortcode_tags['my_shortcode'], array( ':GB', '020 7267 5222', 'ip' );.Shumway
@Shumway hi, how can i add my shortcode to your updated answer, this is my shortcode [easy-pricing-table id="4227"] Can you help me, please ?Roxy
@Roxy Change the line $atts = array() to $atts = array( 'id' => 4227 );. I'll update the answer to make this more clear.Shumway
@Shumway thank your for answer, my shortcode is [easy-pricing-table id="4227"] and i changed your code to $shortcode = 'easy-pricing-table'; $atts = array( 'id' => 4227 ); and I created some custom link which URL is #profile_link# for test, but it doesn't work. There aren't any error or warning but nothing happensRoxy
Btw, this is my question link i asked 2 days ago and stated bounty 50 rep but answers don't working. #39613060Roxy
I don't think this solution works with more recent WP versions. I ended up having to use the Short Codes in Menus plugin.Balneal
E
2

Enable description on the menu page, paste to the description textarea of the link your shortcode, in functions.php add next code:

add_filter('walker_nav_menu_start_el', function($item_output, $item) {
    if (!is_object($item) || !isset($item->object)) {
        return $item_output;
    }

    if ($item->ID === 829) {
        $item_output = do_shortcode($item->description);
    }

    return $item_output;
}, 20, 2);
Evvie answered 20/7, 2020 at 13:10 Comment(1)
I assume this will only work if your theme supports showing the description in the menu?Balneal
A
0

Open functions.php and insert the provided code:

// Custom Walker Function
class Shortcode_Menu_Walker extends Walker_Nav_Menu {
    function start_el(&$output, $item, $depth = 0, $args = NULL, $id = 0) {
        $output .= apply_filters('walker_nav_menu_start_el', do_shortcode($item->title), $item, $depth, $args);
    }
}

// Apply Custom Walker to Menu
add_filter('wp_nav_menu_args', 'custom_menu_walker');
function custom_menu_walker($args) {
    if (isset($args['walker']) && $args['walker'] === false) {
        $args['walker'] = new Shortcode_Menu_Walker();
    }
    return $args;
}
Aeolipile answered 9/7, 2012 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.