I read the Codex and a few blog posts about using jQuery in WordPress, and its very frustrating. I’ve got as far as loading jQuery in functions.php
file, but all of the guides out there are crappy because they assume you already have a ton of WordPress experience. For instance, they say that now that I’m loading jQuery through the functions.php
file, now all I have to do is load my jQuery.
How exactly do I do this? What files, specifically, do I add code to? How exactly do I add it for a single WordPress page?
I know what you mean about the tutorials. Here’s how I do it:
First you need to write your script. In your theme folder create a folder called something like ‘js’. Create a file in that folder for your javascript. E.g. your-script.js. Add your jQuery script to that file (you don’t need
<script>
tags in a .js file).Here is an example of how your jQuery script (in wp-content/themes/your-theme/js/your-scrript.js) might look:
Notice that I use jQuery and not $ at the start of the function.
Ok, now open your theme’s functions.php file. You’ll want to use the
wp_enqueue_script()
function so that you can add your script whilst also telling WordPress that it relies on jQuery. Here’s how to do that:Assuming that your theme has wp_head and wp_footer in the right places, this should work. Let me know if you need any more help.
WordPress questions can be asked over at WordPress Answers.
After much searching, I finally found something that works with the latest WordPress. Here are the steps to follow:
Make sure your custom jQuery .js looks like this:
Go to the theme’s directory, open up functions.php
Add some code near the top that looks like this:
If you use wordpress child theme for add scripts to your theme, you should change the get_template_directory_uri function to get_stylesheet_directory_uri, for example :
Parent Theme :
Child Theme :
get_template_directory_uri : /your-site/wp-content/themes/parent-theme
get_stylesheet_directory_uri : /your-site/wp-content/themes/child-theme
You can add jQuery or javascript in theme’s function.php file.
The code is as below :
For more detail visit this tutorial : http://www.codecanal.com/add-simple-jquery-script-wordpress/
Beside putting the script in through functions you can “just” include a link ( a link rel tag that is) in the header, the footer, in any template, where ever. You just need to make sure the path is correct. I suggest using something like this (assuming you are in your theme’s directory).
A good practice is to include this right before the closing body tag or at least just prior to your footer. You can also use php includes, or several other methods of pulling this file in.
The solutions I’ve seen are from the perspective of adding javascript features to a theme. However, the OP asked, specifically, “How exactly do I add it for a single WordPress page?” This sounds like it might be how I use javascript in my WordPress blog, where individual posts may have different javascript-powered “widgets”. For instance, a post might let the user change variables (sliders, checkboxes, text input fields), and plots or lists the results.
Starting from the JavaScript perspective:
Donât even think about including significant JavaScript in your postâs htmlâcreate a JavaScript file, or files, with your code.
If your JavaScript widget interacts with html controls and fields, youâll need to understand how to query and set those elements from JavaScript, and also how to let UI elements call your JavaScript functions. Here are a couple of examples; first, from JavaScript:
And from html:
Add this to your .js file, using the name of your function that configures and draws your JavaScript widget when the page is ready for it:
In the WordPress code editor, I typically specify the scripts at the end of the post. For instance, I have a scripts folder in my main directory. Inside I have a utilities directory with common JavaScript that some of my posts may shareâin this case some of my own math utility function and the flotr2 plotting library. I find it more convenient to group the post-specific JavaScript in another directory, with subdirectories based on date instead of using the media manager, for instance.
WordPress registers jQuery, but it isnât available unless you tell WordPress you need it, by enqueuing it. If you donât, the jQuery command will fail. Many sources tell you how to add this command to your functions.php, but assume you know some other important details.
First, itâs a bad idea to edit a themeâany future update of the theme will wipe out your changes. Make a child theme. Hereâs how:
https://developer.wordpress.org/themes/advanced-topics/child-themes/
The childâs functions.php file does not override the parent themeâs file of the same name, it adds to it. The child-themes tutorial suggest how to enqueue the parent and child style.css file. We can simply add another line to that function to also enqueue jQuery. Here’s my entire functions.php file for the child theme:
**#Method 1:**Try to put your jquery code in a separate js file.
Now register that script in functions.php file.
Now you are done.
Registering script in functions has it benefits as it comes in
<head>
section when page loads thus it is a part of header.php always. So you don’t have to repeat your code each time you write a new html content.#Method 2: put the script code inside the page body under
<script>
tag. Then you don’t have to register it in functions.You can add custom javascript or jquery using this plugin.
https://wordpress.org/plugins/custom-javascript-editor/
When you use jQuery don’t forget use jquery noconflict mode
There are many tutorials and answers here how to add your script to be included in the page. But what I couldn’t find is how to structure that code so it will work properly. This is due the $ being not used in this form of JQuery.
So here is my code and you can use that as a template.
Answer from here: https://premium.wpmudev.org/blog/adding-jquery-scripts-wordpress/
“We have Google” cit.
For properly use script inside wordpress just add hosted libraries. Like Google
After selected library that you need link it before your custom script: exmpl
and after your own script
The simplest way to add a script inside your functions.php file (on your theme / child theme) without using wp_enqueue_script is this one:
As you see, you use the wp_footer action to inject the code.
This may not be a good practice if you use it heavily or if you have to ‘speak’ with other plugins, etc. But is the fastest way!
You can also put directly the Javascript code inside header.php or footer.php if is a code that will be inserted all-over WordPress
You can use WordPress predefined function to add script file to WordPress plugin.
Look at the post which helps you to understand that how easily you can implement jQuery and CSS in WordPress plugin.
No. You should never just add a link to an external script like this in WordPress. Enqueuing them through the functions.php file ensures that scripts are loaded in the correct order.
Failure to enqueue them may result in your script not working, although it is written correctly.
you can write your script in another file.And enqueue your file like this
suppose your script name is
image-ticker.js
.in the place of
/js/image-ticker.js
you should put your js file path.In WordPress, the correct way to include the scripts in your website is by using the following functions.
These functions are called inside the hook
wp_enqueue_script
.For more details and examples, you can check Adding JS files in WordPress using wp_register_script & wp_enqueue_script
Example:
I was having some serious issues with all the other answers here, so here’s my addition for those who are wanting a more up to date solution.
I know this is not exactly what the OP asked because it uses shortcodes, but this is the only way I could make it work and it has the added benefit of only having the function when the page contains the shortcode.
This doesn’t use
wp_enqueue_script()
noradd_action()
functions.I use the Code Snippets plugin which means that there’s no need to fiddle around with functions.php and create new .js files.
In a shortcode, echo the jQuery function as so:
Do you only need to load jquery?
1) Like the other guides say, register your script in your functions.php file like so:
2) Notice that we don’t need to register jQuery because it’s already in the core. Make sure wp_footer() is called in your footer.php and wp_head() is called in your header.php (this is where it will output the script tag), and jQuery will load on every page. When you enqueue jQuery with WordPress it will be in “no conflict” mode, so you have to use jQuery instead of $. You can deregister jQuery if you want and re-register your own by doing wp_deregister_script(‘jquery’).
I’m using this plugin with elementor https://wordpress.org/plugins/insert-php/
You can copy and paste your script and call it using a shortcode. Without the need to edit functions.php