Reece Reece - 11 months ago 69
PHP Question

Adding jQuery in WordPress functions file

I have been having a problem where my contact form 7 plugin has been interfering with my jQuery for slick slider. I think its because the contact form 7 old jQuery is overriding mine. I originally added the query with in the head. I think if I do it through the functions file it might fix this. I found this code which solved my problem but I'm not sure what its doing exactly.

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://", false, null);

Answer Source

Basically, what the snippet is doing is changing the already registered and enqueued jquery with the newer file of your choice (in this case, loading jquery from google cdn).

Lets break it down:

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);

If you're not logged in as admin, then you attach the function "my_jquery_enqueue" to the hook "wp_enqueue_scripts" with priority 11.

The function my_jquery_enqueue() then removes the registered jquery script (probably registered by another plugin with the handler jquery), registeres the script you want to have there and finally enqueues it.

All this happens before WP generates the page (that's why you can swap out the file easily). Have a read through the following links to understand it better:

  1. Plugin API, hooks, Actions and Filters
  2. wp_enqueue_scripts
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download