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.

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

1 Answer 1

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.

12
  • Still the same issue continues.
    – Rang
    Commented Feb 17, 2017 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
    Commented Feb 17, 2017 at 14:54
  • var_dump() output: bool(false)
    – Rang
    Commented Feb 17, 2017 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
    Commented Feb 17, 2017 at 15:02
  • Every time the value is return empty.
    – Rang
    Commented Feb 17, 2017 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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