Connect with us

Others

3 Methods to Easily Add JavaScript in WordPress Pages or Posts

Published

on

3 Methods to Easily Add JavaScript in WordPress Pages or Posts
Share our content or if you have any questions about an article, leave us a comment.

Do you want to add JavaScript in your WordPress pages or posts?

Sometimes you may need to add JavaScript code to your entire website or into specific pages and posts. By default, WordPress does not let you add code directly in your posts.

In this article, we’ll show you how to add JavaScript in WordPress pages or posts easily.

What is JavaScript?

JavaScript is a programming language that runs on the user’s browser, not on your server. This client-side programming allows developers to do a lot of cool things without slowing down your website.

If you want to embed a video player, add calculators, or some other third-party service, then you will be often asked to copy and paste a JavaScript code snippet into your WordPress website.

A typical 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>

But, if you add a javascript code snippet to a WordPress post or page, then it will be deleted by WordPress when you try to save it.

With this method, you need to add code to your WordPress files. If you haven’t done this before, then check out our guide on how to copy and paste code in WordPress.

First, we’ll show you how to add code to your WordPress site’s header. You need to copy the following code and add it to your functions.php, in a site-specific plugin, or by using a code snippets plugin.

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

Adding JavaScript to a Specific WordPress Post Using Code

If you only want to add JavaScript to a single WordPress post, then you will need to add conditional logic to the code.

Take a look at the following code snippet:

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

The code above will only run the JavaScript if the post ID matches ’5’. Make sure you replace the ’5’ with your own post ID.

 

Disclaimer: We sell any type of script/themes/plugins, we also offer web design/developement, graphics design, SEO, Social media followers, Admob self-clicking. visit our contact page to get in touch with us or WhatsApp (+234) 08114313795.

Ikuesan Oluwaseun is the CEO of Ofofonobs and a Full-stack Developer. Ofofonobs.com is a Nigerian digital media and news platform launched in July 2020. Kindly check our contact page to get in touch with me.

Advertisement
Click to comment

Leave a Reply

Your email address will not be published.

Entertainment

Blake Snell Net Worth Blake Snell Net Worth
Entertainment2 months ago

Blake Snell Net Worth

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItWhat is Blake...

Beenie Man Net Worth Beenie Man Net Worth
Entertainment2 months ago

Beenie Man Net Worth

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItWhat is Beenie...

How to load an already used recharge card How to load an already used recharge card
Entertainment3 months ago

See how to load an already used recharge card (Topsecrets)

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItHave you ever...

Nigerians on Twitter find humour in Oxlade's leaked s*x tape Nigerians on Twitter find humour in Oxlade's leaked s*x tape
Entertainment3 months ago

Nigerians on Twitter find humour in Oxlade’s leaked s*x tape

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItFull video of...

Oxlade S*x Tape With Unidentified Lady Breaks Internet Oxlade S*x Tape With Unidentified Lady Breaks Internet
Breaking News3 months ago

Oxlade S*x Tape With Unidentified Lady Breaks Internet

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItFull video of...

Oba Solomon Agbaye Biography, Career, Wife, Net Worth Oba Solomon Agbaye Biography, Career, Wife, Net Worth
Entertainment3 months ago

Oba Solomon Agbaye Biography, Career, Wife, Net Worth

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItAdelana Adekunle Solomon,...

Nigerians react to Obi Cubana’s first son, Alex’s grades in school Nigerians react to Obi Cubana’s first son, Alex’s grades in school
Entertainment4 months ago

Nigerians react to Obi Cubana’s first son, Alex’s grades in school

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItObi Cubana’s first...

Nigerians worry over size of Bobrisky’s head in new video Nigerians worry over size of Bobrisky’s head in new video
Entertainment4 months ago

Nigerians worry over size of Bobrisky’s head in new video

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItControversial crossdresser, Bobrisky has sparked...

Man Arrested For Advertising Fake Eggs On Social Media Man Arrested For Advertising Fake Eggs On Social Media
Entertainment4 months ago

Man Arrested For Advertising Fake Eggs On Social Media

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItA 24-year-old man,...

Scream Director Teases Sam’s Mom’s Identity Scream Director Teases Sam’s Mom’s Identity
Entertainment4 months ago

Scream Director Teases Sam’s Mom’s Identity

Share our content or if you have any questions about an article, leave us a comment. TwitterFacebookGoogle+BufferLinkedInPin ItIn a recent...

ADVERTISEMENT