我试过使用sanitize_text_field()
和esc_attr()
在将复选框数据的值保存到数据库时筛选复选框数据,但这会导致数据无法保存。
是什么原因造成的以及过滤输入的正确方法是什么checkbox
和radio
?
我试过使用sanitize_text_field()
和esc_attr()
在将复选框数据的值保存到数据库时筛选复选框数据,但这会导致数据无法保存。
是什么原因造成的以及过滤输入的正确方法是什么checkbox
和radio
?
我将使用filter\\u var()函数。它有一些预定义的过滤器,您可以根据所需的数据类型(如字符串、数字等)使用这些过滤器。
因此,要进行一些消毒:
$sanitizedNum = filter_var($yourVar, FILTER_SANITIZE_NUMBER_INT);
对于字符串,只需将“\\u NUM\\u INT”更改为“\\u string”。然后将它们包装到自定义函数中。
我的意思是checkbox或radio的值通常是一个整数值。如果是整数值,则将其设置为整数作为实心过滤器。
$checkbox = (int) $checkbox;
esc_attr
过滤固体。功能sanitize_text_field
还有一个过滤器,其他插件可以更改输出,可能对您的目标没有帮助。该函数更多用于过滤来自用户或数据库的输入。esc_attr
还有一个过滤器,但更符合您的要求。有关更多信息,请访问codex page about validation.
I have use this function it working.
/************************************************************************
************** How to sanitize checkbox*************************
************************************************************************/
function theme_slug_customizer( $wp_customize ) {
//your section
$wp_customize->add_section(
\'theme_slug_customizer_your_section\',
array(
\'title\' => esc_html__( \'Your Section\', \'theme_slug\' ),
\'priority\' => 150
)
);
//checkbox sanitization function
function theme_slug_sanitize_checkbox( $input ){
//returns true if checkbox is checked
return ( isset( $input ) ? true : false );
}
//add setting to your section
$wp_customize->add_setting(
\'theme_slug_customizer_checkbox\',
array(
\'default\' => \'\',
\'sanitize_callback\' => \'theme_slug_sanitize_checkbox\'
)
);
$wp_customize->add_control(
\'theme_slug_customizer_checkbox\',
array(
\'label\' => esc_html__( \'Your Setting with Checkbox\', \'theme_slug\' ),
\'section\' => \'theme_slug_customizer_your_section\',
\'type\' => \'checkbox\'
)
);
}
add_action( \'customize_register\', \'theme_slug_customizer\' );
/************************************************************************
************** How to sanitize radio box *************************
************************************************************************/
function theme_slug_customizer( $wp_customize ) {
//your section
$wp_customize->add_section(
\'theme_slug_customizer_your_section\',
array(
\'title\' => esc_html__( \'Your Section\', \'theme_slug\' ),
\'priority\' => 150
)
);
//radio box sanitization function
function theme_slug_sanitize_radio( $input, $setting ){
//input must be a slug: lowercase alphanumeric characters, dashes and underscores are allowed only
$input = sanitize_key($input);
//get the list of possible radio box options
$choices = $setting->manager->get_control( $setting->id )->choices;
//return input if valid or return default option
return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}
//add setting to your section
$wp_customize->add_setting(
\'theme_slug_customizer_radio\',
array(
\'sanitize_callback\' => \'theme_slug_sanitize_radio\'
)
);
$wp_customize->add_control(
\'theme_slug_customizer_radio\',
array(
\'label\' => esc_html__( \'Your Setting with Radio Box\', \'theme_slug\' ),
\'section\' => \'theme_slug_customizer_your_section\',
\'type\' => \'radio\',
\'choices\' => array(
\'one\' => esc_html__(\'Choice One\',\'theme_slug\'),
\'two\' => esc_html__(\'Choice Two\',\'theme_slug\'),
\'three\' => esc_html__(\'Choice Three\',\'theme_slug\')
)
)
);
}
add_action( \'customize_register\', \'theme_slug_customizer\' );
我的网站受到攻击,所以基本上所有的核心php文件都已损坏。只剩下数据库。现在我在本地主机中进行了尝试。如果我想从这个数据库恢复我的站点,域名是我唯一需要替换字符串的东西吗</我找到了这个教程-Replace string in database - 更改所有表中的域字符串,但几乎不知道将其放入何处以及如何在wordpress中触发它