Skip Navigation

[Resolved] How to use conditional output with checkboxes

This support ticket is created 5 years ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Tagged: 

This topic contains 7 replies, has 2 voices.

Last updated by Shane 4 years, 12 months ago.

Assisted by: Shane.

Author
Posts
#1460299

I have checkboxes field in one of my post - languages. I use 'en' for English and 'de' for German.
How could I use it in conditional output?
So, let's say if I want to check it for value 'en' and then output "<p>English</p>" and similar for 'de' as "<p>German</p>"?

#1460547

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kirill,

Thank you for getting in touch.

In order to do this you just need to use our conditional output like this.
https://toolset.com/documentation/user-guides/views/conditional-html-output-in-views/checking-types-fields-and-custom-fields/#checking-radio-fields-values

The document above should allow you to do this.

Thanks,
Shane

#1460649

This is documentation about "radio" field. I have multiple checkbox.
If I compare it to exact value, it shows only one option. Let's say, if it is both "de" and "en" and I compare it to "en" it is return "false" as it is not equal "en" but both "de" and "en".
So, this is question how to check that "en" is one of values of this field?

#1460703

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kirill,

In this case no it won't be possible to check for both values with the conditionals.

Our conditional code can only work with single options only.

In order to run this in our conditional you will need to pre-evaluate it with some custom code to achieve the same results.

So a custom conditional would need to be written to resolve this.

Thanks,
Shane

#1463753

It is quite surprise due to very obvious usage... also it could be done by string compare as "if Field include 'value'", not as it now "if Field equal to 'value'"...
Do you have any documentation about "custom conditional would need to be written to resolve this." - what is it about and how it could be done?

#1463963

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kiril,

also it could be done by string compare as "if Field include 'value'", not as it now "if Field equal to 'value'".

Will your field checkboxes field only have 1 item selected at any given time ? Meaning can the user only select English or German ?

Or can they select both, because in my testing I find that the checkboxes will work in the conditional with only 1 value selected but breaks with multiple values. I've already escalated this issue for re-evaluation.

However we do not have any documentation on how to create the custom conditional shortcode.

If you have some php knowledge then you can use the tool in the link below to auto generate the shortcode but you will need to write the custom comparison logic for the shortcode to work.
hidden link

Thanks,
Shane

#1464029

Yes, exactly - if only one is selected condition works perfectly, if both - logic is broken. So, I was sure that this is a bug.

Regarding conditional shortcode - I understand how to write shortcode, but not understand how to use shortcode in condition or what do you mean as Conditional Shortcode

#1469649

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Kirill,

This took some time but I managed to draft up a custom shortcode to help with this. add the following to the custom shortcode settings in Toolset->Settings->Custom Code.

// Put the code of your snippet below this comment.

// Create Shortcode checkboxes_conditional
// Shortcode: [checkboxes_conditional checkbox="" separator=","]Content[/checkboxes_conditional]
function create_checkboxescondition_shortcode($atts, $content = null) {

	$atts = shortcode_atts(
		array(
			'checkbox' => '',
			'separator' => ',',
          	'value' => '',
		),
		$atts,
		'checkboxes_conditional'
	);

	$checkbox = $atts['checkbox'];
	$separator = $atts['separator'];
  	$value = $atts['value'];
  
  	$checkbox_arr = array();
  	$value_arr = array();
  
  	//if both checkbox and value to check are comma separated strings
  	if (strpos($checkbox,$separator)!== false && strpos($value,$separator)!== false ){
     
      $checkbox_arr = explode($separator,$checkbox);
      $value_arr = explode($separator,$value);
      $results = array_intersect($value_arr,$checkbox_arr);
      if(!empty($results)){
       return $content; 
      }
      
    }
  	
  //if checkboxes is comma separated string but value isn't
  	if(strpos($checkbox,$separator) !== false){
      	$checkbox_arr = explode($separator,$checkbox);
        $value_arr = explode($separator,$value_arr);
      	if(in_array($value,$checkbox_arr)){
     		return $content; 
        }
    }
  //if value is an array but checkbox isn't
  if(strpos($value,$separator) !== false){
     $checkbox_arr = explode($separator,$checkbox);
     $value_arr = explode($separator,$value);
     if(in_array($checkbox_arr,$value_arr)){
          
     		return $content; 
        }
    }
  // when both are not arrays
  if($checkbox == $value){
   return $content; 
  }
  

}
add_shortcode( 'checkboxes_conditional', 'create_checkboxescondition_shortcode' );

Then activate it.

An example usage is


[checkboxes_conditional checkbox="[types field='checkboxes-group' separator=', '][/types]" separator=',' value='1']This is my content[/checkboxes_conditional]

Please let me know if this helps.
Thanks,
Shane