How to get control choices from $setting object passed to sanitize_callback

Example Code

Working with the Customizer API.

Setting added (example) like so:

    $wp_customize->add_setting( 'theme_oenology_options[' . $option_parameter['name'] . ']', array(
        'default'           => $option_parameter['default'],
        'type'              => 'option',
        'sanitize_callback' => 'oenology_sanitize_' . $option_parameter['sanitize']
    ) );

Control added (example) like so:

        $wp_customize->add_control( 
            'oenology_' . $option_parameter['name'], 
            $customizer_control_parameters 
        );

Note: $customizer_control_parameters is an array defined elsewhere. For select-type controls (select, radio, radio-image, etc.) it includes a 'choices' key.

Sanitize callback defined (example) like so:

function oenology_sanitize_select( $input ) {
    return sanitize_key( $input );
}

Everything works flawlessly in the Customizer. The control is displayed, the setting is configurable, and the sanitization works.

Problem

All good so far, but it would be better to whitelist the setting against pre-defined choices in $wp_customize->add_control(). Let’s use the second parameter passed to 'sanitize_callback', $setting, to get the 'choices' defined in the control!

Core reference:

return apply_filters( "customize_sanitize_{$this->id}", $value, $this );

Solution Attempts

function oenology_sanitize_select( $input, $setting ) {
    $input = sanitize_key( $input );
    $choices = array(); // See below
    return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
}

Problem: using either of the two methods below does not return the array of choices passed to $wp_customize->add_control():

  1. Using $wp_customize global object:
    global $wp_customize;
    $control = $wp_customize->get_control( $setting->id );
    $choices = $control->choices;
  2. Using $setting->manager object:
    $choices = $setting->manager->get_control( $setting->id )->choices;

In both cases, get_control( $setting->id ) returns a null, instead of an object.

Question

How do I get 'choices' from $wp_customize->get_control(), using the $this object passed to "customize_sanitize_{$this->id}"?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

This is correct:

$control = $setting->manager->get_control($setting->id);
$choices = $control->choices;

However, in your example, $setting->id will be theme_oenology_options[name].

So, get_control('theme_oenology_options[name]') will return null because there’s no control with that name. You gave the control the id of oenology_name instead.

I always give the same ID to settings and their matching controls for that reason.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x