如何在SANITIZE函数中获取INPUT_ATTRS?

时间:2018-07-11 作者:Michael

当我试图input_attrs 在sanitize函数中,我总是会出现如下错误:
试图获取中非对象的属性

怎么做
提前谢谢。

控制等级:

class MY_Customize_Number_Control extends WP_Customize_Control {

    public $type = \'number\';

    /**
     * Render the control in the customizer
     *
     * @since 1.0
     */
    public function render_content() {
        $input_id = \'_customize-input-\' . $this->id;
        $description_id = \'_customize-description-\' . $this->id;
        ?>
        <?php if ( ! empty( $this->label ) ) : ?>
            <label for="<?php echo esc_attr( $input_id ); ?>" class="customize-control-title"><?php echo esc_html( $this->label ); ?></label>
        <?php endif; ?>
        <?php if ( ! empty( $this->description ) ) : ?>
            <span id="<?php echo esc_attr( $description_id ); ?>" class="description customize-control-description"><?php echo $this->description; ?></span>
        <?php endif; ?>

        <input type="number" id="<?php echo esc_attr( $input_id ); ?>" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->input_attrs(); ?> <?php $this->link(); ?>>
        <?php
    }

}
消毒:

function my_sanitize_number( $input, $setting ) {
    $input_attrs = $setting->manager->get_control( $setting->id )->input_attrs;
    $min = $input_attrs[\'min\'] ? $input_attrs[\'min\'] : \'\';
    $max = $input_attrs[\'max\'] ? $input_attrs[\'max\'] : \'\';

    if ( isset( $input ) && is_numeric( $input ) ) {
        if( is_array( $input_attrs ) ) {
            if ( isset( $min ) && is_numeric( $min ) ) {
                if ( $input < $min ) {
                    $input = $min;
                }
            }
            if ( isset( $max ) && is_numeric( $max ) ) {
                if ( $input > $max ) {
                    $input = $max;
                }
            }
        }
        return $input;
    } else {
        return $setting->default;
    }
}
控制:

$wp_customize->add_setting(
    \'my_custom_num\', array(
        \'default\'           => 5,
        \'sanitize_callback\' => \'my_sanitize_number\',
        \'transport\'         => \'refresh\'
    )
);
$wp_customize->add_control(
    new MY_Customize_Number_Control(
        $wp_customize,
        \'custom_num\',
        array(
            \'settings\'    => \'my_custom_num\',
            \'priority\'    => 6,
            \'section\'     => \'my_section\',
            \'label\'       => esc_html__( \'Number of post to display\', \'mytheme\' ),
            \'description\' => esc_html__( \'Choose how many posts to display\', \'mytheme\' ),
            \'type\'        => \'number\',
            \'input_attrs\' => array(
                \'min\' => 0,
                \'max\' => 20
            )
        )
    )
);

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

您的控件已命名custom_num 但您的设置名为my_custom_num. 修改设置的sanitize 使用前者的函数:

$input_attrs = $setting->manager->get_control( \'custom_num\' )->input_attrs;
另请参见Customize Input Validity Constraints 插件,您可以在其中查看如何obtain the control for a given setting 无需硬编码:

$controls = array();
foreach ( $setting->manager->controls() as $control ) {
    if ( in_array( $setting, $other_control->settings, true ) ) {
        $controls[] = $control;
    }
}
if ( empty( $controls ) ) {
    return;
}
如果$control 不是null 然后它与此关联$setting. 但请注意,一个设置可能与任何控件都没有关联,也可能与多个设置关联,因此您应该考虑这些情况。

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register