Wordpress: Load only one jquery script on website - php

Just about to launch a WordPress site but have noticed that it's currently loading in two jquery files, one in wp-includes and one from my header.php, is there a way to make wordpress load the wp-include one on the front end? Done quite a bit of search and have the only mention of this seems to include the following code, but I can't find any documentation about it, any ideas?
<?php wp_enqueue_script("jquery"); ?>

As of WordPress 3.3, this is the best way to do it, using the proper hook:
if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11);
function my_jquery_enqueue() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
}

you need to include the following code before <?php wp_head(); ?> in your header.php
<?php wp_enqueue_script("jquery"); ?>
and you can remove other jquery includes from header.php

In addition to what Aram Mkrtchyan said, you can enqueue your scripts also using wp_enqueue_script().
<?php
wp_enqueue_script('jquery');
wp_enqueue_script('your_script', "path/to/your/script.js" , array('jquery'));
?>
The third argument to wp_enqueue_script() tells WordPress that your_script is dependent on jquery, so load it only after jquery has loaded.

Actually, you need to use admin_init hook to make it work in admin section:
function jquery_for_admin() {
wp_enqueue_script('jquery');
return;
}
add_action('admin_init', 'jquery_for_admin');

Related

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_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
}
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:
Plugin API, hooks, Actions and Filters
wp_enqueue_scripts

Waypoints.js with wordpress

I'm developing a wordpress site and I need to trigger the user scroll in order to fire different events and hide/show some images, so Waypoints.js is perfect for it.
However I've tried different attempts to make it work with no results. I add it as a function on functions.php file, like this:
function waypoints_method() {
wp_deregister_script('jquery');
wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js", false, null);
wp_enqueue_script('jquery');
wp_enqueue_script('waypoints', get_stylesheet_directory_uri() . '/vendor/waypoints/lib/jquery.waypoints.min.js');
}
add_action( 'wp_enqueue_scripts', 'waypoints_method' );
And then in the javascript:
jQuery(document).ready(function($) {
$('.waypoint').waypoint(function() {
alert('You have scrolled to my waypoint.');
});
}
The only thing I get it is to console.log when resizing the browser. So, what I need to do in order to make it work? Or, there is any alternative to Waypoints.js that I could use?
Thanks!
Waypoints is using $ to access jQuery, but with WordPress you need to refer to jQuery as jQuery.
I found that I had to use the no framework version of Waypoints to get it working with WordPress.

Jquery loading twice in Wordpress

I have found out JQuery is loading twice on my Wordpress website and is causing some plugins to break. I have done some research and think I need to remove some JQuery code from the header.php file, and then insert some new JQuery code into the functions.php file. Trouble is I am not exactly sure what code to remove/add. Can anyone help me stop JQuery running twice?
To remove the default WordPress jQuery file you can use the following:
function remove_jquery(){
wp_deregister_script('jquery');
wp_dequeue_script('jquery');
}
add_action('wp_enqueue_scripts','remove_jquery');
That should deregister and dequeue the native jQuery file and keep the one that you have in your header.php file.
function dequeue_script() {
wp_dequeue_script( 'jquery' );
}
add_action( 'wp_print_scripts', 'dequeue_script', 100);

How to load css in the footer in function.php with wordpress theme

I was trying to move the load of my style.css in the footer of my wordpress theme.
I have already done something similar with 2 js files and it works fine, they load in the footer:
function my_init() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://www.studionews24.com/wp-content/themes/network/js/jquery.min.js', false, '1.3.2', true);
wp_enqueue_script('jquery');
// load a JS file from my theme: js/theme.js
wp_enqueue_script('my_script', 'http://www.studionews24.com/wp-content/themes/network/js/menu-resp.js', array('jquery'), '1.0', true);
}
}
add_action('init', 'my_init');
Now i was trying to move in the footer also the style.css, and keep only some inline css rules in the <head> tag. I have tried wp_enqueue_style but it seems doesn't work well for me.
Someone could help me for find a smart solution?
Try this:
add_action('wp_footer', 'my_init');
or since you have add parameter true in your script callback function:
add_action('wp_enqueue_scripts', 'my_init');
You may go here http://wordpress.stackexchange.com if you need help around wordpress programming.
replace
add_action('init', 'my_init');
with
add_action('wp_footer', 'my_init', 100);

Include script in template_redirect page

I'm writting a plugin and on this must generate a suggest page, the suggest I generate with template_redirect, like so:
add_action('template_redirect', 'suggest_page');
function suggest_page() {
if (!preg_match('/suggest\.php$/', $_SERVER['REQUEST_URI']))
return;
...
}
I'm wanting to include jQuery on this page, i konw that can use wp_head(); just before </head> and then use wp_enqueue_script('jquery'); but the wp_head(); includes a lot of stuff that i don't need.
so there's a way to add a wordpress script to a page without include all head stuff of wordpress ?
from the template_redirect codex, loading a template from a plugin. have you tried this

Categories