Skip Navigation

[Resolved] Automatically assigning a category to a submission

This thread is resolved. Here is a description of the problem and solution.

Problem:
We have a CRED form that has checkboxes for categories but what we'd really like to do is have that hidden to the user and have all submissions going through this form be automatically assigned to our "Partner Updates" category. Can you tell me how I can achieve that?

Solution:
This is not possible at moment with CRED interface options but you can do it using "cred_save_data" hook. Solution here:
https://toolset.com/forums/topic/automatically-assigning-a-category-to-a-submission/#post-572543

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created 7 years, 2 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 4 replies, has 2 voices.

Last updated by RogerD3433 7 years, 2 months ago.

Assisted by: Noman.

Author
Posts
#572527

We're using types, layouts and CRED to allow our partners to submit an update about their work via our website. You can see the live CRED form here: hidden link and I'm pasting the code for it below.

You'll see from the live form that we have checkboxes for categories but what we'd really like to do is have that hidden to the user and have all submissions going through this form be automatically assigned to our "Partner Updates" category.

Can you tell me how I can achieve that? Thanks very much.

----

<h2 class="entry-title">Project Update Form</h2>

[credform class='cred-form cred-keep-original']

[cred_field field='form_messages' value='' class='alert alert-warning']
<p> </p>
<p> Rather than require a detailed report, we ask that you answer a few questions to update us on your project’s progress. Short, informal answers of 1-3 sentences each are fine. We also ask that you upload a recent photo showing your garden and, ideally, some of the people involved in it or benefitting from it. PLEASE NOTE: your answers and photo will be posted to our website so please watch your spelling and please be sure that you have the permission of any people appearing in your photos.</p>
<p> </p>

<div class="form-group">
<label>What is your full name?</label>
[cred_field field='updateauthorname' post='update' value='' urlparam='' class='form-control' output='bootstrap']
</div>

<div class="form-group">
<label>What is your email address?</label>
[cred_field field='updateauthoremail' post='update' value='' urlparam='' class='form-control' output='bootstrap']
</div>

<div class="form-group">
<label>What is the name of your garden project?</label>
[cred_field field='post_title' post='update' value='' urlparam='' class='form-control' output='bootstrap']
</div>

<div class="form-group">
<label>Where is your project located? </label>
[cred_field field='updateprojectaddress' post='update' value='' urlparam='' class='form-control' output='bootstrap']
</div>

<div class="form-group">
<label>How many people does your project reach or impact in a DIRECT way?</label>
[cred_field field='updateprojectreach' post='update' value='' urlparam='' class='form-control' output='bootstrap']
</div>

<div class="form-group">
<label>What were your biggest successes this season?</label>
[cred_field field='updatesuccessfield' post='update' value='' urlparam='' class='form-control' output='bootstrap']
</div>

<div class="form-group">
<label>What challenges did or does your project face?</label>
[cred_field field='updatechallengefield' post='update' value='' urlparam='' class='form-control' output='bootstrap']
</div>

<div class="form-group">
<label>How are you working to overcome them?</label>
[cred_field field='updateovercomefield' post='update' value='' urlparam='' class='form-control' output='bootstrap']
</div>

<div class="form-group">
<label>What did you or your project learn this season that might be useful to others as well?</label>
[cred_field field='updatelearnfield' post='update' value='' urlparam='' class='form-control' output='bootstrap']
</div>

<div class="form-group">
<label>Categories</label>
[cred_field field='category' display='checkbox' output='bootstrap' value='checked']
</div>

<div class="form-group">
<label>Upload main photo</label>
[cred_field field='updateimage' post='update' value='' urlparam='' output='bootstrap']
</div>

<div class="form-group">
<label>Upload photo for thumbnail</label>
[cred_field field='_featured_image' value='' urlparam='' output='bootstrap']
</div>

[cred_field field='recaptcha' value='' urlparam='' class='form-control' output='bootstrap']
<p> </p>
[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]

#572543

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Roger,

Thank you for contacting Toolset support. You can add "cred_save_data" hook to your functions.php file.

Please find following link with solution:
https://toolset.com/forums/topic/set-default-category-for-posts/#post-389970

Please remember to replace the following values in the code provided in above link:

Replace 6177 with your Form ID.
Replace 202 with your Term ID.
Replace 'category' with your category slug (Taxonomy slug, not the term slug).

Thank you

#572652

Thank you, Noman. I added the code below to my functions.php file but instead of assigning the "Partner Updates' category to my posts, it is applying "Uncategorized". Did I get something wrong in the syntax?

My form ID is 6551, the category ID is 17 and the category slug is partner-updates

---

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"]==6551)
{
$category_ids = array(17); // replace your category ID
wp_set_object_terms( $post_id, $category_ids, 'partner-updates');
 
}
}
#572668

Noman
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Taxonomy Slug.png

By category slug I actually meant to say “Taxonomy slug”. Instead of 'partner-updates' please add your main Taxonomy slug in the code and it will work. I have just tested this in my test site. Please see attached screenshot if you are unsure about the main Taxonomy slug.

partner-updates is basically the Term slug.

Please let me know if it resolves the issue. Thank you

#572686

Thanks so much, Noman. That did the trick!