Show shortcode output in menu

[ad_1]

Hallo @mabufoysal,

you can do this via PHP. But if you want to do that, you can try to use this plugin:
https://projectdmc.org/plugins/megamenu/

PHP Example without Plugin:

if ( ! has_filter( 'wp_nav_menu', 'do_shortcode' ) ) {
    add_filter( 'wp_nav_menu', 'shortcode_unautop' );
    add_filter( 'wp_nav_menu', 'do_shortcode', 11 );
}

Documentation: https://developer.projectdmc.org/reference/functions/wp_nav_menu/

Greetings
Benjamin

Thanks @benjamin_zekavica .

Sorry, I couldn’t express myself properly. I created a Shortcode named country_name and it is working perfectly in Posts.

But I would like to use this shortcode with Menu like the screenshot. May be you are clear after check the screenshot.

This Shortcode will show a Country name.

How can I use the Shortcode with the menu like the screenshot ?

Hallo @mabufoysal,

yes use this Filter wp_nav_menu or you can write a WP Menu Walker that allows you to use Shortcodes inside the Menu. Alternative you can use the Plugin.

More Details you can find here:
https://developer.projectdmc.org/reference/classes/walker/
https://developer.projectdmc.org/reference/functions/do_shortcode/

Thanks @benjamin_zekavica . How to use wp_nav_menu filter ? I am using like below.

add_shortcode('country_name', 'country_name_shortcode');

function country_name_shortcode($country_name) {

    return $country_name;

}

add_filter( 'wp_nav_menu', 'country_name_shortcode' );

Just use :

if ( ! has_filter( 'wp_nav_menu', 'do_shortcode' ) ) {
add_filter( 'wp_nav_menu', 'shortcode_unautop' );
add_filter( 'wp_nav_menu', 'do_shortcode', 11 );
}

in addition to your shortcode coding. With the above 4 lines you activate the use of shortcode in the menu. Add them there where you add your shortcode-code.

Thanks @threadi and @benjamin_zekavica. Your solution works like Magic. Thanks.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer