Skip to main content

Add inline JS to WordPress footer

Adding JS to WordPress can be done in two ways. The first way is to enqueue the JS file.

The second way can be done by adding inline JS via a hook in functions.php – this is most likely used to initialize a script that is enqueued or add Google Tracking codes, Facebook pixels etc.

function my_code_snippet()
{ ?>

<script>
 alert('it works');
</script>


<?php
}
add_action('wp_footer', 'my_code_snippet', 100);

Note, the 100 is the priority of where the snippet should go. So, if you want to add a script to the bottom of the page underneath all of the enqueued JS files. Add a large

number to ensure it comes down the bottom.

Leave a Reply