1

When I try to get input_attrs in the sanitize function I have always errors like:
Trying to get property of non-object in ...

How to do it ?
Thanks in advance.

Control class:

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
    }

}

Sanitize:

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;
    }
}

Control:

$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
            )
        )
    )
);
0

Your control is named custom_num but your setting is named my_custom_num. Modify your setting's sanitize function to use the former:

$input_attrs = $setting->manager->get_control( 'custom_num' )->input_attrs;

See also the Customize Input Validity Constraints plugin, where you can see how to obtain the control for a given setting without having to hard-code it:

$controls = array();
foreach ( $setting->manager->controls() as $control ) {
    if ( in_array( $setting, $other_control->settings, true ) ) {
        $controls[] = $control;
    }
}
if ( empty( $controls ) ) {
    return;
}

If $control is not null then it is associated with this $setting. Do note however that a setting may not be associated with any control or it may be associated with multiple settings, so you should account for those cases.

  • Thanks, it works! I have one more querstion, how to get control id ? I need solution for multiple controls. – Michael Jul 11 '18 at 15:18
  • @Michael I added some code to show how to obtain the control(s) that are associated with a given setting. – Weston Ruter Jul 11 '18 at 15:27
  • Thanks for help. I have another solution: $control_id = str_replace( 'myprefix_', '', $setting->id ); $control = $setting->manager->get_control( $control_id ); $input_attrs = $control->input_attrs; – Michael Jul 11 '18 at 16:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.