Skip Navigation

[Résolu] CRED form generic field not saving Taxonomy data cred_save_data

This support ticket is created Il y a 4 années et 11 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Ce sujet contient 4 réponses, a 2 voix.

Dernière mise à jour par stuart Il y a 4 années et 11 mois.

Assisté par: Nigel.

Auteur
Publications
#1403551

I am trying to use a generic field in a CRED form, populated with values from a a View.

[Reason I want to use generic fields is so I can chain them together at some stage, e.g - parent, child taxonomy values, a question for another time.]

Then save the data to a related Taxonomy using cred_save_data hook.
[basing my experiments with this ticket:
https://toolset.com/forums/topic/using-generic-cred-fields-instead-of-taxonomy-selects/ ]

Here is what I have:

CRED Edit Form:
[code]
<div class="form-group">
<label>Industries Serviced</label>
[cred_generic_field type='multiselect' field='partner-service-industries' class='partner-service-industries']
{
"required":0,
"persist":1,
"options":[[wpv-view name="partner-industries-select"]]
}
[/cred_generic_field]
</div>
[/code]
- The values from the view are populating the field on the front end fine (using Select2) - see image: lien caché

Now using the Toolset -> Settings -> Custom Code I have used this code to save the taxonomy to to the post.

The cred post edit form ID number has been set correctly

The id of the field is the "partner-service-industries" as you can see above.

And the related Taxonomy is that the array is to be save into is: "partner-industry"

[code]
/**
* Generic CRED save to Taxonomy
*/

function my_custom_save_data_action_385879_386143( $post_id, $form_data ){
//array of CRED forms to be considered
$cred_forms_array = array(385879, 386143);
// if a specific form
if (in_array($form_data['id'], $cred_forms_array)){
if (!empty($_POST['partner-service-industries'])){
$tag = array( $_POST['partner-service-industries'] ); // id of your tag to be saved
$taxonomy = 'partner-industry'; // add the slug of the taxonomy
wp_set_post_terms( $post_id, $tag, $taxonomy );
}
}
}

[/code]

However - nothing saves when I view the edited post via admin...

- Now I also tried the example [exactly as it was in the post above] and I got the same result - nothing saved.
https://toolset.com/forums/topic/using-generic-cred-fields-instead-of-taxonomy-selects/

I am also going to want to have this data (once working) displayed on the front end in a generic field that is editable the same way.

thanks

#1404343

Nigel
Supporter

Les langues: Anglais (English ) Espagnol (Español )

Fuseau horaire: Europe/London (GMT+00:00)

Hi Stuart

Is that the entirety of your code snippet?

You have a function ("my_custom_save_data_action_385879_386143") but then you don't add the function to any hook.

I would expect to see this, too:

add_action( 'cred_save_data', 'my_custom_save_data_action_385879_386143', 10, 2 );

Without that, your code simply isn't running, so can you confirm that, first?

#1404877

yea sorry Nigel, cut and paste error (busy day)...

full code snippet (not working)

[code]
<?php
/**
* Generic CRED save to Taxonomy
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );
add_action( 'cred_save_data', 'my_custom_save_data_action_385879_386143', 10, 2 );
function my_custom_save_data_action_385879_386143( $post_id, $form_data ){
//array of CRED forms to be considered
$cred_forms_array = array(385879, 386143);
// if a specific form
if (in_array($form_data['id'], $cred_forms_array)){
if (!empty($_POST['partner-service-industries'])){
$tag = array( $_POST['partner-service-industries'] ); // id of your tag to be saved
$taxonomy = 'partner-industry'; // add the slug of the taxonomy
wp_set_post_terms( $post_id, $tag, $taxonomy );
}
}
}

[/code]
Thanks!

#1405451

Ok ran some more tests and fixed this one up, however it led to more questions 🙂

here is the working version in case anyone is interested and ill start another ticket for the other questions - its related but probably worth its own ticket as it related to saving the option value and the option label into the taxonomy...

In the CRED edit form:
[code]
[cred_generic_field type='multiselect' field='test' class='partner-service-industries']
{
"required":0,
"persist":1,
"default":[[wpv-view name="test-tax"]],
"options":[[wpv-view name="partner-industries-select"]]
}
[/cred_generic_field]
[/code]

In the code snippets section:
[code]
<?php
/**
* New custom code snippet (replace this with snippet description).
*/

toolset_snippet_security_check() or die( 'Direct access is not allowed' );

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']==386143)
{
if (isset($_POST['test']))
{
wp_set_object_terms( $post_id, $_POST['test'], 'test', False );
}
}
}
[/code]

EXTRA Note: the default option on the generic custom field here which allows the cred form refresh to show all the generic multi-select values need to be formatted in a particular way:

Ensure you select the correct taxonomy, and untick the query options " Don't show empty terms" (no idea why you need to).
Also, in the query filter, ensure this: Taxonomy is set by the page where this View is inserted
(and make sure: Disable the wrapping DIV around the View )

In the view loop editor - do this:
[code]
<wpv-loop>
[wpv-item index=1]
"[wpv-taxonomy-slug]"
[wpv-item index=other],"[wpv-taxonomy-slug]"
</wpv-loop>
[/code]

#1405453

My issue is resolved now. Thank you!