Using an if statement in the theme customizer page

I am slowly but surely chipping away at my first wordpress theme. I tried to make all of the aesthetics have different options, but well categorized so it is not overwhelming.

So in my theme customizer page I have different checkboxes that could distinguish different options for a certain components style. For instance I could have two checkboxes to distinguish what button type the user wants.

Read More

So my question is:

How can I automatically uncheck one checkbox if the another in it’s group is checked? I figure this will prevent me from having different options for the same element on the page checked and potentially messing something up. I figure if statements are probably the only way to go but I am unsure how to add them since I didn’t create the page, it is all done using the theme customizer API.

I am starting to think I should of used an options page instead, even though it is supposed to be a thing of the past and doesn’t provide live previews, but it seems like it would add more flexibility.

Related posts

1 comment

  1. Your description makes it sound like you need a radio button. Radio buttons by design are exclusive– only one button per button set can be checked at any one time.

    The Settings API supports radio boxes, though the documentation for using them is sparse. I had to dig around a bit, and experiment. The key is the second parameter of add_control. You need to pass 'type' =>radio’and provide achoices` array for the radio button values.

    function radio_controls_wpse_117203($wpc) {
    
      $wpc->add_setting(
        'radio_control_wpse_117203',
        array(
          'default' => 'hi',
        )
      );
    
      $wpc->add_section(
        'radio_section_wpse_117203',
        array(
          'title' => 'Radio WPSE 117203',
          'description' => 'A holder for our radio buttons.',
        )
      );
    
      $wpc->add_control(
        'radio_control_wpse_117203',
        array(
          'type' => 'radio',
          'label' => 'Salutation',
          'section' => 'radio_section_wpse_117203',
          'choices' => array(
            'hi' => 'Hi',
            'howdy' => 'Howdy',
          ),
        )
      );
    
    }
    add_action('customize_register','radio_controls_wpse_117203');
    

Comments are closed.