Javascript in WordPress Menus URL


By default wordpress don’t allow you to put JavaScript function in its menu URL

There are few steps to insert Javascript in WordPress Menus URL with jQuery without editing wordpress core.


Javascript in WordPress Menus URL

many users need add onclick event on wordpress menu and wordpress does not recognize javascript:void(0) or any other javascript function.
here is the step to put javascript in wp menu..

Step 1. Put # in your wordpress menu url  in which you want the JavaScript Function.

Javascript in WordPress Menus URL

Step 2. Track this menu url (li ID) link from source code or Firebug or Developer Tool.

in this case li id is menu-item-1002 .

this id is auto generated by wordpress and always unique. So the every wordpress menu url id is unique

javascript in wordpress

Step 3. Place the following code before the end of head tag (open header.php find </head>)  of current theme and put you menu id in highlighted area.
the wordpress menu url id is always unique for every wordpress menu.so we can use javascript easy.

Javascript in WordPress Menus URL

put your javascript code in section
//Your javascript Code here.
in this way you can execute any javascript code on wordpress menu
Enjoy.

Here Is the script that you need to add in header.php of your wordpress current theme, before the end of head tag (</head>).

[js]
jQuery.noConflict();
jQuery(document).ready(function(){
var menuID = jQuery(‘#your menu id’);

findA = menuID.find(‘a’);

findA.click(function(event){
// Your Javascript Code Here
});
});
[/js]

For Plugin see: Updated Post [ onclick on Wordpress Menu- Plugin  ]

Download Plugin Version

, ,