• I have this code for the customizer to add some select options:

    $wp_customize->add_setting( 'price1', array('default'=>'95'));
      $wp_customize->add_control( 'price1', array(
        'label' => 'price 1',
        'settings' =>  'price1',
        'section' => 'pricessection',
        'type'     => 'select',
            'choices'  => array(
                'reguler price'  => '95',
                'special price' => '65',
                'other price' => '35',
            ),
    ) );

    Now if i use this:

    echo get_theme_mod( 'price1' );
    In my theme i will get the value of the selected option.

    I want to be able to get the label of the selected option also (not the label of the setting). is it possible? how?

    Or in other words:
    How do i echo the “other price” and how do i echo the “35”?

    • This topic was modified 7 years, 11 months ago by Begin.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    All of the choices are only available when your section is open in the customizer. In any other context, only the saved selection is available.

    In practice this shouldn’t be a problem because the options would typically be dynamically generated. For example, the other price being 35 comes from some sort of product data. If someone were to select “other price”, you can just go back to the product data to get the price. Anything else about the setting should only matter in the customizer. All that should matter outside is what the eventual selection was.

    Thread Starter Begin

    (@bentalgad)

    Didn’t really get you. i don’t want the unselected options, i want the selected option, but i want to be able to echo the value and the label. (‘other price’ and ’35’). of course i can do something like if == ’35’ echo ‘other price’ but that would be a very long code if i need it for multiple options.

    Moderator bcworkz

    (@bcworkz)

    Outside of the customizer context, all that’s available is what’s returned by get_theme_mod(). The associated 35 is as inaccessible as the unselected options.

    In actual practice, you shouldn’t be hardcoding that 35 in the customizer. It should be coming from some other data so managers can update the pricing from time to time without editing code. Knowing the user selected “other price” in the “price1” setting should be enough to enable you to get the current price from wherever it came from to begin with. This isn’t any different than a normal drop down form field. The selected option value is passed back to the server, not the associated label.

    If you really would need some lengthy lookup table to work out what goes with “other price”, then you need to rethink your data schema.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Is it possible to get the label of a theme customizer option?’ is closed to new replies.