WordPress / Customizing WordPress with Code

Adding Custom JavaScript to WordPress

This tutorial dives into how you can enhance your WordPress site's interactivity by adding custom JavaScript. Discover how scripts can make your website more dynamic and engaging.

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Explains how to customize WordPress sites using PHP, CSS, and JavaScript.

1. Introduction

This tutorial aims to guide you on how to add custom JavaScript to your WordPress site. By doing so, you will be able to enhance your site's interactivity, making it more dynamic and engaging for your users.

You will learn:
- The basics of adding custom JavaScript to WordPress
- How to use the WordPress functions.php file
- How to enqueue scripts in WordPress

Prerequisites:
- Basic knowledge of WordPress
- Basic knowledge of JavaScript

2. Step-by-Step Guide

We'll be adding custom JavaScript using two methods: directly into your theme's functions.php file and by enqueuing the script.

Directly into functions.php

  • Find the functions.php file in your theme directory and open it.
  • Add your JavaScript code inside the script tags.

Enqueuing the script

  • Create a .js file in your theme directory.
  • Write your JavaScript code in this file and save it.
  • Use the wp_enqueue_script function to enqueue your script in the functions.php file.

Best Practices
* Always enqueue scripts to avoid conflicts with other scripts and plugins.
* Use no-conflict mode when writing jQuery to avoid conflicts with other libraries.

3. Code Examples

Adding custom JavaScript directly into functions.php

<?php
function add_custom_script() {
echo "<script>
// Your JavaScript code here
</script>";
}
add_action('wp_footer', 'add_custom_script');
?>

Enqueueing the script in functions.php

Assuming the JavaScript file is named 'custom.js' and located in a folder named 'js' in your theme directory.

<?php
function add_custom_script() {
wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/js/custom.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'add_custom_script' );
?>

4. Summary

You've learned how to add custom JavaScript to your WordPress site directly into your theme's functions.php file and by enqueueing the script. You've learned how to use the wp_enqueue_script function and you've understood the importance of using no-conflict mode with jQuery.

Next steps for learning would be exploring more WordPress built-in functions and learning more about jQuery.

5. Practice Exercises

  1. Add a simple alert to your website using both methods. The alert should display a message when the website loads.
  2. Create a script that changes the background color of your website every time the page loads.

Solutions

  1. Directly into functions.php
<?php
function add_custom_script() {
echo "<script>
alert('Website loaded');
</script>";
}
add_action('wp_footer', 'add_custom_script');
?>

Enqueueing the script

alert('Website loaded');
  1. Directly into functions.php
<?php
function add_custom_script() {
echo "<script>
document.body.style.backgroundColor = '#' + Math.floor(Math.random()*16777215).toString(16);
</script>";
}
add_action('wp_footer', 'add_custom_script');
?>

Enqueueing the script

document.body.style.backgroundColor = '#' + Math.floor(Math.random()*16777215).toString(16);

Tips for further practice: Try adding more complex scripts to your website, like a slideshow or a contact form validation script.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

XML Sitemap Generator

Generate XML sitemaps for search engines.

Use tool

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help