Remove ajax.googleapis.com from my Wordpress site - php

Any reference to googleapis will block my website in China. How can I remove these references?
Here are the results of my grep search for google api reference:
./codesearch.php:$command = "grep -ri 'ajax.googleapis.com' ./*"; ./wp-content/cache/all/privacy-policy/index.html: ./wp-content/cache/all/privacy-policy/index.html: ./wp-content/cache/all/contact/index.html: ./wp-content/cache/all/contact/index.html: ./wp-content/cache/all/index.html: ./wp-content/cache/all/index.html: ./wp-content/cache/all/author/boomerlanglearning/index.html: ./wp-content/cache/all/author/boomerlanglearning/index.html: ./wp-content/cache/all/my-account/index.html: ./wp-content/cache/all/my-account/index.html: ./wp-content/cache/all/my-account/lost-password/index.html: ./wp-content/cache/all/my-account/lost-password/index.html: ./wp-content/cache/all/terms-and-conditions/index.html: ./wp-content/cache/all/terms-and-conditions/index.html: ./wp-content/plugins/woocommerce-checkout-manager/woocommerce-checkout-manager.php: // wp_enqueue_script( 'jquery-lib', '//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js' ); ./wp-content/plugins/booster-plus-for-woocommerce/includes/settings/wcj-settings-general.php: 'default' => '//ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css', ./wp-content/plugins/booster-plus-for-woocommerce/includes/classes/class-wcj-scripts.php: $datepicker_css_path = '//ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css'; ./wp-content/plugins/woocommerce-bookings/woocommerce-bookings.php: wp_enqueue_style( 'jquery-ui-style', '//ajax.googleapis.com/ajax/libs/jqueryui/' . $jquery_version . '/themes/smoothness/jquery-ui.min.css' ); ./wp-includes/script-loader.php: $scripts->add( 'prototype', 'https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js', array(), '1.7.1'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-root', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/scriptaculous.js', array('prototype'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-builder', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/builder.js', array('scriptaculous-root'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-dragdrop', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/dragdrop.js', array('scriptaculous-builder', 'scriptaculous-effects'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-effects', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/effects.js', array('scriptaculous-root'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-slider', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/slider.js', array('scriptaculous-effects'), '1.9.0'); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-sound', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/sound.js', array( 'scriptaculous-root' ), '1.9.0' ); ./wp-includes/script-loader.php: $scripts->add( 'scriptaculous-controls', 'https://ajax.googleapis.com/ajax/libs/scriptaculous/1.9.0/controls.js', array('scriptaculous-root'), '1.9.0'); Grep job over.
Here is the code I added to the functions.php file of my child theme:
function modify_jquery() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js', false, '1.9.0');
wp_enqueue_script('jquery');
}
}
add_action('init', 'modify_jquery');

wp_enqueue_scripts should do the job for the non-admin front end as you require.
add_action('wp_enqueue_scripts', 'modify_jquery', 99);
function modify_jquery(){
wp_dequeue_script( 'jquery');
wp_deregister_script( 'jquery');
wp_register_script('jquery', 'https://ajax.aspnetcdn.com/ajax/jquery/jquery-1.9.0.js', false, '1.9.0');
wp_enqueue_script('jquery');
// repeat for your other googleapi scripts
}
Above is modified from your code - but in my case script dereg/reg and "do late" priority of 99 was unnecessary (see below).
Your grep output includes scripts which may have dependencies e.g. 'jquery' AND stylesheets. I'm not wading through through your grep; but the following is a cut and paste job example of dequeing stylesheets and handling script dependencies in child functions.php for Hemingway theme.
function dequeue_unnecessary_fonts() {
wp_dequeue_style( 'hemingway_googleFonts-css' );
wp_deregister_style( 'hemingway_googleFonts-css' );
wp_dequeue_style( 'hemingway_googleFonts' );
wp_deregister_style( 'hemingway_googleFonts' );
}
// add_action( 'wp_print_styles', 'dequeue_unnecessary_fonts',20 );
add_action( 'wp_enqueue_scripts', 'dequeue_unnecessary_fonts',20 );
// wp_print_styles (still) works for me (it can affect admin styles)
// but post WP3.3 the Codex recommends using above style functions with wp_enqueue_scripts instead
function hem_script_fix() {
wp_dequeue_script( 'hemingway_global' );
wp_enqueue_script( 'hemingway_global', get_stylesheet_directory_uri() . '/global.js', array( 'jquery' ) );
// identifies script is dependent on jquery which must be "loaded" earlier
}
add_action( 'wp_enqueue_scripts', 'hem_script_fix' );

Related

How to run script for specific page from child-theme?

I'm working on a project that I have a child-theme on which I'm running specific functions from the function.php file.
My problem is that when I try to add an if condition to only run the script on a specific page, it doesn't work.
I'm using the functions.php from the child theme.
In other words... I need to be able to get the current page on the child theme.
What am I doing wrong?
add_action(
'init', function() {
if (is_page('contact')) {
wp_register_script( 'my-func', get_stylesheet_directory_uri() . 'my-func.js', '', '', false );
wp_enqueue_script( 'my-func' );
}
}
);
Thanks
for theme code will go to your functions.php file and for plugin code will go to the public functions.
add_action( 'wp_enqueue_scripts', 'my_assets' );
function my_assets()
{
if (is_page('booking')) {
/* for plugin */
wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/custom.css', array(), $this->version, 'all');
/*for theme */
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
}

I cannot get jQuery to work in wordpress

Tried many different techniques to get jquery working in my custom wordpress theme but still haven't got anywhere.
I've included two different methods that I've tried below near the end of the code, with one commented out here.
Can anyone see why this code might not be working?
PHP:
<?php
function wpb_custom_new_menu() {
register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );
function enqueue_stylesheets() {
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/css/style.css');
wp_enqueue_style('fonts', 'https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Raleway:400,500,600,700');
wp_enqueue_style( 'fontAwesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' );
}
/*function wp_enqeue_scripts() {
wp_register_script('navbarScroll', home_url() . '/js/navbarScroll.js', array( 'jquery' ));
wp_enqueue_script('navbarScroll');
//wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js', array('jquery'), '3.3.4', true );
}*/
function navbar_script() {
wp_register_script( 'jquery.navbarScroll', get_template_directory_uri() . '/js/jquery.navbarScroll.js', array( 'jquery' ) );
wp_enqueue_script( 'jquery.navbarScroll' );
}
add_action('wp_enqueue_scripts', 'enqueue_stylesheets', 'enqueue_scripts', 'navbar_script');
My Test JS (I've used both $ and jquery):
jquery(document).ready(function() {
jquery('#jQueryTest').html('jQuery is Working');
jQuery('nav').hover(function() {
$(this).css('display', 'none');
})
/*var a = $('nav').offset().top;
$(document).scroll(function() {
if ($(this).scrollTop() > a)
{
$(this).removeClass('nav');
$(this).addClass('nav-scrolled');
} else {
$(this).removeClass('nav-scrolled');
}
});*/
});
Well it seems that you have too many parameters in your add_action function.
The add_action should have only 4 arguments as follows:
add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )
See WordPress documentation for more information.
That's what should be working just fine:
function enqueueScript() {
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/css/style.css');
wp_enqueue_style('fonts', 'https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Raleway:400,500,600,700');
wp_enqueue_style( 'fontAwesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' );
wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js', array('jquery'), '3.3.4', true );
wp_register_script('navbarScroll', get_stylesheet_directory_uri() . '/js/navbarScroll.js', array( 'jquery' ), '', true );
wp_enqueue_script('navbarScroll');
wp_register_script( 'jquery.navbarScroll', get_template_directory_uri() . '/js/jquery.navbarScroll.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'jquery.navbarScroll' );
}
add_action( 'wp_enqueue_scripts', 'enqueueScript' );
Note that it is not necessary to enqueue style and script separately. One method can do both by using wp_enqueue_scripts.
If you really want to separate your style and script in different function, you should call add_action this way:
add_action('wp_enqueue_scripts','wp_enqueue_scripts');
add_action( 'wp_enqueue_scripts','enqueue_stylesheets');
add_action( 'wp_enqueue_scripts','enqueue_scripts');
add_action( 'wp_enqueue_scripts','navbar_script');
If you want to load jQuery, add it to your function:
wp_enqueue_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js', array(), null, true);
Its easy if you can have a code like this to solve correctly and without confusiong. You have forgotten to pass add_action for your scripts.
function wpb_custom_new_menu() {
register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );
function enqueue_stylesheets() {
//For registering Styles
wp_enqueue_style('style', get_stylesheet_directory_uri() . '/css/style.css');
wp_enqueue_style('fonts', 'https://fonts.googleapis.com/css?family=Montserrat:400,600,700|Raleway:400,500,600,700');
wp_enqueue_style( 'fontAwesome', 'https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css' );
//For registering Scripts files
wp_enqueue_script( 'jquery');
wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js', array('jquery'), '3.3.4', true );
wp_register_script('navbarScroll', get_stylesheet_directory_uri() . '/js/navbarScroll.js', array( 'jquery' ), '', true );
wp_enqueue_script('navbarScroll');
wp_register_script( 'jquery.navbarScroll', get_template_directory_uri() . '/js/jquery.navbarScroll.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'jquery.navbarScroll' );
}
add_action( 'wp_enqueue_scripts', 'enqueue_stylesheets' );
Hope this works for you.
THanks

Use latest Boostrap deliverd via MAXCDN via Wordpress

I am creating my first wordpress theme from a static site I have built which runs on Boostrap delivered by CDN.
Please no suggestions to download boostrap for this project it needs to be delivered via the CDN.
I would like to load boostrap via the CDN using my functions.php file but instead of loading it just displays the text at the top of the loaded page (there is nothing obvious in the inspector pannel and no error message it just appears to dispaly the infomration from functions.php as text).
I have included <?php wp_head(); ?> in header.php
All code from functions.php:
function my_scripts_enqueue() {
wp_register_script( 'bootstrap-js', '://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js', array('jquery'), NULL, true );
wp_register_script( 'gajax-js', '://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js', array('jquery'), NULL, true );
wp_register_style( 'bootstrap-css', '://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css', false, NULL, 'all' );
wp_register_style( 'fontawsome-css', '://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css', false, NULL, 'all' );
wp_enqueue_script( 'bootstrap-js' );
wp_enqueue_script( 'gajax-js' );
wp_enqueue_style( 'bootstrap-css' );
wp_enqueue_style( 'fontawsome-css' );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_enqueue' );
If you want everything from the CDN, you'll have to get rid of the default jQuery expect on the backend or loginscreen.
After that you just add the CDN without ":"
add_action( 'wp_enqueue_scripts', 'register_jquery' );
function register_jquery() {
if (!is_admin() && $GLOBALS['pagenow'] != 'wp-login.php') {
wp_deregister_script('jquery');
wp_register_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js', false, '1.11.2');
wp_register_script( 'bootstrap-js', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js', array('jquery'));
wp_register_style( 'bootstrap-css', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');
wp_register_style( 'fontawsome-css', '//maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css');
wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap-js');
wp_enqueue_style('bootstrap-css');
wp_enqueue_style('fontawsome-css');
}
}

Wordpress and Bootstrap- Unable to link scripts through functions.php

Read the wordpress codex several times and I was still unable to link the files so that my dropdown menu could work. Is there anything I need to do that is not in the functions.php?
<?php
function load_js() {
wp_enqueue_script( 'mytheme-jquery', get_template_directory_uri() . '/js/jquery-2.1.4.min.js', array( 'jquery' ) );
wp_enqueue_script( 'mytheme-bootstrap', get_template_directory_uri() . '/js/bootstrap.min.js', array( 'jquery' ) );
}
add_action( 'wp_enqueue_scripts', 'load_js' );
?>

Bootstrap & Wordpress issue

it will probably something stupid as ussual but.. I can't get my style.css or custom.css to override the bootstrap css. I don't wanna put all my custom css in my header file because that is not really the way to do it I suppose..
Now I Tried to add it in my functions.php like this:
//Enqueue scripts and styles
function horizon_scripts() {
//Load the theme CSS
wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/bootstrap/css/bootstrap.min.css');
wp_enqueue_style( 'main', get_template_directory_uri() . '/style.css');
// Register the script like this for a theme:
wp_register_script( 'custom-script', get_template_directory_uri() . '/bootstrap/js/bootstrap.js', array( 'jquery' ) );
wp_register_script( 'custom-script', get_template_directory_uri() . '/bootstrap/js/bootstrap.min.js', array( 'jquery' ) );
wp_register_script( 'custom-script', get_template_directory_uri() . '/bootstrap/css/custom.css', array(), '1.0.1', 'all' );
// For either a plugin or a theme, you can then enqueue the script:
wp_enqueue_script( 'custom-script' );
}
add_action( 'wp_enqueue_scripts', 'horizon_scripts' );
So it loads my bootstrap css first and then the style.css and custom.css but that didn't quite hit it..
So any help would be appreciated :)
You should be using get_stylesheet_directory_uri(). Here's just the css, without your js:
function horizon_scripts() {
wp_enqueue_style( 'bootstrap', get_stylesheet_directory_uri() . '/bootstrap/css/bootstrap.min.css' );
wp_enqueue_style( 'main', get_stylesheet_uri(), array('bootstrap') );
wp_enqueue_style( 'custom', get_stylesheet_directory_uri() . '/custom.css', array('main') );
}
add_action( 'wp_enqueue_scripts', 'horizon_scripts' );
Note that the third parameter of wp_enqueue_style() is "dependencies". Read more in the Codex.

Categories