[rank_math_breadcrumb]

How to add JavaScript in WordPress Pages or Posts (2 Methods)

Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn

Do you want to add JavaScript in wordpress website post or page? as a default there’s no way to add script code. so in this tutorials i will going to guide you how to add custom javascript in your wordpress pages or posts.

What is JavaScript?

JavaScript, often abbreviated as JS, is a high-level, just-in-time compiled, multi-paradigm programming language that conforms to the ECMAScript specification. JavaScript has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions. Wikipedia

JavaScript code snippet may look like this:

<script type="text/javascript"> 
 
// Some JavaScript code
 
</script>
 
<!-- Another Example: --!>  
 
<script type="text/javascript" src="/path/to/some-script.js"></script>

Method 1. Add a JavaScript Code Using a Plugin injected to Header/Footer

By adding JavaScript in your site using a plugin you need to Install Insert Headers and Footers Plugin. For video tutorial how to install a plugin please refer this video below.

Method 2. Add Javascript Code Manually via function.php

Adding a tracking code in your site like google analytics you need to add it inside <head> tag in your site but some of the themes don’t have it as default, so you need to add it manually in your child theme functions.php by adding this code below.

function ac_hook_javascript() {
    ?>
        <script>
          // your javscript code goes
        </script>
    <?php
}
add_action('wp_head', 'ac_hook_javascript');

Adding JavaScript to a Specific WordPress Post or Page Using Code below.

function ac_hook_javascript() {
  if (is_single ('1191')) { 
    ?>
        <script type="text/javascript">
          // your javscript code goes here
        </script>
    <?php
  }
}
add_action('wp_head', 'ac_hook_javascript');

Note: 1191 is a POST id / PAGE id. If you can’t find it please see the video below.

If you liked this article, then please subscribe to our YouTube Channel for more WordPress video tutorials.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

You want to Learn WordPress?

Be automatically notified of all new posts. No spam. No annoying marketing emails.

How to

Install

WORDPRESS

(Step by Step)

How to

start a

WP BLOG

(Step by Step)

WordPress Help for Beginners

Please join our Facebook group to connect WordPress experts.