0

I'm trying to put a line break in my input text field in customizer. I'm using a sanitization function like this

function sanitize_text( $input ) {
   $allowed_html = array(
     'br' => array(),
   );

   return wp_kses( $input, $allowed_html );
}

I also using php strip_tags but neither of them works.
Thanks in advance

0

I tried the code you shared above and everything appears to be working as intended.

Are you sure you're creating and referencing the sanitize method correctly for the control?

Here is the full code added to functions.php I used:

    function test_sanitize_text( $input ) {
        $allowed_html = array(
            'br' => array(),
        );

        return wp_kses( $input, $allowed_html );
    }

    function test_customizer( $wp_customize ) {
        $wp_customize->add_setting( 'themeslug_text_setting_id', array(
            'capability' => 'edit_theme_options',
            'default' => 'Lorem Ipsum',
            'sanitize_callback' => 'test_sanitize_text',
        ) );

        $wp_customize->add_control( 'themeslug_text_setting_id', array(
            'type' => 'text',
            'section' => 'title_tagline', // Add a default or your own section
            'label' => __( 'Custom Text' ),
            'description' => __( 'This is a custom text box.' ),
        ) );
    }

I went to the customizer to site identity section where the input was added and the following code and saved the customizer: <div class="htmls"><p>testing</p><br><br><p>another</p></div>

Resulting value of $mod = get_theme_mod( 'themeslug_text_setting_id' );:

string(22) "testing<br><br>another"

| improve this answer | |
  • 1
    Thanks. I had the same result too, but my text was rendered like this "testing <br><br> another" ( in your test case ) and not line breaks. So I replaced the code rendering the text, from esc_html to wp_kses_post and It seems to work fine. Thanks anyway – Nguyen Viet Jul 24 '17 at 4:39
  • Yes definitely - esc_html will escape the tags making testing&lt;br&gt;&lt;br&gt;another and display the actual tags on a page. wp_kses_post is the way to go. Glad to hear you got it resolved! – Tim Elsass Jul 24 '17 at 4:53

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.