Skip Navigation

[Resolved] Post expiration date in form doesn’t work anymore

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

Problem:
Post expiration date in form doesn't work anymore -set post expiration time using generic custom date field

Solution:
Toolset forms saves the post expiration time to postmeta table with the key "_cred_post_expiration_time". To override the value of post expiration time based on the custom date field value, you will require to use the Toolset form's hook "cred_submit_complete".

https://toolset.com/forums/topic/post-expiration-date-in-form-doesnt-work-anymore/#post-2072441

Relevant Documentation:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cred_submit_completecre

This support ticket is created 3 years, 6 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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 13 replies, has 2 voices.

Last updated by katjaL 3 years, 6 months ago.

Assisted by: Minesh.

Author
Posts
#2070247

I got working couple of years ago the post expiration date following this thread https://toolset.com/forums/topic/set-the-post-expiration-date-within-a-form/.

Now it doesn't work anymore for some reason. Now what happens is: the date is changed to the default (what is defined in form settings) expiration date after saving. If there is no default expiration time set, the expiration date is set to current date.

The non-working form code is this:

[cred_generic_field field='expiration-date' type='date' class='' urlparam='']
{
"required":0,
"validate_format":0,
"default":""
}
[/cred_generic_field]

and the snippet is this:

add_action('cred_save_data', 'set_expiry_date', 10, 2);
function set_expiry_date($post_id, $form_data)
{
if ($form_data['id']==7654 or $form_data['id']==7655)
{
if (isset($_POST['expiration-date']['datepicker']))
{
update_post_meta($post_id, '_cred_post_expiration_time', $_POST['expiration-date']['datepicker']);
}
}}

#2070795

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Toolset custom date field value saved as Unix Timestamp, Can you please try to adjust your code as given under:

Instead of using $_POST['expiration-date']['datepicker'], what if you try to use the $_POST['expiration-date']['timestamp'].

For example:

add_action('cred_save_data', 'set_expiry_date', 10, 2);
function set_expiry_date($post_id, $form_data) {

if ($form_data['id']==7654 or $form_data['id']==7655) {
    if (isset($_POST['expiration-date']['datepicker'])){
        update_post_meta($post_id, '_cred_post_expiration_time', $_POST['expiration-date']['timestamp']);
    }
}
}

I hope above code will help you to resolve your issue.

#2070813

Hi Minesh, thank you. Unfortunately it didn't help.

#2070817

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please share on what page you added the form and share admin access details. Please share where you added the code I shared.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2070849

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I could not able to locate the Snippets, can you please share the direct link where I can access it?

#2070863

hidden link

#2070961

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thank you for sharing the link but when I try to access that link it shows me the permission error:

Sinulla ei ole oikeutta tähän sivuun.

Can you please grant me permission so that I should be able to access the shared snippet page.

#2071161

Sorry Minesh, it seems the Snippets plugin requires super admin priviledges. You have it now, so the code should be visible for you.

#2072203

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I see with your form, you set the the post expiration setting for the form is 60 days:
=> hidden link

Do you mean that post expiration date should be set based on selected date + 60 days?

#2072211

This 60 days would be the expiration time if user doesn't fill the date in date field. I did try this also without this "default" expiration time. Didnt' work that way either.

#2072349

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - thank you for confirmation.

However - I see weird datepicker when I click on calendar icon. Can you please disable this or tell me how can I display the calendar normally?
=> hidden link

#2072389

I removed the accessibility alarms, now check again.

#2072441

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code snipped code as given under:

add_action('cred_submit_complete', 'func_submit_complete',10,2);
function func_submit_complete($post_id, $form_data){
    // if a specific form
    if ($form_data['id']==7654 or $form_data['id']==7655){
      	
        if (isset($_POST['expiration-date']['datepicker'])){
	  		  	update_post_meta($post_id, '_cred_post_expiration_time', $_POST['expiration-date']['timestamp']);
 	   }else{
			  $current_plus_60_days = strtotime("+60 days");
	  			update_post_meta($post_id, '_cred_post_expiration_time',$current_plus_60_days);
		}
    }
}

I've added the post with title "ts-test-9" and selected the exipiration date 9th June and once I create the post using form when I check on the backend, it shows the 9th June as expiration date.

Can you please confirm:

#2072533

My issue is resolved now. Thank you very much Minesh!