Skip to content

Instantly share code, notes, and snippets.

@smchenrybc
Last active April 28, 2025 16:38
Show Gist options
  • Save smchenrybc/5d3bf74631908ccbb7416c5f55087b8e to your computer and use it in GitHub Desktop.
Save smchenrybc/5d3bf74631908ccbb7416c5f55087b8e to your computer and use it in GitHub Desktop.
Inject <script> added to custom field to header and footer
<?php
/**
* Add script code to page's header
*/
function bc_add_page_script_code_header() {
$script_raw = get_field( 'script_code', get_the_ID(), false );
$script_location = get_field( 'script_location', get_the_ID() );
if ( !is_admin() && $script_raw && $script_location == 'header' ) {
$script_decoded = htmlspecialchars_decode( $script_raw );
echo $script_decoded;
}
}
add_action( 'wp_head', 'bc_add_page_script_code_header' );
/**
* Add script code to page's footer
*/
function bc_add_page_script_code_footer() {
$script_raw = get_field( 'script_code', get_the_ID(), false );
$script_location = get_field( 'script_location', get_the_ID() );
if ( !is_admin() && $script_raw && $script_location == 'footer' ) {
$script_decoded = htmlspecialchars_decode( $script_raw );
echo $script_decoded;
}
}
add_action( 'wp_footer', 'bc_add_page_script_code_footer' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment