1

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.

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.

2
  • It is though, because it uses the theme customizer api. I don't actually write any of the code to create the forms so I don't control it. I add which control I want and link it to a setting. What I really need to know is how can I gain control of the form controls so I can write the logic? Also some groups may need to boxes checked off while the other ones are unchecked. I don't know if radio buttons will help me there. Commented Oct 9, 2013 at 2:31
  • Have a look at this, wordpress.stackexchange.com/questions/4991/… , to toggle multiple checkbox states use jQuery.
    – Wyck
    Commented Oct 9, 2013 at 6:27

1 Answer 1

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');
2
  • I modified the customizer to use the radio buttons instead and it really helps. Thank you, I appreciate it. Commented Oct 10, 2013 at 16:57
  • It seems that the default value isn't working for me. I am not sure why. It works fine when I change the value in theme customizer and save it. It is only the default value. Do you know why this could be? Commented Nov 16, 2013 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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