-
-
Save aryanrajseo/87def7f05de9dff19f7f3967093b0bdb to your computer and use it in GitHub Desktop.
Adds an Additional JS control to the Customizer and outputs the JS in the site 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 | |
/** | |
* Additional JS Customizer Control | |
* | |
* @author Lee Anthony | |
* @link https://seothemes.com | |
* @copyright Copyright © 2018 SEO Themes | |
* @license GPL-2.0-or-later | |
*/ | |
add_action( 'customize_register', 'prefix_customize_register' ); | |
/** | |
* Adds an Additional JS setting to the Customizer. | |
* | |
* @since 1.0.0 | |
* | |
* @param $wp_customize | |
* | |
* @return void | |
*/ | |
function prefix_customize_register( $wp_customize ) { | |
$wp_customize->add_section( 'custom_js', array( | |
'title' => __( 'Additional JS', 'textdomain' ), | |
'priority' => 190, | |
) ); | |
$wp_customize->add_setting( 'custom_js', array( | |
'type' => 'option', | |
) ); | |
$wp_customize->add_control( new WP_Customize_Code_Editor_Control( $wp_customize, 'custom_html', array( | |
'label' => 'Additional JS', | |
'code_type' => 'javascript', | |
'settings' => 'custom_js', | |
'section' => 'custom_js', | |
) ) ); | |
} | |
add_action( 'wp_footer', 'prefix_customize_output' ); | |
/** | |
* Outputs Additional JS to site footer. | |
* | |
* @since 1.0.0 | |
* | |
* @return void | |
*/ | |
function prefix_customize_output() { | |
$js = get_option( 'custom_js', '' ); | |
if ( '' === $js ) { | |
return; | |
} | |
?> | |
<script type="text/javascript"> | |
jQuery(function ($) { | |
"use strict"; | |
<?php echo $js . "\n"; ?> | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment