注册设置时,传递该设置的santize回调:
register_setting(
\'my_setting_group\',
\'my_setting_name\',
// The next parameter is the validation callback
\'my_setting_validation\'
);
然后,在验证回调中,您可以允许您想要的任何内容。例如,在下一个代码段中
unfiltered_html
允许插入原始HTML代码;允许其他用户在帖子内容中插入相同的HTML标记:
function my_setting_validation( $input ) {
// Check for the field that we want to allow html
if( $input[\'ng_menu_html\'] ) {
if ( current_user_can(\'unfiltered_html\') ) {
$validated_input[\'ng_menu_html\'] = $input[\'ng_menu_html\'];
} else {
$validated_input[\'ng_menu_html\'] = stripslashes( wp_filter_post_kses( wp_slash( $input[\'ng_menu_html\'] ) ) ); // wp_filter_post_kses() expects slashed
}
} else {
// Sanitize here other fields with no HTML or whatever you want
}
return $validated_input;
}
最后,您需要使用
esc_attr()
设置字段值时:
function ng_html_callback() {
$options = get_option( \'my_setting_name\' );
if( !isset( $options[\'ng_html\'] ) ) $options[\'ng_html\'] = \'\';
echo \'<label for="ng_html">\' . _e( \'Insert additional HTML\', \'plugin\') . \'</label>\';
echo \'<input type="text" id="ng_html" name="my_settings[ng_html]" value="\' . esc_attr( $options[\'ng_html\'] ) . \'" placeholder="Add HTML">\';
}
}