Skip Navigation

[Resolved] How to set a default value for date/time field in CPT?

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

Problem:

I have a CPT called Events. I use a Types date/time field for the event date and time. The event start time is always 7:00 PM.

However, PHP 7.2 makes the use of $this as a function parameter a Fatal Error. Here is the code I currently have that fails in PHP 7.2:

https://toolset.com/forums/topic/how-to-set-a-default-value-for-date-time-field-in-cpt/#post-1204861

Solution:

It is a custom codes problem, see example codes here:

https://toolset.com/forums/topic/how-to-set-a-default-value-for-date-time-field-in-cpt/#post-1205239

Relevant Documentation:

This support ticket is created 5 years, 8 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
- 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 2 replies, has 2 voices.

Last updated by jeffS-2 5 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1204861

Tell us what you are trying to do?
I have a CPT called Events. I use a Types date/time field for the event date and time. The event start time is always 7:00 PM.
Someone (I think, Luo) helped me a long time ago, to set a default value as below. However, PHP 7.2 makes the use of $this as a function parameter a Fatal Error. Here is the code I currently have that fails in PHP 7.2:

[code]
add_filter('wpcf_fields_slug_event-date-time_value_get', 'default_value_events9_func', 10, 2);
function default_value_events9_func($meta, $this) {
if(empty($meta)){
$meta = strtotime("07:00 PM");
}
return $meta;
}
[/code]

I tried to rewrite this with a few different methods but I could not get it to work. I thought it should be easy.

Can you please show me how to do this without the use of $this as a parameter?

Thanks,
Jeff Safire
---------------

#1205239

Hello,

In you case, it does not need "$this" parameter, for example:

add_filter('wpcf_fields_slug_event-date-time_value_get', 'default_value_events9_func', 10, 1);
function default_value_events9_func($meta) {
if(empty($meta)){
$meta = strtotime("07:00 PM");
}
return $meta;
}
#1226184

My issue is resolved now. Thank you!