Skip to content

Instantly share code, notes, and snippets.

@endurtech
Created September 3, 2022 10:53
Show Gist options
  • Save endurtech/8967ff1f4cecc1328de58b783573e731 to your computer and use it in GitHub Desktop.
Save endurtech/8967ff1f4cecc1328de58b783573e731 to your computer and use it in GitHub Desktop.
Insert this code into the functions.php file within your WordPress child-theme to inject any custom code into the BODY of your WordPress website just after the opening <body> tag.
<?php
// Inserting a Script into WordPress Body
// https://endurtech.com/insert-script-into-wordpress-body/
// wp_body_open action hook to inject a script
add_action( 'wp_body_open', 'custom_body_scripts', 10 );
function custom_body_scripts()
{
echo '<script>alert( "This is triggered from within the body of your website." );</script>';
}
// Alternatively, you can stop and restart PHP processing to inject your code
// which helps bypass potential issues with apostrophes or quotations
add_action( 'wp_body_open', 'custom_body_scripts', 10 );
function custom_body_scripts()
{
?>
<script>alert( "This is triggered from within the body of your website." );</script>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment