How to add custom JavaScript to WordPress without a Plugin

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.

How to add custom JavaScript to WordPress without a Plugin

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:

  1. Access your WordPress site via FTP, a File Manager (from your hosting control panel), or locally (if working offline).
  2. Go to “/wp-content/themes/your-active-theme/
  3. Create a folder named js inside your active theme directory.

Create the custom-script.js File

  1. Inside the js folder, create a new file named custom-script.js.
  2. Open custom-script.js in a text editor and add the following code: “console.log("Hello, this is a Custom JavaScript");
  3. 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

  1. Log in to your WordPress admin dashboard.
  2. Navigate to Appearance > Theme File Editor.
  3. 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

  1. Using FTP or File Manager, navigate to your active theme’s directory
/wp-content/themes/your-active-theme/
  1. Create a folder named js if it doesn’t already exist.
  2. Inside the js folder, create a file named custom-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

  1. Open your website in a browser.
  2. Right-click anywhere on the page and select Inspect (or press Ctrl+Shift+I / Cmd+Option+I).
  3. Go to the Console tab in the developer tools.
  4. Refresh the page. You should see the message:

My Personal Recommendation

Web Development Tools and Resources:

  1. Books for Learning Web Development:
  2. Coding Accessories:
  3. Laptops for Development:
  4. External Monitors:
  5. Development-Friendly Gadgets:

Discover more from Prime Inspire

Subscribe to get the latest posts sent to your email.

Leave a Reply

Scroll to Top

Discover more from Prime Inspire

Subscribe now to keep reading and get access to the full archive.

Continue reading