Hi, here's a sample CRED form that shows what I was thinking:
[creduserform class='cred-user-form cred-keep-original']
[cred_field field='form_messages' value='' class='alert alert-warning']
[cred_generic_field field='activities' type='hidden' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":"123"
}
[/cred_generic_field]
[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']
[/creduserform]
You can see that all the inputs have been removed except a generic, hidden field. I have hard-coded the Activity ID to 123 for now, but you'll probably use some other shortcode or a URL parameter to determine the relevant Activity ID and pass it in here.
Next, here's cred_save_data hook that will add that activity ID to a User's profile in a repeating numeric field with slug "activities":
add_action('cred_save_data', 'add_user_activity',10,2);
function add_user_activity($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==123)
{
if (isset($_POST['activities']))
{
// add it to saved post meta
add_user_meta($post_id, 'wpcf-activities', $_POST['activities']);
}
}
}
Change 123 to the numeric ID of the CRED form. $_POST['activities'] is how you access the value passed into the activities field, and add_user_meta() is how you store that value in the User's custom field. It will continue to add new values as the User checks off more and more Activities as completed.
Christian,
This makes sense.
One question ... you used a generic field, and then added a custom field value into it afterward. I didn't know this was possible. All the information that I found in the forums about making a custom field hidden was to use CSS. I am able to just create a generic field, make it hidden, and add my custom field in the "field=" part?
Thanks so much for your continued support!!
Not exactly. The generic hidden field isn't related programmatically to the real custom field. The generic field name "activities" in this case is arbitrary, but "wpcf-activities" is the prefixed slug of the actual custom field. If you want to use a generic hidden field in a form instead of a real field, then you must add custom code like we have here to capture the generic value and then store it in the real custom field using update_post_meta or add_post_meta. Otherwise, the value in the hidden field will not be saved. CSS is a simpler approach to hide a real custom field.
I have yet to implement this. I had to jump off of this project, but I'd like to keep this support ticket open. I will be getting back to this very soon.
Thanks!
No problem, I will stand by for your update.