Last active
April 28, 2025 16:38
-
-
Save smchenrybc/5d3bf74631908ccbb7416c5f55087b8e to your computer and use it in GitHub Desktop.
Inject <script> added to custom field to header and footer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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