对多个字段执行一次清理回调

时间:2021-08-26 作者:Shakti Goyal

希望大家都做得好!如何对多个字段进行一次清理回调?

这是我的密码

add_action(\'admin_init\',\'settings\');


function settings(){
add_settings_section(\'menu_first_section\',null,null,\'plugin-menu\');

add_settings_field(\'field_1\',\'Field 1\',\'field_1_html\',\'plugin-menu\',\'menu_first_section\');
register_setting(\'option_group_1\',\'field_1\',array(\'sanitize_callback\' => \'sanitize_one\',\'default\'=> \'\'));

add_settings_field(\'field_2\',\'Field 2\',\'field_2_html\',\'plugin-menu\',\'menu_first_section\');
register_setting(\'option_group_2\',\'field_2\',array(\'sanitize_callback\' => \'sanitize_two\',\'default\'=> \'\'));

}

function sanitize_one($input) {
  // validation

}

function sanitize_two($input) {
   // validation
}
如何对多个字段进行一次清理回调,然后访问其值($input)?

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

如何对多个字段进行一次清理回调,然后访问它的值($input)?

对于具有相同验证逻辑的字段,只需设置sanitize_callback 到相同的回调,例如,如果field_1field_2 是单行和纯文本字段(即不允许HTML),您可以使用\'sanitize_callback\' => \'sanitize_text_field\' 对于这两个字段。(参见sanitize_text_field())

但对于需要访问选项名称的自定义回调(可能是因为回调在不同字段类型之间共享),则:

选项1:手动挂钩sanitize_option_<option name>

register_setting() 使用sanitize_option_<option name> filter 注册sanitize回调,但函数调用add_filter() 将第四个参数设置为默认值(即。1) 因此,只有一个参数将传递给sanitize回调,因此它不会收到选项名称($option 参数)。

因此,如果要接收选项名称,请不要设置sanitize_callback 参数,并手动钩住上述过滤器。例如。

// Shared sanitize callback:
// Just a basic example of validating based on the option/input name.
function sanitize_any( $value, $option ) {
    switch ( $option ) {
        case \'field_1\':
            if ( empty( $value ) ) {
                add_settings_error( \'my_plugin_messages\', \'missing_field_1\',
                    \'Please enter a valid value into Field 1.\' );

                // Use the current database value. This will cancel saving the option.
                $value = get_option( $option );
            }

            break;

        // .. your code ...
    }

    return $value;
}

// Register the settings:
register_setting( \'option_group_1\', \'field_1\', array( \'default\' => \'\' ) );
register_setting( \'option_group_2\', \'field_2\', array( \'default\' => \'\' ) );

// Hook on sanitize_option_field_1 and sanitize_option_field_2:
add_filter( \'sanitize_option_field_1\', \'sanitize_any\', 10, 2 );
add_filter( \'sanitize_option_field_2\', \'sanitize_any\', 10, 2 );

/* Sample <input>:
<input name="field_1" value="<?php echo esc_attr( get_option( \'field_1\' ) ); ?>">
*/

选项2:如果输入属于same option group (即相同的形式),您可以将它们存储在一个数组中,这意味着field_1field_2 将位于单个数据库选项中,例如my_fields. 例如:

// This function returns the default values for my_fields.
function my_fields_default_options() {
    return array(
        \'field_1\' => \'\',
        \'field_2\' => \'\',
    );
}

// The sanitize callback:
function sanitize_my_fields( $value ) {
    $value = (array) $value;

    if ( empty( $value[\'field_1\'] ) ) {
        add_settings_error( \'my_plugin_messages\', \'missing_field_1\',
            \'Please enter a valid value into Field 1.\' );

        // Use the current database value. This will cancel saving the option.
        $value = get_option( \'my_fields\' );
    }

    // .. your code ...

    return $value;
}

// Register the setting:
register_setting( \'my_option_group\', \'my_fields\', array(
    \'sanitize_callback\' => \'sanitize_my_fields\',
    \'default\'           => my_fields_default_options(),
) );

/* And then in the <input> name, use the array format. E.g.
<input name="my_fields[field_1]" value="<?php echo esc_attr(
    get_option( \'my_fields\', my_fields_default_options() )[\'field_1\']
); ?>">
*/

相关推荐