How to add Javascript in WordPress site

1.Simply Add this snippet in your functions.php file

function wpsnippetclub_javascript() {
    ?>
    <script>
        Your JavaScript
    </script>
    <?php
}
add_action('wp_head', 'wpsnippetclub_javascript');

2.Add the JS only in specific pages, this can help, because the code, doesn’t have to execute if not nececary.

function wpsnippetclub_javascript() { 
if (is_page ('42')) {
?>
<script>
Your JavaScript
</script>
<?php
}
}
add_action('wp_head', 'wpsnippetclub_javascript');
Was this article helpful?
YesNo

Leave a Comment