I know that it is possible to set a post expiration period in a CRED form which will apply to all posts created with that form.
However, I would like users to be able to choose the date when the post expires (and goes to the bin) rather than have a specified time period from posting. Is it possible to have a date field in the form which achieves this. i know I can filtyer in views based on a date but I don't want to do that.
Thanks
Dear Nick,
There isn't such a built-in feature within CRED plugin, but it is possible within custom codes, CRED is using a hidden field "_cred_post_expiration_time" to store the value of "Post Expiry date" in time-stamp format,
So you can try these:
1) Setup a generic date field in the CRED form for user choose the expiration date.
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_generic_field
2) When user submit the CRED form, use CRED action hook "cred_save_data" to save "_cred_post_expiration_time" value into your database, more help:
cred_save_data
Hook to do custom action when post data are saved to database.
https://toolset.com/documentation/user-guides/cred-api/#csd
Thanks.
I am using hooks for the first time.
I'm a bit unclear how to pass the expiry date data from the generic field to the function.
I have adapted the example code given but I don't understand how the form can pass the data:
add_action('cred_save_data', 'set_expiry_date',10,1);
function set_expiry_date($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==xx)
{
if (isset($_POST['_cred_post_expiration_time']))
{
// add it to saved post meta
add_post_meta($post_id, '__cred_post_expiration_time', $_POST['_cred_post_expiration_time'], true);
}
}
}
Here are detail steps:
1) Edit your CRED form, add a generic date field:
<div class="form-group">
<label>Expiration date</label>
[cred_generic_field field='expiration-date' type='date' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":""
}
[/cred_generic_field]
</div>
2) Modify your PHP codes as below:
add_action('cred_save_data', 'set_expiry_date', 10, 2);
function set_expiry_date($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==123)
{
if (isset($_POST['expiration-date']['datepicker']))
{
// add it to saved post meta
update_post_meta($post_id, '_cred_post_expiration_time', $_POST['expiration-date']['datepicker']);
}
}
}
Please replace 123 with your CRED form's ID, and test again