Enqueuing jQuery in plug-ins

First of all, if your code (or the theme’s code) is unregistering jQuery and re-registering it from another location (Google), you need to add this somewhere: jQuery.noConflict(); This makes jQuery cooperate with other scripts (i.e. Prototype) that try to define the $ variable globally. WordPress has this line bundled into their version, Google does not. … Read more

Using wp_ajax and wp_ajax_nopriv hooks

It depends on what exactly you want to do. If you want to show just a short message, use the same callback for both. But if you need completely different objects and load different additional files depending on the log-in status, use separate callbacks. Basic example, probably not a recommended implementation. 🙂 class Ajax_Controller { … Read more

WordPress Script Loading/Unloading — wp_deregister_script(‘jquery’)

WordPress has essentially two groups of methods to handle scripts, both of which should be used: wp_register_script Registers a script in WordPress. It does not get called, it is just available for WordPress, if it is needed. wp_deregister_script is the exact opposite. It deletes the definitions made in wp_register_script, the script is no longer available … Read more

How to load a jQuery script last?

add_action(‘wp_footer’,’myscript_in_footer’); function myscript_in_footer(){ ?> <script type=”text/javascript”> jQuery(document).ready(function(){ jQuery(“div.domTip_tipBody”).mouseover(function(){ jQuery(“#yyy a.tippy_link”).css(“background-color”,”yellow”);}); jQuery(“div.domTip_tipBody”).mouseout(function(){ jQuery(“#yyy a.tippy_link”).css(“background-color”,”red”); }); }); </script> <?php }

Enqueue jQuery in WordPress

You’re on the right track, but missing one piece: add_action(‘wp_enqueue_scripts’, ‘my_scripts_method’); add_action allows you to run code at specific times during page loads / specific events. The above action tells WP to run your function when it is adding the scripts to the html head element. Your function in turn instructs WP to add the … Read more

wp_localize_script, variable is not defined in jquery

wp_localize_script should be called AFTER wp_enqueue_script: IMPORTANT!: wp_localize_script() MUST be called after the script it’s being attached to has been enqueued or registered. It doesn’t put the localized script in a queue for later scripts. Fix it in your function: function my_action_callback() { wp_enqueue_script(‘jscustom’); // I assume you registered it somewhere else wp_localize_script(‘jscustom’, ‘ajax_custom’, array( … Read more

How wp_enqueue_script works?

In simple case you need to enqueue script before header scripts are printed, which happens in wp_head hook. Basic approach would be this in functions.php of your theme: add_action(‘wp_enqueue_scripts’, ‘my_enqueue_scripts’); function my_enqueue_scripts() { wp_enqueue_script(‘jquery’); }

jQuery in WordPress – Why is it not working?

just to help a bit further … WordPress runs jQuery in ‘safe’ mode which means in WordPress you need to write code like this jQuery(document).ready(function() { and not like this $(document).ready(function() { But what the HTML5BP has done is added this funky bit of code (probably kindly to help Developers) // remap jQuery to $ … Read more