Remove Google Jquery in Wordpress - php

I want to remove jQuery from Google CDN on my WordPress site. See here
I want to remove that, because I already have jquery script on my website. And also the jQuery from Google takes 1.1s too load. See here
I tried to add this script on the function.php
function my_init() {
if (!is_admin()) {
wp_deregister_script('jquery-js');
wp_register_script('jquery-js', 'https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js', false);
}
}
add_action('init', 'my_init');
But it's not working. So do you guys know how to remove the jQuery script from Google in Wordpress?

have you tried deregister without .js ?
like this:
wp_deregister_script('jquery');

Related

How to make the WP-Shopify only work on certain pages

I wondered if someone could help me with editing the wpshopify/wp-shopify.php in WordPress.
My goal is to make the Shopify plugin work on certain pages and not run on other pages.
So for example I would like the plugin to work on the [shop] page and not the [about us] page.
I have seen some "Plugin Organizers" but unfortunately I couldn't make it work.
Does anyone have the experience or know-how to get this done?
If you want to manage the styles and JavaScript of a plugin in WordPress so that on any page you just want to be loaded and used, I suggest using the following plugins.
WordPress Assets manager, dequeue scripts, dequeue styles
gonzales wp
Deactivate Plugins Per Page
But if you want to write a condition that you can manage, it means a specific plugin only when you want it to work like a specific page. For this you need to know the exact name of the plugin and then do it using a plugin management function.
I wrote this code and tested it, it worked properly.
In this code, I first check the post ID, whether page or post or any other type of post.
Then I disable all plugin styles and scripts and delete the class that is attached to the body
Finally, I remove a new element created in a class to display the plugin root.
Put this code in the functions.php file
function disble_shopwp_pages()
{
$post_id_array = array(
218, 433, 71, 2066, 825, 7, 2009, 2284, 420, 2402, 2394,
);
if (in_array(get_the_ID(), $post_id_array)) {
return true;
}
}
function remove_wpshopify()
{
if (disble_shopwp_pages()):
wp_dequeue_style('shopwp-styles-frontend-all');
wp_deregister_style('shopwp-styles-frontend-all');
wp_dequeue_script('shopwp-runtime');
wp_dequeue_script('shopwp-vendors-public');
wp_dequeue_script('shopwp-public');
endif;
}
add_action('wp_enqueue_scripts', 'remove_wpshopify', 9999);
function wpshopify_body_class($classes)
{
if (disble_shopwp_pages()) {
unset($classes[array_search('shopwp', $classes)]);
}
return $classes;
}
add_filter('body_class', 'wpshopify_body_class', 999, 2);
function remove_shopwp_root_elements()
{
if (disble_shopwp_pages()) {
echo '<script>
jQuery(document).ready(function () {
jQuery("#shopwp-root").remove();
});
</script>';
}
}
add_action('wp_footer', 'remove_shopwp_root_elements');

Wordpress - Create Page with custom CSS and JS only

I am creating a Wordpress plugin that adds a page that embeds a React app.
On plugin activation, a post is inserted with wp_insert_post() function, and then the load_app_template() function is triggered to set the post template.
This template contains wp_head() in the head section and my React app.
The problem is that some other plugins are loading some JS and CSS in the head that are disturbing the React app behavior. I am trying to remove all JS and CSS given by Wordpress & other plugins and add my own.
If I remove wp_head() from the post template, then I cannot register styles and scripts with my plugin through wp_register_script.
How to remove all JS and CSS from Wordpress and plugins and add my own from my plugin ?
There could be two ways :
Remove wp_head() and manually add links to my JS and CSS in the HTML template. I can do that for Jquery and Bootstrap but I don't know how to put links of JS files from my plugin directory.
Keep wp_head(), deregister all CSS and JS and add my own afterwards with Wordpress hooks. I am not able to remove some Javascript from other plugins with this solution. I tried deregistering all following this answer.
Thank you !
Try to remove unnecessary scripts and styles using wordpress hooks
(this might help you figure out how to do so: https://wordpress.stackexchange.com/questions/233140/how-can-i-get-a-list-of-all-enqueued-scripts-and-styles) if your template is used on page.
You can remove unused CSS/JS from that page. Put this code on your function.php
function remove_unused_css_js() {
// remove only from that page
if(get_the_ID() == 'Your Page ID'){
wp_dequeue_style('css-id'); // remove css
wp_dequeue_script('js-id'); // remove js
}
}
add_action( 'wp_print_styles', 'remove_unused_css_js', 100 );
Hope this will help.

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);

Categories