2

Customizer Codes:

$wp_customize->add_setting (
    'script-code',
    array (
        'default' => esc_html__( 'Script Code', 'x' ),
        'sanitize_callback' => 'wp_kses_post'
    )
);

$wp_customize->add_control (
    new WP_Customize_Control (
        $wp_customize,
        'script-code',
        array (
            'label' => esc_html__( 'Script Code', 'x' ),
            'section' => 'script',
            'settings' => 'script-code',
            'type' => 'textarea',
            'priority' => 1
        )
    )
);

Photo from Customizer:

enter image description here

from codes for output:

<?php echo wp_kses_post(get_theme_mod('script-code')); ?>

from output, return the empty:

<main class="script-code">
</main>

How can I use script tag in Customizer textarea setting field ?

Thanks so much.

  • 1
    please explain your question a bit more.. – Arsalan Mithani Feb 17 '17 at 12:28
  • I can not get the <script>js codes</script> output of the textarea field on the wordpress customizer page. – Rang Feb 17 '17 at 12:55
  • need to see snapshot where actually you are putting your code – Arsalan Mithani Feb 17 '17 at 13:01
  • Of course, one moment. – Rang Feb 17 '17 at 13:04
  • @ArsalanMithani I added photo links. – Rang Feb 17 '17 at 13:18
0

This is because you are using wp_kses_post to sanitize the output data, try without it:

<?php echo get_theme_mod( 'script-code'); ?>

also remove it from here:

$wp_customize->add_setting (
    'script-code',
    array (
        'default' => esc_html__( 'Script Code', 'x' ),
        'sanitize_callback' => '' // remove wp_kses_post
    )
); 

also make sure you are using the right name of your theme mod you can check that in two ways:

1.- do a var_dump(get_theme_mods()); check there how is named.

2.- Inspect the HTML code of your control in the customizer

enter image description here

the id without the prefix customize-control- is the name of your theme mod.

  • Still the same issue continues. – Rang Feb 17 '17 at 14:43
  • can you var_dump(get_theme_mod( 'script-code')); and add that to your question, also the code that you are using to add this control and setting to the customizer are you using any sanitize_callback? – David Lee Feb 17 '17 at 14:54
  • var_dump() output: bool(false) – Rang Feb 17 '17 at 14:57
  • that means that theme-mod doesnt exists, can you var_dump(get_theme_mods()); and check the name of your theme mod i dont think its 'script-code' – David Lee Feb 17 '17 at 15:02
  • Every time the value is return empty. – Rang Feb 17 '17 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.