11 views and you’re just in time for more!
In this tutorial, we will learn how to add custom JavaScript to a WordPress site without using a plugin. Adding JavaScript is essential for customizing functionality or enhancing the interactivity of your website.
Instead of relying on plugins, we’ll follow the WordPress-recommended method of enqueuing scripts using the functions.php
file.
As part of this tutorial, we’ll write a simple JavaScript file that logs the message:"Hello, this is a Custom JavaScript"
to the browser’s console. This hands-on example will help you understand the process of integrating custom scripts into your WordPress theme step by step.
Step-by-Step: Enqueue Custom JavaScript Using functions.php
Let’s 1st prepare our JavaScript file. Now, For a seamless coding experience, consider using an ergonomic keyboard like the Logitech MX Keys. If you are just getting started with JavaScript or even if you are an experienced developer, Books like Eloquent JavaScript can help you master JavaScript for WordPress customizations
Navigate to Your Theme Directory
The .js
file will be placed in your active theme’s folder. The path will look like this:
/wp-content/themes/your-active-theme/js/custom-script.js
Create the js
Folder
If the js
folder doesn’t already exist in your theme:
- Access your WordPress site via FTP, a File Manager (from your hosting control panel), or locally (if working offline).
- Go to “/wp-content/themes/your-active-theme/“
- Create a folder named
js
inside your active theme directory.
Create the custom-script.js
File
- Inside the
js
folder, create a new file namedcustom-script.js
. - Open
custom-script.js
in a text editor and add the following code: “console.log("Hello, this is a Custom JavaScript");“ - Save the file.
Final File Structure
Your theme folder should now look like this:
your-active-theme/
├── js/
│ └── custom-script.js
├── functions.php
├── style.css
└── other theme files...
Now, lets see how to register it in our theme step by step.
1. Locate the functions.php
File
- Log in to your WordPress admin dashboard.
- Navigate to Appearance > Theme File Editor.
- On the right-hand side, find and click on the
functions.php
file under the active theme.Note: If Theme File Editor is disabled, you can access the file through FTP or your hosting file manager:- Connect via FTP or File Manager.
- Navigate to
/wp-content/themes/your-active-theme/
. - Open the
functions.php
file.
2. Add Code to Enqueue the Custom Script
Add the following code snippet at the bottom of the functions.php
file:
function custom_enqueue_scripts() {
wp_enqueue_script(
'custom-script', // Handle for the script
get_template_directory_uri() . '/js/custom-script.js', // Path to the JS file
array(), // Dependencies (leave empty if no dependencies)
'1.0', // Version of the script
true // Load script in footer (true for footer, false for header)
);
}
add_action('wp_enqueue_scripts', 'custom_enqueue_scripts');
Explanation of Parameters:
'custom-script'
: A unique name for the script.get_template_directory_uri() . '/js/custom-script.js'
: Dynamically generates the path to the JavaScript file in the theme directory.array()
: Specifies dependencies (e.g.,'jquery'
if your script depends on jQuery). Leave empty for no dependencies.'1.0'
: Script version. Useful for cache busting when updating the script.true
: Ensures the script is loaded in the footer for better page load performance.
3. Create the JavaScript File
- Using FTP or File Manager, navigate to your active theme’s directory
/wp-content/themes/your-active-theme/
- Create a folder named
js
if it doesn’t already exist. - Inside the
js
folder, create a file namedcustom-script.js
4. Add JavaScript Code to the File
Open custom-script.js
in a text editor and add the following code:
console.log("Hello, this is a Custom JavaScript");
Save the file.
5. Verify File Permissions
Ensure the custom-script.js
file has the correct permissions (typically 644
), so WordPress can load it properly.
6. Test the Implementation
- Open your website in a browser.
- Right-click anywhere on the page and select Inspect (or press
Ctrl+Shift+I
/Cmd+Option+I
). - Go to the Console tab in the developer tools.
- Refresh the page. You should see the message:
My Personal Recommendation
Web Development Tools and Resources:
- Books for Learning Web Development:
- “JavaScript: The Definitive Guide” by David Flanagan.
- “Eloquent JavaScript“ by Marijn Haverbeke.
- Coding Accessories:
- Ergonomic keyboards and mice for comfortable coding sessions.
- Standing desks or laptop stands.
- Laptops for Development:
- Lightweight laptops like MacBook Air or Dell XPS series for developers.
- High-performance laptops like ASUS ROG for multitasking.
- External Monitors:
- Recommend monitors with high resolution, ideal for coding (e.g., Dell Ultrasharp series or LG UltraWide).
- Development-Friendly Gadgets:
- External hard drives or SSDs for project storage.
- Bluetooth earphones or headphones for focus (e.g., OnePlus Bullets, Sony WH-1000XM series).
Please note that some of the links in this post are affiliate links. This means that if you click on the link and purchase an item, I will receive a small commission. There is no additional cost to you. Thank you for your support!
Discover more from Prime Inspire
Subscribe to get the latest posts sent to your email.