Wordpress Add javascript:void(0); to menu link item?
Problem: I want to add javascript:void(0); to menu link item instead of # but it is not accepting any JavaScript. Now i am thing to replace # with javascript:void(0); using filters, but could not find the appropriate filter for the task.
Solution:
add_filter('walker_nav_menu_start_el', 'sd_activate_js_void_0', 999);
/**
* Replace # with js in menu
* @param string $menu_item item HTML
* @return string item HTML
*/
function sd_activate_js_void_0($menu_item) {
if (strpos($menu_item, 'href="#"') !== false) {
$menu_item = str_replace('href="#"', 'href="javascript:void(0);"', $menu_item);
}
return $menu_item;
}