Skip Navigation

[Resolved] Checkboxes – conditional test on a checkbox title

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 2 replies, has 2 voices.

Last updated by philippeS-4 1 year, 7 months ago.

Author
Posts
#2593531
Screenshot 2023-04-14 at 10.37.04.png

Hi there,
I have created a custom field checkboxes with mutliple chechboxes as per attached. In a view how can I do a conditional test of a particular checkbox (checkbox title) if it is checked or unchecked?

Thanks for your help,
Phil

#2593591

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Phil

The first thing you need to do is make sure that in the settings for your checkboxes field, all of the options store different values.

(In your screenshot, the first option stores value 1.)

This is because when you output the checkboxes field (e.g. with a types shortcode) it will output the saved values, and if all the options have the same value you won't be able to differentiate between them.

If there would only ever be one checkbox checked at a time then the solution would be straightforward, you can add a condition (either a conditional block or conditional shortcode, depending on which editor you are using) that checks the output of the checkboxes field and compares it to the stored value.

I suspect, though, that there could be several checkboxes checked, and you want to see if any one of the checked values matches your target.

The problem here is that the comparison operators for a conditional check only include comparisons such as = or != that test a single value, rather than what you require, e.g. CONTAINS.

So you would need to register a custom shortcode that does the test for you, returning true or false type values that your condition can check for.

Here is an example of such a shortcode, together with examples of how it is used in a conditional shortcode.

Note that to be able to use custom shortcodes in conditions, you must register the custom shortcode at Toolset > Settings > Front-end Content.

/**
 * Register shortcode to check whether a checkboxes option is among those checked
 * 
 * @att 'checked' - list of the values of the checked checkboxes
 * @att 'target' - which option to test if it is among those checked
 */
function checkboxes_contains_func($atts) {
  $atts = shortcode_atts( array(
      'checked' => '',
      'target' => ''
  ), $atts );
  $checked = isset( $atts['checked'] ) ? array_map( 'trim', explode(',', $atts['checked']) ) : null;
  $target = isset( $atts['target'] ) ? array_map( 'trim', explode(',', $atts['target']) ) : null;

  return !empty( array_intersect( $checked, $target ) );
}
add_shortcode("checkboxes_contains", "checkboxes_contains_func");

/* Example usage (wpv-conditional will match '1' with true and '0' with false)

[wpv-conditional if="( [checkboxes_contains checked='[types field='my-checkboxes' separator=','][/types]' target='2'] eq '1' )"]
<p>The checkbox with value = 2 of the my-checkboxes field is checked</p>
[/wpv-conditional]

[wpv-conditional if="( [checkboxes_contains checked='[types field='my-checkboxes' separator=','][/types]' target='2,3'] eq '1' )"]
<p>Either or both the checkboxes with values 2 and 3 of the my-checkboxes field are checked</p>
[/wpv-conditional]

*/
#2593641

Thank you Nigel much appreciated!