How to Add Custom JavaScript
WordPress Tutorials & How-To (Advanced)
How to Add Custom JavaScript in WordPress
Adding custom JavaScript in WordPress allows you to enhance your website with animations, tracking codes, API integrations, dynamic behaviors, custom forms, and advanced UI features. But adding JS incorrectly can break your design, cause console errors, or slow down your site. This advanced tutorial shows the safest ways to add JavaScript using Elementor, plugins, themes, functions.php, and custom JS files.
1
Why Add Custom JavaScript?
Add functionality that plugins and themes cannot provide.
JavaScript is used for:
- Custom animations
- Form validation
- API integrations
- Event tracking scripts (GA4, FB Pixel)
- Custom sliders & dynamic UI components
- Custom click events
- Sticky elements & scroll effects
Let’s explore the safest methods to add JS in WordPress.
2
Method 1 — Add JavaScript Using Elementor
Fastest way for Elementor Pro users.
Step 1 — Open Elementor Custom Code
Go to:
Elementor → Custom Code → Add New
Step 2 — Paste Your JavaScript
<script>
document.addEventListener("DOMContentLoaded", function(){
console.log("Custom JS Loaded");
});
</script>
Step 3 — Choose Location
- Header (tracking codes)
- Body (functional scripts)
- Footer (performance-friendly)
Elementor automatically minifies and safely loads your script.
3
Method 2 — Add JS Using a Code Snippets Plugin (Recommended)
Safest for non-developers.
Install the plugin:
- WPCode (Insert Headers & Footers)
- Code Snippets
WPCode Method:
- Go to Code Snippets → Header & Footer
- Paste your JS code inside “Header”, “Body”, or “Footer”
- Save
This method keeps your script safe even during theme updates.
4
Method 3 — Add Inline JavaScript in a Page/Post
For single page requirements.
Gutenberg Method:
Use the Custom HTML block:
<script>
alert("JS running on this page only!");
</script>
Good for page-specific effects.
Elementor Method:
Use an HTML widget and paste your script.
5
Method 4 — Add JavaScript in functions.php (Advanced)
Used by developers & theme creators.
Add this code inside functions.php or a child theme:
function custom_js_file() {
wp_enqueue_script(
'custom-script',
get_stylesheet_directory_uri() . '/js/custom.js',
array(),
'1.0',
true
);
}
add_action('wp_enqueue_scripts', 'custom_js_file');
This loads the JS file from:
/wp-content/themes/your-theme/js/custom.js
Benefits:
- Faster performance
- Safe from plugin conflicts
- Works with caching/minification tools
6
Method 5 — Add JavaScript Using a Child Theme
Protects your JS during theme updates.
- Create folder:
child-theme/js/ - Create file:
custom.js - Enqueue using functions.php (same as above)
Always use a child theme if you are modifying theme files.
7
Method 6 — Add JS in Header/Footer Manually
Only for quick one-time scripts.
Edit header.php or footer.php:
<script>
console.log("This script runs on all pages");
</script>
Warning: Not recommended because updates overwrite changes.
8
Special Use Case — Add Google Analytics, Facebook Pixel, Ads Scripts
Tracking scripts require correct placement.
Use WPCode or Elementor Custom Code.
Best placement:
- Analytics: Head
- Pixel: Head + Event code in Body
- Ads scripts: Head
This ensures accurate user tracking.
9
Troubleshooting JavaScript Errors
Fix common JS problems quickly.
1. JavaScript Not Working?
- Open browser console (F12 → Console)
- Check for red errors
- Fix syntax errors
- Clear cache (plugin + browser + server)
2. Script Running Before Page Loads?
Wrap JS inside DOMContentLoaded:
document.addEventListener("DOMContentLoaded", function() {
// JS here
});
3. Conflicts With Other Plugins?
- Avoid using global variables
- Avoid overwriting jQuery
- Disable minification temporarily
4. jQuery Error: $ is not defined
jQuery(function($){
// use $ safely
});
Need Custom JavaScript or Advanced Functionality?
All SiteCrafted premium websites are developer-friendly and support custom JS, dynamic effects, API integrations, and advanced interactive features.









