lexvw
Forum Replies Created
-
Forum: Plugins
In reply to: [Max Mega Menu] Shortcode in submenuHi Tom,
Thank you for your fast response. Your answer set me in the right direction and I got it working. I went on a deep dive search again and did this:function eta_enqueue_menu_assets($items, $args) {
$styles_dir = plugin_dir_url(__FILE__) . '../assets/css/';
$scripts_dir = plugin_dir_url(__FILE__) . '../assets/js/';
// Check if the menu contains any of the shortcodes
if (strpos($items, '[eta_chatbot]') !== false) {
eta_enqueue_dynamic_css('chatbot_css', $styles_dir . 'chatbot.css');
wp_enqueue_script('chatbot_js', $scripts_dir . 'chatbot.js', array('jquery'), null, true);
}
if (strpos($items, '[eta_dropdown]') !== false) {
wp_enqueue_style('dropdown_css', $styles_dir . 'dropdown.css');
wp_enqueue_script('dropdown_js', $scripts_dir . 'dropdown.js', array('jquery'), null, true);
}
if (strpos($items, '[eta_license_plate_checker]') !== false) {
wp_enqueue_style('license_plate_css', $styles_dir . 'license-plate.css');
wp_enqueue_script('license_plate_js', $scripts_dir . 'license-plate.js', array('jquery'), null, true);
}
return $items;
}function eta_process_menu_shortcodes($items, $args) {
return do_shortcode($items);
}Then I used those function in my init method like this:
add_filter('wp_nav_menu_items', 'eta_enqueue_menu_assets', 10, 2);
add_filter('wp_nav_menu_items', 'eta_process_menu_shortcodes', 10, 2);The ‘wp_nav_menu_items’ filters the HTML list contents for menus so we can use the ‘eta_enqueue_menu_assets’ to check if any specific shortcodes (like
[eta_chatbot]
or[eta_license_plate_checker]
) are present in the menu items and then conditionally enqueues the necessary CSS and JavaScript files.
Now i dont use the $args but i did include them here, this is an object containing additional arguments used by thewp_nav_menu()
function (like the menu location, theme location, and other settings). It’s useful if you need context, like whether you’re dealing with a specific menu location, but you don’t always need it.
And by making a custom function (eta_process_menu_shortcodes) to, for now, only call ‘do_shortcode’ ensures that I can later add some extra functionality I need.
Now I am not sure if this is the correct/best way to do this. But it works.Forum: Plugins
In reply to: [Max Mega Menu] Shortcode in submenuExtra information: I loaded the shortcodes in via using the custom HTML widget. And by adding this filter to my main php file.
add_filter('wp_nav_menu_items', 'do_shortcode')