How to Add JavaScript to WordPress Pages and Posts (Complete Guide)
JavaScript allows you to add interactivity, dynamic behaviour, and custom functionality to WordPress pages and posts. From simple pop-ups and form validation to advanced tracking scripts and animations, JavaScript helps Irish businesses improve user experience without changing the core WordPress system.
This guide explains safe and practical ways to add JavaScript to WordPress, when to use each method, and how to avoid common mistakes.
Why Add JavaScript to WordPress?
You might want to add JavaScript to:
-
Track user behaviour and conversions
-
Add custom interactions or animations
-
Integrate third-party tools
-
Modify page behaviour without editing themes
-
Improve usability and engagement
WordPress doesn’t require coding knowledge, but adding JavaScript correctly ensures your site stays secure, fast, and update-safe.
Important Rule Before You Start
Avoid editing core WordPress files.
Always add JavaScript in a way that does not break your theme during updates.
Method 1: Add JavaScript Using a Plugin (Recommended for Beginners)
This is the safest and easiest option.
Step 1: Install a JavaScript Plugin
Go to Plugins → Add New and install a plugin that allows custom code insertion.
Step 2: Activate the Plugin
Once activated, the plugin will create a section for adding scripts.
Step 3: Choose Where the Script Should Load
Most plugins allow you to insert JavaScript:
-
Site-wide
-
Only on specific pages
-
Only in posts
Step 4: Paste Your JavaScript Code
Copy and paste your JavaScript into the provided field.
Step 5: Save and Test
Save changes and test the page to ensure the script runs correctly.
This method is ideal for tracking scripts and simple functionality.
Method 2: Add JavaScript Using the WordPress Block Editor (Per Page or Post)
Best for page-specific scripts.
Step 1: Edit the Page or Post
Open the page or post where the JavaScript is required.
Step 2: Add a Custom HTML Block
Click the + Add Block button and select Custom HTML.
Step 3: Insert the JavaScript Code
Wrap your code inside <script> tags and paste it into the block.
Step 4: Update the Page
Save and preview the page to confirm the script works.
Method 3: Add JavaScript via Theme Files (Advanced Method)
This method is recommended for developers or experienced users.
Step 1: Use a Child Theme
Never edit the main theme directly. Always create or use a child theme.
Step 2: Locate the functions.php File
Go to Appearance → Theme File Editor and open functions.php.
Step 3: Enqueue the JavaScript File
Add code to properly load the JavaScript file.
Step 4: Upload the JavaScript File
Upload your .js file to the child theme directory.
Step 5: Save and Test
Refresh your website and confirm the script loads correctly.
This approach is best for reusable scripts across the site.
Method 4: Add JavaScript Using Elementor
If you’re using Elementor, there are built-in options.
Add JavaScript to a Single Page
-
Open the page with Edit with Elementor
-
Go to Advanced → Custom CSS or Custom Code
-
Paste your JavaScript
-
Save and preview
Add JavaScript Site-Wide
Elementor Pro allows you to add custom scripts globally via settings.
Method 5: Add External JavaScript Files
Used for tools like chat widgets, analytics, or third-party libraries.
Steps
-
Copy the external script URL
-
Insert it via plugin, header, or footer
-
Choose footer placement to improve page speed
-
Test on multiple devices