Skip to content

Instantly share code, notes, and snippets.

@yratof
Created September 1, 2021 12:08
Show Gist options
  • Save yratof/96cc8b646cf2dae39e93d24d56471fbf to your computer and use it in GitHub Desktop.
Save yratof/96cc8b646cf2dae39e93d24d56471fbf to your computer and use it in GitHub Desktop.
Add scripts to customizer for wordpress
<?php
/* Customiser script */
add_action( 'customize_register', 'custom_editor' );
function custom_editor( $wp_customize ) {
// Analytics section
$wp_customize->add_section('analytics_section', array(
'title' => __( 'Analytics', 'tuesday' ),
'description' => __( 'Enable tracking and analytics by placing your script tags in the correct location. <small><strong>Note:</strong> All scripts must be self-containing &lt;script&gt;&lt;/script&gt;, otherwise they will just print the code onto the website.</small>', 'tuesday' ),
'priority' => 160,
'transport' => 'postMessage',
));
// head scripts
$wp_customize->add_setting('analytics_head', array(
'default' => '',
'capability' => 'edit_theme_options',
));
$wp_customize->add_control('analytics_head', array(
'label' => __( 'Scripts in the &lt;head&gt;', 'tuesday' ),
'description' => __( 'Place script tags in the &lt;head&gt;', 'tuesday' ),
'type' => 'textarea',
'input_attrs' => [
'data-name' => __( 'Head', 'tuesday' ),
'placeholder' => __( '&lt;script&gt;google.analytic.manager...', 'tuesday' ),
],
'section' => 'analytics_section',
'settings' => 'analytics_head',
));
$wp_customize->add_setting('analytics_footer', array(
'default' => '',
'capability' => 'edit_theme_options',
));
$wp_customize->add_control('analytics_footer', array(
'label' => __( 'Scripts in the &lt;footer&gt;', 'tuesday' ),
'description' => __( 'Place scripts in the footer', 'tuesday' ),
'type' => 'textarea',
'input_attrs' => [
'data-name' => __( 'Footer', 'tuesday' ),
'placeholder' => __( '&lt;script&gt;RUM.manager...', 'tuesday' ),
],
'section' => 'analytics_section',
'settings' => 'analytics_footer',
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment