How to get input_attrs in the sanitize function?

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.

Leave a Comment