Skip Navigation

[Resolved] Set the post expiration date within a form

This support ticket is created 7 years 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 7 years ago.

Assisted by: Luo Yang.

Author
Posts
#592588

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

#592707

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

#593972

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);
}
}
}

#594224

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