Skip Navigation

[Resolved] CRED: User-defined post expiration

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

Problem:
With a CRED form that uses automatic post expiration, is it possible for the user to choose the period and overwrite the form settings?

Solution:
Add a field to your form to select the expiry time. You can then use the cred_save_data hook to run some code when the form is submitted that takes the value of the generic date field (available from the $_POST object), which will be in UNIX timestamp format, and overwrite the default post expiration date set by the form.

This is stored as post meta with a key of _cred_post_expiration_time and is also a UNIX timestamp, so it is simply a question of overwriting the current value with that retrieved from your generic field, using update_post_meta.

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://developer.wordpress.org/reference/functions/update_post_meta/

This support ticket is created 6 years, 9 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 4 replies, has 2 voices.

Last updated by webD-3 6 years, 9 months ago.

Assisted by: Nigel.

Author
Posts
#613078

I understand that CRED can be configured to have posts expire after a certain pre-determined amount of time.
https://toolset.com/documentation/user-guides/automatic-post-expiration/

Is there a way for the public user/submitter to choose their own post expiration date?

Use case: user-submitted coupons/offers. I have a field for expiration date, which I can display on the page, but would also like to use this data to set an expiration date on the post. Is this possible?

#613169

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

You can add a generic date field to your form where the users set the target expiration date (https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/).

You can then use the cred_save_data hook (https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data) to run some code when the form is submitted that takes the value of the generic date field (available from the $_POST object), which will be in UNIX timestamp format, and overwrite the default post expiration date set by the form.

This is stored as post meta with a key of _cred_post_expiration_time and is also a UNIX timestamp, so it is simply a question of overwriting the current value with that retrieved from your generic field, using update_post_meta (https://developer.wordpress.org/reference/functions/update_post_meta/).

If you try that and run into problems let me know and I will try and help.

#613546

Nigel,

Thank you! I was able to hook into cred_save_data and actually use my wpcf-ending-date custom field, rather than a generic date field. This way the submitter only has to put in an ending date once, not twice. Thanks for pointing me in the right direction.

One further question. I have "Check for expired content" set to "Once Daily" in Settings > Forms > Other. At what time each day does the Once Daily check happen?

I am already modifying the timestamp in the function to add one day, to make the expiry occur at 7pm of the date selected instead of 7pm the day before (thanks to being in the UTC -5 time zone.) Depending on when the Once Daily check occurs I might need to adjust that.

Thanks for your help.

#613644

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

The timing of when the checks happen is unpredictable.

It works using a standard WP cron job. These are only triggered when there is any activity on your site (front or back-end, anything that spins up WP), and so if you specify 'daily' it will check to see whether it is 24hours+ since the last time the cron job ran. So it is entirely unpredictable.

This is just the job that checks whether a post expired. So in certain circumstances it could be that when a post "expires" it is only changed to draft or whatever you selected some 23h59m later, or longer if there is no activity on your site.

If you need the time to be more accurate you would need to set a shorter interval (although the shorter the interval the more demands on your server).

It is not expected that you can accurately target the specific moment when a post expires. You should think of the post expiration time as "it won't expire before" rather than "it will have expired by".

#614271

Nigel,

Thanks again for the reply. I decided to add 29 hours to the unix timestamp, so that the expiration happens (at earliest) at the conclusion of the defined expiration date. Then I set the cron to check hourly rather than once daily. This way the post should be removed from the site in the overnight after the defined expiration date has fully passed. I appreciate your time.