Skip to content

Instantly share code, notes, and snippets.

@seothemes
Last active July 20, 2018 03:19
Show Gist options
  • Save seothemes/872e16d08eaeece088e717044643d39e to your computer and use it in GitHub Desktop.
Save seothemes/872e16d08eaeece088e717044643d39e 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.
<?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' => '',
'description' => __( 'Add your custom JavaScript below', 'textdomain' ),
'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