I am fairly new to WordPress.
I am currently using a plugin which requires me to add a custom script before the </body> tag.
How do I do it?
I am fairly new to WordPress.
I am currently using a plugin which requires me to add a custom script before the </body> tag.
How do I do it?
You have two options for how to add the required JavaScript. The code below should be added to the functions.php file of your WordPress theme (or child theme).
This assumes the WordPress theme has used the wp_footer() function correctly.
function addScriptTagToBody() {
?>
<script type="text/javascript">
// JavaScript here..
</script>
<?php
}
add_action( 'wp_footer', 'addScriptTagToBody' );
Alternatively, if you want to link to an external JavaScript file, you can use the following:
wp_enqueue_script(
'custom-script', // A unique name for the script
get_stylesheet_directory_uri() . '/custom-script.js', // Assumes the script is in your theme directory
array( 'jquery' ), // An array of dependencies for your script
'1', // Version number for your script
true // Add script just before </body> tag
);
The fifth argument for wp_enqueue_script() lets you specify whether the <script src=""></script> is added in the <head></head> or just before the </body> tag.
functions.php file. (functions.php, with an s).