Created
April 12, 2020 08:01
-
-
Save Alipio123/856b546b86c513d4c445f064dfd5346b to your computer and use it in GitHub Desktop.
Add Additional Javascript on any Theme. Located under Theme Customization
This file contains 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 | |
// File Security Check | |
if ( ! defined( 'ABSPATH' ) ) { exit; } | |
add_action( 'customize_register' , array( 'AdditionalJS_Customize' , 'register' ) ); | |
//echoing the scripts. | |
add_action( 'wp_head' , array( 'AdditionalJS_Customize' , 'header_output' ) ); | |
add_action( 'wp_body_open' , array( 'AdditionalJS_Customize' , 'bodyopen_output' ) ); | |
add_action( 'wp_footer' , array( 'AdditionalJS_Customize' , 'footer_output' ) ); | |
class AdditionalJS_Customize { | |
public static function register ( $wp_customize ) { | |
//Adding New Setting Section in Theme Customizer Name: Additional Javascript | |
$wp_customize->add_section( 'theme_javascript', | |
array( | |
'title' => __( 'Additional Javascript', 'hellobabychild' ), | |
'priority' => 9999, | |
'description' => __('Allows you to add additional Javascript in the Theme. Please include <script></script>`', 'hellobabychild'), | |
) | |
); | |
//Additional Javascript List of settings | |
//JS inside head, before closing of body tag and below body tag | |
$wp_customize->add_setting( 'js_head', | |
array( | |
'default' => '', | |
) | |
); | |
$wp_customize->add_setting( 'js_bodyopen', | |
array( | |
'default' => '', | |
) | |
); | |
$wp_customize->add_setting( 'js_footer', | |
array( | |
'default' => '', | |
) | |
); | |
//creating controls for Additional Javascript | |
$wp_customize->add_control( new WP_Customize_Code_Editor_Control( $wp_customize, 'theme_js_head', | |
array( | |
'label' => __( 'Javascript inside <head> tag', 'hellobabychild' ), | |
'description' => __( 'You can place your Google Analytic code here.', 'hellobabychild' ), | |
'settings' => 'js_head', | |
'section' => 'theme_javascript', | |
) | |
)); | |
$wp_customize->add_control( new WP_Customize_Code_Editor_Control( $wp_customize, 'theme_js_bodyopen', | |
array( | |
'label' => __( 'Javascript after the opening <body> tag', 'hellobabychild' ), | |
'description' => __( 'You can place your facebook pixel code here.', 'hellobabychild' ), | |
'settings' => 'js_bodyopen', | |
'section' => 'theme_javascript', | |
) | |
)); | |
$wp_customize->add_control( new WP_Customize_Code_Editor_Control( $wp_customize, 'theme_js_footer', | |
array( | |
'label' => __( 'Javascript before the closing </body> tag', 'hellobabychild' ), | |
'description' => __( 'You can place your custom javascript code here.', 'hellobabychild' ), | |
'settings' => 'js_footer', | |
'section' => 'theme_javascript', | |
) | |
)); | |
} | |
public static function header_output() { | |
$headjs = get_theme_mod( 'js_head' ); | |
if( ''===$headjs ) { | |
return; | |
} | |
echo $headjs; | |
} | |
public static function bodyopen_output() { | |
$after_body_js = get_theme_mod( 'js_bodyopen' ); | |
if( ''=== $after_body_js ) { | |
return; | |
} | |
echo $after_body_js; | |
} | |
public static function footer_output() { | |
$footerjs = get_theme_mod( 'js_footer' ); | |
if( ''=== $footerjs ) { | |
return; | |
} | |
echo $footerjs; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Cool, here's how I would write that class:
And then add this somewhere in the theme to run it: