Connect with us
Padiwise Banner

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, VTU Platform and more. visit our contact page to get in touch with us or WhatsApp (+234) 08114313795. data
Click to comment

Leave a Reply

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

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

Entertainment

Entertainment1 month ago

John Okafor Mr. Ibu Biography: Net Worth, Family, Movies And More

John Okafor Mr. Ibu Biography & Net Worth ( Fully Updated ) John Okafor is more commonly known as Mr....

Entertainment1 month ago

Brely Evans Biography: Husband, Books, Age, Career

Brely Evans, who was born on December 9, 1972, in Oakland, California, and raised in Milwaukee, Wisconsin, is a multi-talented...

Entertainment1 month ago

Meet Gbadebo Rhodes Vivour, The Labour Party Governorship Candidate In Lagos State 

Gbadebo Rhodes Vivour profile Gbadebo Chinedu Rhodes-Vivour, popularly known as GRV, is a Nigerian architect, activist, and politician born on...

Entertainment1 month ago

Yinka Okeleye Biography: Age, State Of Origin, Education, Wife, Songs, Girlfriend

Adeyinka Okeleye is a talented Gospel singer and worship leader from Ogun State, Nigeria. He currently serves as the Music...

Entertainment1 month ago

Sunmisola Agbebi Biography: Age, Songs, Family, Boyfriend, Net worth, Education

Sunmisola Elizabeth Agbebi is a talented Nigerian gospel singer with a distinct and energetic style. She is 24 years old...

Entertainment1 month ago

Meet Beatrice Itubo, The LP Governorship Candidate In Rivers State

Beatrice Itubo, the Labour Party gubernatorial candidate for the 2023 governorship election in Rivers State was born on Sunday, June...

Entertainment1 month ago

Meet Somachi Chris Asoluka , The New CEO Of Tony Elumelu Foundation

Somachi Chris Asoluka was announced as the new Chief Executive Officer of Tony Elumelu Foundation on January 9, 2023. She...

Entertainment1 month ago

Tasha Cobbs Leonard Christmas Family Photos With Husband And Children

It’s December 26 and everyone especially married people is sharing their beautiful family photos with matching pajamas . Well, famous...

Entertainment1 month ago

10 THINGS YOU SHOULD KNOW ABOUT DEBORAH PAUL ENENCHE

Deborah Paul Enenche was born on 28 of March 1995 2. She is a Nigerian top-rated gospel singer and first...

Entertainment1 month ago

Lillian Mutambo Biography: Age, Investment , Marriage And More

Lillian Mutambo is a 39 year -old Zambian social worker and youtuber based in the United Kingdom. Music Career Lily...