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

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.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Entertainment

Hyundai Veloster N TCR Makes World Debut in Detroit Hyundai Veloster N TCR Makes World Debut in Detroit
Entertainment3 weeks ago

Hyundai Veloster N TCR Makes World Debut in Detroit

Just before it sets of to conquer the race tracks of the 2019 International Motor Sports Association Michelin Pilot Challenge series,...

Reconditioned 1974 Honda CB750 Looks as If It Came Straight Out of a Museum Reconditioned 1974 Honda CB750 Looks as If It Came Straight Out of a Museum
Entertainment3 weeks ago

Reconditioned 1974 Honda CB750 Looks as If It Came Straight Out of a Museum

You might find it hard to believe, but this 1974 Honda CB750 was nothing more than a non-running project a few years...

Beautiful Music for Successful Wedding Videos Beautiful Music for Successful Wedding Videos
Entertainment3 weeks ago

Beautiful Music for Successful Wedding Videos

When it comes to wedding videos, ensuring you have the right music at hand can make all the difference. Any...

Why it’s better to have a child with someone you don’t love – Yvonne Nelson spills Why it’s better to have a child with someone you don’t love – Yvonne Nelson spills
Entertainment2 months ago

Why it’s better to have a child with someone you don’t love – Yvonne Nelson spills

Popular Ghanaian actress, Yvonne Nelson has revealed why it’s better and advisable for people to have children with people they are not...

Wizkid’s Made In Lagos Hits Half A Billion Streams On Apple Music Wizkid’s Made In Lagos Hits Half A Billion Streams On Apple Music
Entertainment2 months ago

Wizkid’s Made In Lagos Hits Half A Billion Streams On Apple Music

Nigerian superstar, Wizkid has recorded another feat with his Made In Lagos Deluxe album version with a milestone of 500M...

Blake Snell Net Worth Blake Snell Net Worth
Entertainment4 months ago

Blake Snell Net Worth

What is Blake Snell’s net worth? Net Worth: $10 Million Age: 28 Born: December 4, 1992 Country of Origin: United...

Beenie Man Net Worth Beenie Man Net Worth
Entertainment4 months ago

Beenie Man Net Worth

What is Beenie Man’s net worth? Net Worth: $4 Million Age: 47 Born: August 22, 1973 Country of Origin: Jamaica...

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

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

Have you ever gotten free Airtime Credit from MTN? If you haven’t, you’re still dulling yourself. This article will teach...

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

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

Full video of a fast-rising singer identified as Oxlade smashing an unidentified lady breaks the internet, Brand Spur Nigeria reports. In the tape...

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

Oxlade S*x Tape With Unidentified Lady Breaks Internet

Full video of a fast-rising singer identified as Oxlade smashing an unidentified lady breaks the internet, Brand Spur Nigeria reports. The 24-year-old whose...

ADVERTISEMENT