Conditional Logic based on post data field
-
Hi,
I wanted to know the possibility of having a field having conditional logic based on a specific field in the Post Data form field.
For example, I am building a Business Directory with a custom post type of “Listing”. The listings has a categories field with in, which can have the business category e.g. Restaurant, Plumber etc.
I want a Number field to be able to have conditional logic to show only if the business category is Restaurant. Is there any way to implement this? Currently it only gives me the option to put conditional logic based on the whole post (see screenshot)
-
Hi @sitenerds
I hope you are doing well today.
By default, this will be not possible. I pinged out SLS Team to review this and see what will be possible in this case. We will post an update here as soon as more information is available.
Kind Regards,
KrisHi again @sitenerds
This snippet should help
add_action('wp_footer', 'wpmudev_number_field_visibility_category', 9999 ); function wpmudev_number_field_visibility_category() { global $post; if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) { return; } ?> <script type="text/javascript"> jQuery(document).ready(function($) { setTimeout(function() { $('.forminator-custom-form').trigger('after.load.forminator'); }, 100); $(document).on('after.load.forminator', function(e, form_id) { if ( e.target.id == 'forminator-module-1609' ) { // Please change the form ID. $('select[name="postdata-1-category"]').on('select2:select', function(ev) { var select_val = $(ev.currentTarget).val(); if ( 3 == select_val ) { // Please change 3 to your category ID. $('#number-1').removeClass('forminator-hidden'); } else { $('#number-1').addClass('forminator-hidden'); } }); } }); }); </script> <?php }Note to change 1609 to your form ID and 3 to your category ID.
He is a guide on how to install mu-plugin:
https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-pluginsKind Regards,
KrisHi Kris,
Thanks for looking into this. I tried the snippet but it didn’t seem to work. Could you double-check that it is correct?
For clarity, I separated the JavaScript and PHP codes and input them separately using the WP Code Snippets plugin.
Hi @sitenerds,
Apologies for the delay in responding. It seems that the response was not notified on our end for some reason.
I verified the code on a test website at my end and could confirm it works fine.
Can you please try adding the code using a mu-plugin as suggested instead of using the WP Code Snippets plugin?
Please let us know the result.
Kind Regards,
Nebu JohnHi Nebu,
No worries, thanks for the reply.
I implemented the code as a mu-plugin as instructed, changed the form ID and added in my category ID and it still did not change anything. I still get the same options in the conditional logic: https://ibb.co/ykk05dp
Hi @sitenerds,
Sorry to know that the workaround is still not working at your end.
Can you please try disabling the option “Load form using AJAX” under the Behaviour tab of the form and check if that helps?
If the issue persists, please provide an export of the form using a cloud service like Google Drive or pastebin.com so that we can take a closer look.
Please find detailed instructions on how to export a form in our documentation here: https://wpmudev.com/docs/wpmu-dev-plugins/forminator/#import-export
Kind Regards,
Nebu JohnHello @sitenerds ,
It’s been several days since we asked for the above test result and without the response we are not able to provide further assistance.
If you require further help please feel free to reopen this topic.
kind regards,
KasiaHi @wpmudev-support2 / @wpmudevsupport14
Loading form with AJAX was unchecked and I still am not able to select the category in the conditional logic.
Please see export here: https://docs.google.com/document/d/1IWg5EcKrb_-KNPXnH4bucQiErzRp_BZVXF6n_DvwREU/edit?usp=drive_link
-
This reply was modified 1 year, 12 months ago by
sitenerds.
Hi @sitenerds,
It appears the Google Drive URL is private, so we couldn’t access the export. Please make the URL public, so that we can check further.
Looking forward to your response.
Best Regards,
Nithin
Hello @sitenerds,
Thank you for sharing the form. Could you please try the following snippet, instead of the previous one:
<?php add_action( 'wp_head', function() { global $post; if ( is_a( $post, 'WP_Post' ) && ! has_shortcode( $post->post_content, 'forminator_form' ) ) { return; } $form_id = 1609; $cat_id = 3; $cond_field = 'number-1'; ?> <script type="text/javascript"> jQuery(document).bind("ready ajaxComplete", function(){ if(jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-category"]').val() == <?php echo $cat_id; ?>) { jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden"); } else { jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden"); } jQuery('#forminator-module-<?php echo $form_id; ?> select[name="postdata-1-category"]').bind("select2:select", function(e){ if(jQuery(this).val() == <?php echo $cat_id; ?>) { jQuery("#<?php echo $cond_field; ?>").removeClass("forminator-hidden"); } else { jQuery("#<?php echo $cond_field; ?>").addClass("forminator-hidden"); } }); }); </script>; <?php } );– replace the form and category IDs on these lines:
$form_id = 1609; $cat_id = 3;Hope this helps. Please let us know if there’s still any issue.
Best Regards,
DmytroHello again,
I am still getting the same output. The options in for conditional logic do not show the post category: https://ibb.co/PwnWR1z
Hello @sitenerds,
Please note, that the snippet doesn’t add any user interface elements for the conditional logic (this would require additional programming, which is beyond our support scope). I’m sorry for any confusion regarding this.
Instead, it hides/shows the field programmatically, depending on the category which a user selects on the front end.
The options in for conditional logic do not show the post category
You’ll need to specify the ID of the Restaurant category. This ID can be found in the address bar, when you open this category for editing:
https://prnt.sc/CM9YT2Egnxd5Could you please check if the form and category IDs in the code have been replaced with the correct values, and then try selecting a category in the form after publishing it.
Please let us know if there’s still any issue.
Best Regards,
Dmytro-
This reply was modified 1 year, 11 months ago by
Dmytro - WPMU DEV Support.
Hello,
Thanks for clarifying. So the number field correctly is hidden on the front-end when the snippet is implemented, however it does not seem to be showing when I have the category in the post selected.
I went into the category and found the id 50 from the following url: term.php?taxonomy=hp_listing_category&tag_ID=50&post_type=hp_listing
Having replaced this in the snippet, the the field doesn’t show up as expected with the category selected: https://ibb.co/C5pHHqR
The field doesn’t appear with any of the other categories selected either (tested incase there was an issue with the id).
Hi @sitenerds,
Could you please share the full code which you have implemented?
I gave a quick test on my test site and it does work out of the box where the “Average Speed” field only shows up when the category is added under the following line in the code is selected:
$cat_id = 3;Is there a page URL where you have the form added so we can check and give a closer look?
Looking forward to your response.
Kind Regards,
Nithin
-
This reply was modified 1 year, 12 months ago by
The topic ‘Conditional Logic based on post data field’ is closed to new replies.