Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aryanrajseo/87def7f05de9dff19f7f3967093b0bdb to your computer and use it in GitHub Desktop.
Save aryanrajseo/87def7f05de9dff19f7f3967093b0bdb to your computer and use it in GitHub Desktop.

Revisions

  1. @seothemes seothemes created this gist Jul 18, 2018.
    69 changes: 69 additions & 0 deletions customizer-additional-js.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    <?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

    }