Skip Navigation

[Résolu] cred_save_data to select a category in a form

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.

This topic contains 12 réponses, has 5 voix.

Last updated by Peter Il y a 8 années.

Assigned support staff: Ana.

Auteur
Publications
#121256

I've posted about this in the past but never really got a working solution and I moved on to different things so as not to get stuck. Now it's time to address it so I am bringing it up again.

What I have as a setup:

Travel Tip Listings (Parent)
|-------------------------------------------- Travel Tip Reviews (child)

Both of these have a Custom Taxonomy 'travel-tips-category'

Within the Parent I have posts such as:

Beaches
Places to go
Things To Do

I have taxonomies with exactly the same names.
I have one cred form to submit reviews for ALL the parents.

How it works on the front end:

Customer goes on the parent page - for example Beaches
Customer wants to write a review about beaches
Customer clicks Cred Form. Parent Selector is correctly tagged to beaches, but client is then forced to find the category too (beaches).

The Problem:

The problem is that it is not feasable for someone to look for the category (there are around 50) when they should already be automatically tagged as the category of the parent post.

Could the solution be the following?

I have tried the cred_save_data but this does not work for me since I only have one form (id16740) for the entire CPT's in Travel Tips. The examples I found, including previous suggestions made to me suggest to use it on a per form basis. Is there a hook I can use which works with the following logic:

If CPT Parent = Beaches
Then Pre-Select Custom Taxonomy = Beaches

It is important that the custom taxonomy is pre-selected for the user (then I can hide the div) rather than forcing the user to select anything which in my case simply does not make sense.

Thanks

#121562

Luo Yang
Supporter

Languages: Anglais (English ) Chinois simplifié (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Hi triggeru571,

Follow the logic you mentioned above, I suggest you try crete a custom function with CRED hook "cred_save_data",
use WP function wp_get_post_terms() to get parent post terms,
then use WP function wp_set_post_terms() child post terms as same as parent post.

More help:
http://codex.wordpress.org/Function_Reference/wp_get_post_terms
http://codex.wordpress.org/Function_Reference/wp_set_post_terms

#125007

I'm sorry to bother you again Luoy, I have tried the above but my PHP skills are still very basic and no matter what I try to create the above function I keep getting a 500 server error, so unfortunatly I need to beg for your kind assistance a little more. Would you mind guiding me with the actual code to use here based on the above structure?

#126047

Dear triggeru571,

I'm Adriano and I will handle this issue now.

Please let me see how your code is (which you created like Luo told you).

#127976

HI Adriano, The code's was giving a server error of 500 or did nothing at all. Because of the errors I deleted them and did not keep the snippets as they were obviously wrong. So, as I mentioned in the post before this, perhaps you could assist in building a workable solution for this as I have been trying to resolve it with no success for six months.

Here are my previous support requests:
https://toolset.com/forums/topic/pre-populated-select-field-in-cred-form/
https://toolset.com/forums/topic/pre-populated-custom-taxonomy-category-in-cred/

There is also another ticket which I cannot find. Can you please assist on this to get a working solution?

Cheers

#128331

Dear triggeru571,

Is there a dev site where I can try some codes (previously configured)? If yes please send the credentials to (adriano.f@onthegosystems.com) and put this thread URL in the subject's email.

#129099

Sent you in email

#129279

Thank you for provide the access. As I understood what you want to achieve is pre-select the taxonomies in "Travel Tips Categories" with the correct. I'm afraid it is not possible yet.

#129369

So what about the suggestions on this topic and on the other two https://toolset.com/forums/topic/pre-populated-select-field-in-cred-form/
https://toolset.com/forums/topic/pre-populated-custom-taxonomy-category-in-cred/ ? I was told the cred_save_data should do the trick.

#129718

Dear triggeru571,

I've passed your ticket to our CRED developer, Ana. She will help you how to achieve it. Thank you for your patience.

#129756

Ana
Supporter

Dear triggeru571,

You can do this by adding the following hook to your functions.php file:

add_action('cred_save_data_16673', 'save_data_for_form_with_id_16673', 10, 2);
function save_data_for_form_with_id_16673($post_id, $form_data) {
	if (!empty($_GET['parent_travel-tips-listings_id'])) {
		$parent_travel_tips_listing_id = intval($_GET['parent_travel-tips-listings_id']);
		$my_taxonomy = 'travel-tip-category';
		$term_list = wp_get_post_terms($parent_travel_tips_listing_id, $my_taxonomy, array("fields" => "ids"));
		add_post_meta($post_id, '_wpcf_belongs_travel-tips-listings_id', $parent_travel_tips_listing_id, true);
		wp_set_post_terms($post_id, $term_list, $my_taxonomy);
	}
}

This will add an action to the CRED form 'Add Travel Tips' for when saving the data to the add travel tip append the information of parent 'travel tips listing' and also of the parents taxonomy 'travel tip category'. With this you no longer need to have on your form the information of parent and the common taxonomy so you can remove the two fields from the CRED form.

Please let me know if you are satisfied with my answer and if I can help you with any other questions you might have.

Regards,
Ana

#129839

Ow Ana you are my hero and made my day today 🙂 It finally works and we are past the major stumbling block for an efficient site. Thanks so much.

#163905

Hi, I am going in circles here. I have the same problem and need a some example code too please (as I bought the Toolset package so I didn't have to code).

I have a CRED form that creates a new post and I want a default category automatically submitted when the form is saved (normally just a hidden field value).

My details are:

----------------------------
CRED Form Name = New Widget
CRED Form ID = 123
Post Type Name = Cool Widgets
Default Category name I would like saved with form: Black Widget
-----------------------------

I realise there is this link (but I don't understand it):
https://toolset.com/documentation/user-guides/cred-api/

Could you please just write the function.php code for my Widget example above, (like you did for triggeru571). I would really appreciate it.