找到没有清理回调函数的Customizer设置。 wordpress主题检查插件

时间:2015-01-08 13:19:32

标签: wordpress themes

我已将'sanitize_callback'添加到每个add_setting()功能但仍显示错误。请任何人检查我的代码片段并给我建议

function bookish_customize_register( $wp_customize ) {

$wp_customize->get_setting( 'blogname' )->transport        = 'postMessage';
$wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';

$wp_customize->remove_control( 'header_textcolor' );
$wp_customize->remove_control( 'background_color' );
$wp_customize->remove_section( 'background_image' );
$wp_customize->remove_section( 'header_image' );

/*-----------------------------------------------------------*
 * Assent Color  section
 *-----------------------------------------------------------*/ 

$wp_customize->add_setting(
    'tcx_link_color',
    array(
        'default'     => '#000000',
        'sanitize_callback' => 'bookish_sanitize_text',
        'transport'   => 'postMessage',

    )
);

$wp_customize->add_control(
    new WP_Customize_Color_Control(
        $wp_customize,
        'link_color',
        array(
            'label'      => __( 'Link Color', 'tcx' ),
            'section'    => 'colors',
            'settings'   => 'tcx_link_color'
        )
    )
);
/*-----------------------------------------------------------*
 * Defining General Setting  section
 *-----------------------------------------------------------*/

$wp_customize->add_section(
    'bookish_general_setting',
    array(
        'title'     => 'General Settings',
        'priority'  => 1

    )
);



$wp_customize->add_setting(
            'bookish-logo',
            array(
                'default' => '',
                'sanitize_callback' => 'esc_url_raw',
                'transport'   => 'postMessage',

            )
        );

$wp_customize->add_control(
    new WP_Customize_Image_Control(
        $wp_customize,
        'bookish-logo',
        array(
            'label'    => __( ' Logo', 'bookish' ),
            'section'  => 'bookish_general_setting',
            'settings' => 'bookish-logo',
            'priority' => 1

        )
    )
);

$wp_customize->add_setting(
        'bookish-retina-logo',
        array(
            'default' => '',
            'sanitize_callback' => 'esc_url_raw',
            'transport'   => 'postMessage',
        )
    );

$wp_customize->add_control(
    new WP_Customize_Image_Control(
        $wp_customize,
        'bookish-retina-logo',
        array(
            'label'    => __( 'Retina Logo', 'bookish' ),
            'section'  => 'bookish_general_setting',
            'settings' => 'bookish-retina-logo',
            'priority' => 2

        )
    )
);

$wp_customize->add_setting(
    'bookish-favicon',
    array(
        'default'  => '',
        'sanitize_callback' => 'esc_url_raw',
        'transport'   => 'postMessage',

    )
);

$wp_customize->add_control(
    new WP_Customize_Image_Control(
        $wp_customize,
        'bookish-favicon',
        array(
            'label'    => __( ' Favicon', 'bookish' ),
            'section'  => 'bookish_general_setting',
            'settings' => 'bookish-favicon',
            'priority' => 3

        )
    )
);

$wp_customize->add_setting(
    'bookish-avatar',
    array(
        'default'          => '',
        'sanitize_callback' => 'esc_url_raw',
        'transport'   => 'postMessage',

    )
);

$wp_customize->add_control(
    new WP_Customize_Image_Control(
        $wp_customize,
        'bookish-avatar',
        array(
            'label'    => __( 'Avatar', 'bookish' ),
            'section'  => 'bookish_general_setting',
            'settings' => 'bookish-avatar',
            'priority' => 4
        )
    )
);

$wp_customize->add_setting(
    'bookish-retina-avatar',
    array(
        'default'          => '',
        'sanitize_callback' => 'esc_url_raw',
        'transport'   => 'postMessage',

    )
);

$wp_customize->add_control(
    new WP_Customize_Image_Control(
        $wp_customize,
        'bookish-retina-avatar',
        array(
            'label'    => __( 'Retina Avatar', 'bookish' ),
            'section'  => 'bookish_general_setting',
            'settings' => 'bookish-retina-avatar',
            'priority' => 5
        )
    )
);

$wp_customize->add_setting(
    'bookish_profile_name',
    array(
        'default'    =>  'Vincent Doe',
        'sanitize_callback' => 'bookish_sanitize_text',
        'transport'  =>  'postMessage',
    )
);

$wp_customize->add_control(
    'bookish_profile_name',
    array(
        'section'   => 'bookish_general_setting',
        'label'     => 'Profile Name',
        'type'      => 'text'
    )
);

$wp_customize->add_setting(
    'bookish_profile_desc',
    array(
        'default'    =>  'I lie close to the earth, a thousand unknown plants are noticed by me: when I hear the buzz of the little world among the stalks.',
        'sanitize_callback' => 'bookish_sanitize_textarea',
        'transport'  =>  'postMessage',

    )
);

$wp_customize->add_control(
    'bookish_profile_desc',
    array(
        'section'   => 'bookish_general_setting',
        'label'     => 'Profile Description',
        'type'      => 'textarea'
    )
);

$wp_customize->add_section(
    'contact_setting',
    array(
        'title'     => 'Contact Info',
        'priority'  => 2
    )
);

$wp_customize->add_setting(
    'contact_heading',
    array(
        'default'    =>  'Get in touch',
        'sanitize_callback' => 'bookish_sanitize_text',
        'transport'  =>  'postMessage',
    )
);

$wp_customize->add_control(
    'contact_heading',
    array(
        'section'   => 'contact_setting',
        'label'     => 'Contact Heading',
        'type'      => 'text'
    )
);

$wp_customize->add_setting(
    'contact_email',
    array(
        'default'    =>  '',
        'sanitize_callback' => 'bookish_sanitize_email',
        'transport'  =>  'postMessage',
    )
);

$wp_customize->add_control(
    'contact_email',
    array(
        'section'   => 'contact_setting',
        'label'     => 'Email',
        'type'      =>  'email'

    )
);

$wp_customize->add_setting(
    'contact_phone',
    array(
        'default'    =>  '',
        'sanitize_callback' => 'bookish_sanitize_number',
        'transport'  =>  'postMessage',
    )
);

$wp_customize->add_control(
    'contact_phone',
    array(
        'section'   => 'contact_setting',
        'label'     => 'Phone Number',
        'type'      => 'text'
    )
);

}

add_action( 'customize_register', 'bookish_customize_register', 11 );

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function bookish_customize_preview_js() {
    wp_enqueue_script( 'bookish_customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-preview' ), '20130508', true );
}
add_action( 'customize_preview_init', 'bookish_customize_preview_js' );

function bookish_sanitize_text( $str ) {
    return sanitize_text_field( $str );
} 

function bookish_sanitize_textarea( $text ) {
    return esc_textarea( $text );
} 

function bookish_sanitize_number( $int ) {
    return absint( $int );
} 

function bookish_sanitize_email( $email ) {
    if(is_email( $email )){
        return $email;
    }else{
        return '';
    }
} 

function bookish_sanitize_file_url( $url ) {
    $output = '';
    $filetype = wp_check_filetype( $url );
    if ( $filetype["ext"] ) {
        $output = esc_url( $url );
    }
    return $output;
}

function tcx_customizer_css() {
?>
     <style type="text/css">

        .TopBanner { background-color: <?php echo get_theme_mod( 'tcx_link_color' ); ?>!important; }

     </style>
<?php
}
add_action( 'wp_head', 'tcx_customizer_css' );

先谢谢

4 个答案:

答案 0 :(得分:4)

如果遇到上述问题,我有新的解决方案

'sanitize_callback' => 'esc_attr',

然后在主题检查器上检查主题后,我不能在那里得到任何错误 我希望帮助一些人。

答案 1 :(得分:2)

安装主题检查插件,然后修改plugins / theme-check / checks / customizer.php文件

添加echo "$file_path: $match ";

在此部分代码之后:

`if ( false === strpos( $match, 'sanitize_callback' ) && false === strpos( $match, 'sanitize_js_callback' ) ) {
$this->error[] = '<span class="tc-lead tc-required">' .       __('REQUIRED','theme-check') . '</span>: ' . __( 'Found a Customizer setting that did not have a sanitization callback function. Every call to the <strong>add_setting()</strong> method needs to have a sanitization callback function passed.', 'theme-check' );`

这应该指示导致错误的文件和代码片段。

答案 2 :(得分:0)

我知道对此发表评论可能已经很老了,但我认为答案将是:

'sanitize_callback' => 'sanitize_hex_color',

我希望这可以帮助其他人。 快乐的编码:)

答案 3 :(得分:-1)

你可以像这样添加函数回调__return_false

'sanitize_callback' => '__return_false'

并添加过滤功能

 function __return_false_value($value) {
    return $value;
}

add_filter('__return_false', '__return_false_value');