Default Date for Cloned Date Field
- This topic has 3 replies, 2 voices, and was last updated 4 years, 7 months ago by
Long Nguyen.
-
AuthorPosts
-
March 15, 2021 at 4:42 AM #24910
Martin
ParticipantHi,
I have a date field (job_update_date) in a cloned group (group_updates). I need to be able to set this field to default to the current date each time a new clone is created as it seems the Today button behaviour is only to highlight entry in the popup calendar and not to actually populate the date field (unlike the time field which populates properly when the Now button is pressed).
This is part of the code so far.
[ 'id' => $prefix . 'group_updates', 'type' => 'group', 'fields' => [ [ 'id' => $prefix . 'job_update_time', 'name' => esc_html__( 'Task Completion Time', 'text-domain' ), 'type' => 'time', ], [ 'id' => $prefix . 'job_update_date', 'type' => 'date', 'name' => esc_html__( 'Task Date', 'text-domain' ), 'clone_default' => true, ],I'm using Builder if that makes any difference.
What is the best way to achieve this?
Thanks.
March 15, 2021 at 12:49 PM #24918Long Nguyen
ModeratorHi Martin,
You should use the code to set the default value (current date) for the first field, then clone fields will inherit the default value.
[ 'id' => $prefix . 'job_update_date', 'type' => 'date', 'name' => esc_html__( 'Task Date', 'text-domain' ), 'clone' => true, 'clone_default' => true, 'std' => date("Y-m-d"), ],Get more details about cloneable sub-fields here https://docs.metabox.io/extensions/meta-box-group/#clone-default-values.
March 15, 2021 at 1:26 PM #24920Martin
ParticipantThanks for that.
A follow up question (probably a bit of a beginners' question) - is it possible to set the value for the std in the Builder?
I've popped it in both the Default value box for the field and as a Custom attribute but both result in the function being set in single quotes in the code and therefore the field value becomes the quoted string rather than the result of the function.
March 15, 2021 at 3:58 PM #24924Long Nguyen
ModeratorHi,
No, it's not possible. The Builder does not support using a function in the input field settings. So I recommend using the code to run a PHP function.
-
AuthorPosts
- You must be logged in to reply to this topic.