I'm trying to dynamically populate a CRED post taxonomy term select field based on the taxonomy term assigned to the page that the form is residing on.
For context, we developed this site that allows people to request services from CRED forms supplied on the frontend. The user selects the industry they're interested in by site navigation. They would then be taken to a page specific to that industry, which contains the form in question.
We initially had a post taxonomy term select field that was automatically populated based on the page it was on. These industry pages were given the same custom taxonomy we created for the CPT post that was to be created from these forms, and assigned the appropriate taxonomy term to match the term the post needed to go to. The idea was that the user shouldn't have to worry about selecting an option from a "Choose Industry" field when they're already on a page designating the industry they're interested in.
We had it working, but for whatever reason it no longer is, so I'm wondering if there was a PHP solution that I could utilize for this case.
You can view one of the pages where the form resides here: enlace oculto
Hello,
I assume we are talking about the taxonomy field created with CRED shortcode [cred_field], for example:
[cred_field field='industry' display='select' single_select='true']
There isn't such a built-in feature within CRED forms, see our document:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_field
The argument "value" works only for custom field in CRED form, it does not work for the taxonomy field.
If you agree, we can take it as a feature request, our developers will evaluate it.
As a workaround, you can setup a generic field in the CRED form to replace the taxonomy field:
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field
For example:
[cred_generic_field field='industry' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":["[wpv-post-taxonomy type="industry" format="slug"]"],
"options":[
{"value":"electrical","label":"Electrical"},
{"value":"flooring","label":"Flooring"},
{"value":"general-contracting","label":"General Contracting"}
]
}
[/cred_generic_field]
When user submit the CRED form, you can use CRED action hook "cred_save_data" to trigger a PHP function, and save the "industry" value into database, see documents:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
This hook allows doing a custom action when post data is saved to database.
https://codex.wordpress.org/Function_Reference/wp_set_object_terms
Relates an object (post, link etc) to a term and taxonomy type
Yes, the field I am currently using is exactly as you showed above. I didn't realize you could utilize the generic field in such a manner. It's unfortunate that I have to manually supply the field values for the dropdown, but it's a better solution than nothing at all.
After implementation and testing, I can confirm that it does indeed work for me. Thank you very much for your assistance.
I would also like to confirm, as a feature request, that it would be super cool if the value attribute was enabled for cred_field taxonomy term fields, so we can potentially dynamically populate this field as it is by using Toolset's shortcode system.
Perhaps, I spoke too soon.
The field is not assigning the post to an Industry taxonomy term based on the selection value, as expected. Here's my markup:
[cred_generic_field field='industry' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":["[wpv-post-taxonomy type='industry' format='slug']"],
"options":[
{"value":"electrical","label":"Electrical"},
{"value":"flooring","label":"Flooring"},
{"value":"general-contracting","label":"General Contracting"},
{"value":"hvac","label":"HVAC"},
{"value":"landscaping","label":"Landscaping"},
{"value":"other-services","label":"Other Services"},
{"value":"painting","label":"Painting"},
{"value":"plumbing","label":"Plumbing"},
{"value":"pool-maintenance","label":"Pool Maintenance"},
{"value":"roofing","label":"Roofing"},
{"value":"tree-care-removal","label":"Tree Care/Removal"}
]
}
[/cred_generic_field]
The dynamic population is working, but the term assignment is not.
Q1) It's unfortunate that I have to manually supply the field values for the dropdown, but it's a better solution than nothing at all.
Yes, it is possible to create a view to list term of taxonomy "industry", and use it as options of the CRED generic select field, see similar thread:
https://toolset.com/forums/topic/filtering-parent-options-by-custom-field/#post-361968
Q2) The field is not assigning the post to an Industry taxonomy term based on the selection value, as expected
Yes, as I mentioned above, it needs custom codes to save the "industry" value into database.
If you need assistance, please provide a test with the same problem, and fill below private detail box with login details and FTP access, also point out the problem page URL and CRED form URL, and where I can edit you PHP codes, I need a live website to test and debug, thanks
Thanks for the details, I have done below modification in your website:
1) Add below codes into theme file "functions.php":
add_shortcode('hide-it', 'hide_it_func');
function hide_it_func(){
return;
}
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==738)
{
if (isset($_POST['industry']))
{
wp_set_object_terms( $post_id, $_POST['industry'], 'industry', False );
}
}
}
2) it is possible to create a view to list term of taxonomy "industry":
enlace oculto
...
<wpv-loop>
[wpv-item index=1]{"value":"[wpv-taxonomy-slug]","label":"[wpv-taxonomy-title]"}[wpv-item index=other],{"value":"[wpv-taxonomy-slug]","label":"[wpv-taxonomy-title]"}
</wpv-loop>
...
3) use it as options of the CRED generic select field, Edit the CRED form
enlace oculto
Edit the shortcode as below:
...
[cred_generic_field field='industry' type='select' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":["[wpv-post-taxonomy type='industry' format='slug']"],
"options":[[wpv-view name="dynamically-populate-taxonomy-term-select-field"]]
}
[/cred_generic_field]
...
Please test again, check if it is fixed or not. thanks
Thank you very much for you assistance. It does work this time.