数据验证/清理回调函数

时间:2015-06-09 04:30:12

标签: wordpress sanitization

我在WP主题的自定义程序中添加了一个部分,允许用户更改主题第一页上显示的类别。但是,在使用Theme Check插件进行检查时,它返回了以下错误:

REQUIRED:找到没有清理回调函数的Customizer设置。每次调用add_setting()方法都需要传递一个清理回调函数。 我不知道如何将此功能添加到我的代码中。如果您能提供帮助,请输入以下代码:

http://pastebin.com/xksf3vWd

提前致谢!

1 个答案:

答案 0 :(得分:1)

默认情况下,Customizer不处理用户输入值的验证和清理。因此,在将这些值保存到数据库之前,必须对这些值进行清理。

WP_Customizer对象的add_setting()方法接受'sanitize_callback'参数,该参数可用于指定清理回调。因此,在每次add_setting()调用中,添加清理回调函数。

$wp_customize->add_setting( 'first_category', array(
    'default'           => 'Uncategorized',     // The default category name.
    'sanitize_callback' => 'ys_sanitize_category',  // Sanitize callback function name
) );

Sanitize回调函数:

function ys_sanitize_category( $category ) {
    if ( ! in_array( $category, array( 'Uncategorized', 'Blogposts', 'News' ) ) ) { // Add the names of your categories here. Use get_categories() to fetch them dynamically.
        $category = 'Uncategorized';
    }
    return $category;
}
相关问题