Skip Navigation

[Résolu] Conditionals using checkboxes

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I am trying to display some content conditionally using the values from a checkboxes group custom field, but the conditionals don't work.

Solution: Checkboxes group fields store data in a format that is not easily used in conditional HTML statements. Instead, use the Types field shortcode itself to apply conditions:

[types usermeta='member-skills' separator='' option='0' state='checked']
  CRED form when the first option is checked
[/types]
[types usermeta='member-skills' separator='' option='1' state='checked']
  CRED form when the second option is checked
[/types]
[types usermeta='member-skills' separator='' option='2' state='checked']
  CRED form when the third option is checked
[/types]

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.

Marqué : 

This topic contains 8 réponses, has 2 voix.

Last updated by Christian Cox Il y a 3 années et 6 mois.

Assigned support staff: Christian Cox.

Auteur
Publications
#624921

I am trying to:
I'm trying to show cred forms according to user field. (member skills:checkboxes)

I tried this code

[wpv-conditional if="( '[wpv-user field='wpcf-member-skills']' eq 'Public Speak' )"]
[cred_form form="Trainings"] <br />
[/wpv-conditional]

And I tried this code.

[wpv-conditional if="( $(wpcf-member-skills) eq 'Public Speak' )"]
[cred_form form="Trainings"] <br />
[/wpv-conditional]

Also I tried this code.

[wpv-conditional if="('[types usermeta='member-skills' user_current='true' output='raw'][/types ]' eq 'Public Speak' )"]
[cred_form form="Trainings"]
[/wpv-conditional]

I couldn't get any result.No cred form shown on page. I need your advice and support.
Thanks

#624992

Hi try to add the field just above the conditional like this:

Member skills: [wpv-user field='wpcf-member-skills']
[wpv-conditional if="( '[wpv-user field='wpcf-member-skills']' eq 'Public Speak' )"]
[cred_form form="Trainings"] <br />
[/wpv-conditional]

What do you see on the front-end when you test the form now?

#625013
cred logic.png

Hi Christian ,

Here's the result shown on page.

Member skills: a:1:{s:64:”wpcf-fields-checkboxes-option-b521b11532c9f168801c1b53f17b255d-1″;a:1:{i:0;s:12:”Public Speak”;}}

#625026

Okay since checkboxes fields store data in a serialized array, it's not really possible to use them in wpv-conditional tags like this. Instead, you can use Types field shortcodes to do something similar:

[types usermeta='member-skills' separator='' option='0' state='checked']
  CRED form when the first option is checked
[/types]
[types usermeta='member-skills' separator='' option='1' state='checked']
  CRED form when the second option is checked
[/types]
[types usermeta='member-skills' separator='' option='2' state='checked']
  CRED form when the third option is checked
[/types]
#625032

Many Thanks Christian, It works now.

#625110

Hi Christian, I missed something. One by one ok. Basic code is working but with this idea I have to make 13 form for 13 skills or more at future. Lots of form on one page.
I have 13 skills and 3 cred form. "Option 1-4 Form1= (Trainings) " , "Option 5 -9 Form2" and "Option 10-13 Form 3".

How Can I show forms according to options selected. Not one form to one option selected.Not one by one.
I tried this

[types usermeta='member-skills' separator='' option='1,2,3,4' state='checked']
 [cred_form form="Trainings"]
[/types]

I tried this

	
[wpv-conditional if="('[types usermeta='member-skills'' separator=', ' option='1,2,3,4' state='checked']' )"]
[cred_form form="Trainings"]
[/wpv-conditional]

I tried this

	
[wpv-conditional if="('[types usermeta='member-skills'' separator=' ' option='1' state='checked']' OR '[types usermeta='member-skills'' separator=' ' option='2' state='checked']' OR '[types usermeta='member-skills'' separator=' ' option='3' state='checked']' OR '[types usermeta='member-skills'' separator=' ' option='4' state='checked']' )"]		
[cred_form form="Trainings"]
[/wpv-conditional]

All these codes show empty page. No form is shown.
I need your advice and support.
Thanks

#625346

There's not an easy way to automate or consolidate these, because it would become quite complex to handle all the combinations of possible selections. I think the best solution is to use a custom shortcode that tests the value of the usermeta field against a known value or set of values. Add this code to your child theme's functions.php file:

function checkboxes_contain_any_func($atts) {
  $a = shortcode_atts( array(
      'any' => '',
      'test' => ''
  ), $atts );
  $any = isset( $a['any'] ) ? explode(',', $a['any']) : null;
  $test = isset( $a['test'] ) ? explode( ',', $a['test'] ) : null;

  return !empty( array_intersect( $any, $test ) );
}
add_shortcode("checkboxes_contain_any", "checkboxes_contain_any_func");

Go to Toolset > Settings > Frontend content and add "checkboxes_contain_any" to the Third Party Shortcode Arguments input field. Then you have 3 forms, so you need 3 conditionals. Let's assume the checkbox values are "Option 1", "Option 2", and so on through "Option 12". You can use the shortcode in conditionals like this:

[wpv-conditional if="( [checkboxes_contain_any any='[types usermeta='member-skills' separator=','][/types]' test='Option 1,Option 2,Option 3,Option 4'] eq '1' )"]
[cred_form form="Trainings"]
[/wpv-conditional]

[wpv-conditional if="( [checkboxes_contain_any any='[types usermeta='member-skills' separator=','][/types]' test='Option 5,Option 6,Option 7,Option 8'] eq '1' )"]
[cred_form form="Form 2"]
[/wpv-conditional]

[wpv-conditional if="( [checkboxes_contain_any any='[types usermeta='member-skills' separator=','][/types]' test='Option 9,Option 10,Option 11,Option 12'] eq '1' )"]
[cred_form form="Form 3"]
[/wpv-conditional]

In each conditional, replace the list of values "Option 1,Option 2,Option 3,Option 4" with a comma-separated list of the checkbox values that should display this form. Everything else in the conditional will remain the same. If any of the corresponding checkboxes are checked, the CRED form in this conditional will appear. The checkbox values must be alphanumeric characters and spaces only, no other special characters or punctuation is allowed in these.

#625645

Many Thanks Christian. Your solution is perfect. It worked when types field='member-skills' changed to types usermeta='member-skills'.

#625948

Thanks, I have updated the code here for future reference.