WordPress定制器允许换行符

时间:2017-07-24 作者:Nguyen Viet

我试图在customizer中的输入文本字段中设置换行符。我正在使用这样的消毒功能

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

   return wp_kses( $input, $allowed_html );
}
我也使用php strip\\u标记,但它们都不起作用<提前谢谢

2 个回复
SO网友:Marcus Domingos

我能让它工作的唯一方法是:

功能。php

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

    $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.\' ),
    ) );
}
前面(例如index.php)

<?php $themeslug_text_setting_id = get_theme_mod(\'themeslug_text_setting_id\');
    if ($themeslug_text_setting_id) { 
        echo nl2br( esc_html( $themeslug_text_setting_id ) );
} ?>
Thesanitize_textarea_field 这是最基本的。

SO网友:Tim Elsass

我尝试了您在上面共享的代码,似乎一切都正常工作。

是否确实为控件正确创建和引用了清理方法?

下面是添加到函数的完整代码。我使用的php:

    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.\' ),
        ) );
    }
我转到customizer to site identity部分,其中添加了输入和以下代码,并保存了customizer:<div class="htmls"><p>testing</p><br><br><p>another</p></div>

的结果值$mod = get_theme_mod( \'themeslug_text_setting_id\' );:

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

结束

相关推荐