Skip to content

Instantly share code, notes, and snippets.

@alinademi
Forked from yratof/scripts.php
Created March 3, 2023 00:34
Show Gist options
  • Save alinademi/bfe942a376a2bd2c765c075fe8f270ad to your computer and use it in GitHub Desktop.
Save alinademi/bfe942a376a2bd2c765c075fe8f270ad 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