How to replace scripts in WordPress with the ones from CDN? - javascript

I am newbie of wordpress.
There are always some JS and Css from CDN in Wordpress, theme or plugins, like:
https://ajax.googleapis.com/ajax/libs/webfont/1.5.0/webfont.js
https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css
Unfortunately, those external CDNs resource are quite slow in our country, and will caused our site serious delay.
Is there anyway to replace it with a local server copy please?
How should I do it please?
Thanks in advance.

One solution you got in comments that how to load jQuery locally and in case you have any other scripts or styles which are not available locally by default from WordPress so you can use wp_enqueue_script.
/**
* Proper way to enqueue scripts and styles.
*/
function wpdocs_theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wpdocs_theme_name_scripts' );`

Well you can always override your current themeto enqueue your css and js. Basically go to :
wordpress/wp-content/themes/(the theme your using)/functions.php
You will need to modify the "functions.php" file and add an action in order to enqueue your new scripes and styles.
function mynew_scripts() {
wp_enqueue_script('webfont', 'https://ajax.googleapis.com/ajax/libs/webfont/1.5.0/webfont.js');
wp_enqueue_style('jquery-ui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css');
//And for local file use get_template_directory_uri() to get the path
wp_enqueue_script('yourjsfile',get_template_directory_uri().'/yourthemedir/assets/js/yourjsfile.js');
wp_enqueue_style('yourcssfile',get_template_directory_uri().'/yourthemedir/assets/js/yourcssfile.css');
}
add_action('wp_enqueue_scripts', 'mynew_scripts');
check up these functions wp_enqueue_script , wp_enqueue_style

Related

load jquery inline script without dependency in wordpress

I'm rather new to the whole wordpress scene so I dont"t really know the do's and dont's of wordpress of whats possible and what not.
I am trying to build a custom theme from a pre-build one called illdy. And i want to load a jquery script in the footer for changing the menu styles on scroll by toggling a class. but the script won't load. i've used wp_enqueu_script and a link to the google ajax library, and put my script in a function in my functions.php to include it in the footer.
i don't know if this is even possible, any help will be appreciated.
Did you follow this?
Usage
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
Links a script file to the generated page at the right time according to the script dependencies, if the script has not been already included and if all the dependencies have been registered. You could either link a script with a handle previously registered using the wp_register_script() function, or provide this function with all the parameters necessary to link a script.
This is the recommended method of linking JavaScript to a WordPress generated page.
Please note:
The function should be called using the wp_enqueue_scripts action hook if you want to call it on the front-end of the site, like in the examples above. To call it on the administration screens, use the admin_enqueue_scripts action hook. For the login screen, use the login_enqueue_scripts action hook. Calling it outside of an action hook can lead to problems, see the ticket #11526 for details.
function themeslug_enqueue_style() {
wp_enqueue_style( 'core', 'style.css', false );
}
function themeslug_enqueue_script() {
wp_enqueue_script( 'my-js', 'filename.js', false );
}
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' );
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_script' );
To make a theme compatible with child themes, you can use functions to determine the correct path to the core stylesheets ( without using #import in the CSS ):
function themeslug_enqueue_style() {
if ( is_child_theme() ) {
// load parent stylesheet first if this is a child theme
wp_enqueue_style( 'parent-stylesheet', trailingslashit( get_template_directory_uri() ) . 'style.css', false );
}
// load active theme stylesheet in both cases
wp_enqueue_style( 'theme-stylesheet', get_stylesheet_uri(), false );
}
add_action( 'wp_enqueue_scripts', 'themeslug_enqueue_style' );
From:
https://developer.wordpress.org/reference/functions/wp_enqueue_script/
and
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts

Wordpress - Javascript enqueue

I have a wordpress website and I know that to run scripts on a wordpress page, you need to enqueue the script in the functions.php file. I am not sure how to do this proccess?
my Javascript is called boosting.js
Can someone give me an example code of how to do this?
Thank you very much! (I am a bit new to all this)
You need to add this to functions.php.
script-name - is the name you want to give the script
get_template_directory_uri() . '/js/boosting.js' - is path to where the script is located. If you're using a child theme then change this to get_stylesheet_directory_uri() . '/js/boosting.js'
array() - this is for dependencies. If your script needs jQuery to work than use array('jquery')
1.0.0 - version of the script. Useful for caching
true - means that the script will be loaded in the footer, use false if you needed in the header
/**
* Proper way to enqueue scripts and styles.
*/
function wp_theme_enque_scripts() {
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/boosting.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'wp_theme_enque_scripts' );
You can read more here https://developer.wordpress.org/reference/functions/wp_enqueue_script/

How to add a JS to my wordpress website

So I recently bought a JS file from someone and I have a wordpress website. The script file contains a css folder, a script folder, and a font folder. Where should I upload this to? Publid_html? And how do I implement it to my website? (Its made with bootstrap)
Thank you
You can make folders within the THEME folder.
So basically
-YOURTHEME(main theme folder)
css(folder)
js(folder)
Img(folder)
index.php(file)
Etc.
as for where to call it from: put this code in your functions.php file.
function YOURTHEME_script_enqueue() {
wp_enqueue_script( 'bootstrapjs', get_template_directory_uri() . '/js/bootstrap.min.js', array(), true )
wp_enqueue_style('bootstrapstyle', get_template_directory_uri() . '/css/bootstrap.min.css', array(), 'all');
add_action( 'wp_enqueue_scripts', 'blackmass_script_enqueue');
here you can add the scripts and wordpress will place them in the HEAD or FOOT locations depending on the SCRIPT/STYLE at the begining of the argument, and the ALL/TRUE functions at the end of the argument.
as for the two index.php files one in scripts folder, and one in main theme folder. you only need the one thats in the themes folder. you can delete the other one.
The best implementation of those files would be root/wp-content/themes/{your-custom-theme}/assets
So under assets you can put like css,js,fonts,images
and make it link from functions.php
You must read the WP Codex about this, but here an quick example:
With get_stylesheet_directory_uri() function you can grab link to your current theme folder (themes/your-theme-name/) for example.
In this folder you can create your own structure, but best practice is to have diffrent folders for js/, css/, images/ etc.
When you decide where you want to put your .js and .css files then you must include them with wp_enqueue_script() and wp_enqueue_style() functions using wp_enqueue_scripts hook like this:
function my_scripts_include() {
wp_enqueue_style( 'my-style-name', get_stylesheet_directory_uri() . '/css/my-css-file.css', [] );
wp_enqueue_script( 'my-script-name', get_template_directory_uri() . '/js/my-script.js', array('my-script-depends-from-this-script'), '1.0.0', true );
if ( some_logic ) {
wp_enqueue_script('my-other-script' .... );
}
}
add_action( 'wp_enqueue_scripts', 'my_scripts_include' );
You must put this code in your theme functions.php file.
But really, there have so many tutorials about WP, you must read some to understand better this process.

wp_enqueue not working at all

I'm trying to create a wordpress theme and start learning the best way to include javascript and css files using wp_enqueue. After couple hours, I still can't load the files. Here is the code I use
function compro_scripts() {
wp_register_style( 'style', get_template_directory_uri() . '/css/materialize.css', array(), '20150208', 'all');
wp_enqueue_script( 'style' );
}
add_action( 'wp_enqueue_scripts', 'compro_scripts' );
It looked simple on every tutorial I've read, but it still not work for me. Would anyone mind helping me solve this ?
This very basic knowledge of Wordpress, but as a newbie I found out this is pretty confusing. If you want to use the best practice to include javascript and css files make sure to add wp_head() and wp_footer(). The rest of it is described clearly here How to Load Javascript like Wordpress Master
You've registered the "style" handle with wp_register_style() and you're enqueuing it by using wp_enqueue_script() which is incorrect. wp_enqueue_script() in your code needs to be replaced by wp_enqueue_style(), since it's a style that you are including, not a script.
Alternatively, you can simply enqueue scripts and styles directly without the need to register them separately - like this
function compro_scripts() {
$template_dir = get_template_directory_uri();
//enqueue a style
wp_enqueue_style( 'style', $template_dir . '/css/materialize.css', array(), '20150208', 'all');
//enqueue a script
wp_enqueue_script( 'custom', $template_dir . '/js/custom.js', array( 'jquery' ), false, false );
}
add_action( 'wp_enqueue_scripts', 'compro_scripts' );

Load Css, JS plugin and Custom Script file into wordpress

Been having a hard time getting this to work... I'm trying to use animation for some transitions on a website I'm working on but when I include the files the average way it breaks the theme... A lot of reading has brought me knowing I need to use functions.php to enqueue the scripts and CSS but I tried and no dice.
I am using a child theme
The CSS I need to load is located in a folder my child-theme's directory called "css", the custom js and plugin js I need to load is located in a folder in my child-themes directory called "js".
1-The CSS needs to be in the header
2-jQuery needs to load
3-The plugin then needs to load
4-My scripts.js file needs to load, it manipulates the plugin's settings.
If you still don't understand this then basically I want to use http://git.blivesta.com/animsition/ within a wordpress child-theme. Thanks.
Try reading https://codex.wordpress.org/Child_Themes , it will help you a lot.
Regarding your question, you have to place the following code in your child's theme functions.php file:
function animsition_enqueue_styles_and_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
wp_enqueue_style( 'animsition-style', get_stylesheet_directory_uri() . '/css/animsition.min.css' );
wp_enqueue_script( 'animsition-js', get_stylesheet_directory_uri() . '/js/jquery.animsition.min.js', array('jquery'), '20150628', false );
wp_enqueue_script( 'custom-animsition-js', get_stylesheet_directory_uri() . '/js/scripts.js', array('animsition-js','jquery'), '20150628', false );
}
add_action( 'wp_enqueue_scripts', 'animsition_enqueue_styles_and_styles' );

Categories