Skip to content

Instantly share code, notes, and snippets.

@endurtech
Last active September 3, 2022 10:40
Show Gist options
  • Select an option

  • Save endurtech/0e1b6f8918c10a3ca73465533426a173 to your computer and use it in GitHub Desktop.

Select an option

Save endurtech/0e1b6f8918c10a3ca73465533426a173 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 HEADER of your WordPress website.
<?php
// Inserting a Script into WordPress Header
// https://endurtech.com/insert-script-into-wordpress-header/
// wp_head action hook to inject a script
add_action( 'wp_head', 'custom_header_scripts' );
function custom_header_scripts()
{
echo '<script>alert( "This is triggered from within the header 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_head', 'custom_header_scripts' );
function custom_header_scripts()
{
?>
<script>alert( "This is triggered from within the header of your website." );</script>
<?php
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment