How to Easily Add JavaScript in WordPress Pages or Posts – 2 Methods
Do you like to include JavaScript in your WordPress pages or posts? You may need to include JavaScript code throughout your website or on specific pages and posts. By default, WordPress does not allow you to include code directly in your posts. In this tutorial, we’ll teach you how to simply add JavaScript to WordPress pages and posts.
What is JavaScript?
JavaScript is a computer language that runs in the user’s browser, not on your server. This client-side programming allows developers to achieve a variety of exciting things without slowing down your website.
If you wish to include a video player, add calculators, or use another third-party service, you will typically be prompted to copy and paste a JavaScript code snippet into your WordPress page. A typical JavaScript code snippet can look like this:
<script type="text/javascript">
// Some JavaScript code
</script>
<!-- Another Example: --!>
<script type="text/javascript" src="/path/to/some-script.js"></script>
However, if you include a JavaScript code snippet in a WordPress post or page, WordPress will remove it when you attempt to save it.
That being said, let’s look at how to simply add JavaScript to WordPress pages or posts without disrupting the site. You can use the quick buttons below to go directly to the method you want to use.
- Method 1. Add JavaScript Anywhere on Your WordPress Site Using WPCode (Recommended)
- Method 2. Adding JavaScript Code to WordPress Manually Using Code (Advanced)
Method 1. Add JavaScript Anywhere on Your WordPress Site Using WPCode (Recommended)
Sometimes a plugin or tool will require you to copy and paste a JavaScript code snippet onto your website to function effectively. These scripts are often placed in the header or footer of your WordPress blog, so the code is loaded with each page view.
For example, when you install Google Analytics, the code must execute on each page of your website to track your visits. You can manually add the code to your header.php or footer.php files, but any changes will be lost when you update or change your theme.
That is why we propose utilizing WPCode to add JavaScript throughout your WordPress site. WPCode is the most powerful code snippet plugin for WordPress. It allows you to effortlessly add custom code to any area of your website, and it’s completely free.
First, install and activate the free WPCode plugin. Once activated, navigate to Code Snippets » Headers & Footer.
You will see three separate fields labeled ‘Header,’ ‘Body,’ and ‘Footer.’

You can now enter your JavaScript code into one of these boxes, and then click the ‘Save’ button. WPCode will now automatically load the code that you added to each page of your website. You can also include code snippets in other places on your website, such as within posts or pages.
To do this, navigate to Code Snippets » + Add Snippet and select ‘Create Your Own.’

You will now be taken to the ‘Create Custom Snippet‘ page, where you may enter a title for your code and paste it into the ‘Code Preview‘ box.
Next, select ‘JavaScript Snippet‘ from the ‘Code Type’ dropdown option.

Then, simply scroll until you reach the ‘Insertion‘ area.
All you need to do now is choose a ‘Location’ for the code from the dropdown menu. Find ‘Page, Post, Custom Post Type’ and specify where you want the code to appear.
If you select to have WPCode put the snippet before or after a paragraph, you can specify which paragraph of the post it should appear before or after.
For example, if you enter 1 in the ‘Insert Number’ field, the code snippet will appear before or following the first paragraph. Use 2 in the second paragraph, and so on.
After that, simply click the toggle near the top of the page to change to ‘Active,’ and then click the ‘Save Snippet’ button next to it.

That’s all it needs to have your code sample live on the site!
Method 2. Adding JavaScript Code to WordPress Manually Using Code (Advanced)
This solution requires you to add code to your WordPress files. If you’ve never done this before, see our tutorial on how to copy and paste code in WordPress.
First, we’ll show how to add code to your WordPress site’s header. Copy and paste the code below into your functions.php file.
function wpdruggy_hook_javascript() {
?>
<script>
// your javscript code goes
</script>
<?php
}
add_action('wp_head', 'wpdruggy_hook_javascript');
Adding JavaScript to a Specific WordPress Post with Code
If you just want to add JavaScript to one WordPress post, you must add conditional logic to the code.
Check out the following code snippet:
function wpdruggy_hook_javascript() {
if (is_single ('5')) {
?>
<script type="text/javascript">
// your javscript code goes here
</script>
<?php
}
}
add_action('wp_head', 'wpdruggy_hook_javascript');
The given code will only run the JavaScript if the post ID is 5. Make careful to replace ‘5’ with your post ID.
To find the post ID, open the post in which you want the JavaScript to run. The post ID can be found on the page’s URL.

Adding JavaScript to a Specific WordPress Page with Code
If you simply want to add JavaScript to a single WordPress page, you must include conditional logic in the code, as shown before.
Look at the following example:
function wpdruggy_hook_javascript() {
if (is_page ('10')) {
?>
<script type="text/javascript">
// your javscript code goes here
</script>
<?php
}
}
add_action('wp_head', 'wpdruggy_hook_javascript');
The code above only runs JavaScript if the page ID is ’10’. Make careful to replace ’10’ with your page ID.
You may find the page ID using the same approach as before. Simply open the page where you want the JavaScript to run and record the page ID in the URL.
Adding JavaScript to a specific WordPress post or page using code in the footer
If you want the JavaScript to run in the site footer rather than the header, add the following code snippet to your website.
function wpdruggy_hook_javascript_footer() {
?>
<script>
// your javscript code goes
</script>
<?php
}
add_action('wp_footer', 'wpdruggy_hook_javascript_footer');
This code snippet uses wp_footer instead of wp_head. You can also use conditional tags to add JavaScript to specific posts and pages, as demonstrated in the examples above.
Please keep in mind that these solutions are appropriate for both beginners and website owners. If you’re learning WordPress theme or plugin development, you must properly put JavaScript and stylesheets in your projects.
Also Read More Articles
- How to Use User-Generated Content in WordPress to Grow Your Business
- How to Disable Theme and Plugin Editors from the WordPress Admin Panel
- How to Fix ‘Failed To Load Resource’ Error in WordPress
- How To Fix “The Link You Followed Has Expired” Error in WordPress
- How to Fix WordPress RSS Feed Errors (Step-by-Step Guide)
- How to Fix Post-Processing of Image Failed Error in WordPress
- How to Block WordPress Referrer Spam in Google Analytics
- How to Fix WordPress SEO Crawl Budget Problem
- How To Easily Disable the Default WordPress Sitemap
- How to Disable Overflow in WordPress – Remove Horizontal Scroll
The Final Conclusion
In conclusion, integrating JavaScript into WordPress pages or posts is a breeze with the methods outlined above. Whether you opt for the plugin route for simplicity or the manual approach for greater control, both methods empower you to improve your website’s functionality and user experience effortlessly.
By using JavaScript effectively, you can unlock a world of possibilities to customize and optimize your WordPress site according to your unique needs and preferences. So, dive in, experiment, and watch as your website evolves into a dynamic and engaging digital platform.
