Troubleshooting CMB2 Time and Date Field Issues: Default Values, Formatting, and Saving
Content
Working with time and date fields in CMB2 can sometimes be tricky. Users often encounter issues with default values not appearing, formats not applying, or data not saving in the desired format. This guide compiles common problems and their solutions based on community discussions.
Common Issue 1: Default Value Not Working
Problem: A user creating a custom field type found that the 'default' parameter for a text_time field was not being applied.
Why it happens: The 'default' parameter may not be respected in all contexts, especially when the field is part of a more complex custom field implementation or when a value already exists in the database.
Solution: For a more reliable way to set a default, consider using the default_cb parameter with a callback function. For example, 'default_cb' => 'your_callback_function'. This callback can return a default value dynamically, ensuring it is set even if other methods fail.
Common Issue 2: Time Format Not Applying
Problem: Users report that the 'time_format' => 'h:i:s A' parameter does not change the format of the saved or displayed time.
Why it happens: This appears to be a known issue where the format parameter is not fully implemented for the time picker UI in some versions of CMB2.
Solution: As a workaround, you can format the time when retrieving it from the database. Use PHP's date() and strtotime() functions to convert the stored value into your desired format.
$mytime = date( 'h:i:s A', strtotime( get_post_meta( get_the_ID(), 'my_meta_key', true ) ) );
Common Issue 3: Needing to Save as a Timestamp
Problem: Users want to save a time or date as a Unix timestamp for easier querying and sorting but need to maintain a user-friendly input interface.
Why it happens: The standard text_time and text_date fields save values in a human-readable format, which is not ideal for performing meta queries in WordPress.
Solution: Use a timestamp-based field type instead. CMB2 provides several fields that save Unix timestamps automatically, offering the best of both worlds: a user-friendly UI and a sortable integer value in the database.
- For a date only: Use
text_date_timestamp. - For a date and time: Use
text_datetime_timestamp.
These fields will save a UTC timestamp, which you can then convert to any format on the front end of your site.
Common Issue 4: Cannot Delete Field Content
Problem: For text_time fields, particularly within repeatable groups, users cannot delete the value. After manually clearing the input and moving focus away, the time picker automatically repopulates the field.
Why it happens: This is likely due to the jQuery time picker plugin's behavior, which is designed to always have a value.
Solution: This may require a custom JavaScript solution to override the default behavior of the time picker and allow for empty values. Inspecting the CMB2 source and the jQuery UI timepicker documentation would be the best course of action for developers looking to implement this.
Conclusion
Many common issues with CMB2 time and date fields can be resolved by using the appropriate field type for your data storage needs and applying formatting on output. For more persistent bugs, checking the official CMB2 GitHub repository for ongoing discussions and upcoming fixes is recommended.
Related Support Threads Support
-
Default value for text_time not working using own custom field typehttps://wordpress.org/support/topic/default-value-for-text_time-not-working-using-own-custom-field-type/
-
Save time field in secondhttps://wordpress.org/support/topic/save-time-field-in-second/
-
REST API. POST requesthttps://wordpress.org/support/topic/rest-api-post-request/
-
Time Formats…https://wordpress.org/support/topic/time-formats/
-
Saving text_time as unix timestamp insteadhttps://wordpress.org/support/topic/saving-text_time-as-unix-timestamp-instead/
-
text_date save as unix timestamp, display as m/d/Yhttps://wordpress.org/support/topic/text_date-save-as-unix-timestamp-display-as-mdy/
-
Overriding/replacing hidden field valuehttps://wordpress.org/support/topic/overriding-replacing-hidden-field-value/
-
Text Date Fieldhttps://wordpress.org/support/topic/text-date-field/
-
date_format not workinghttps://wordpress.org/support/topic/date_format-not-working/
-
How add cho language time for type “text_time”https://wordpress.org/support/topic/how-add-cho-language-time-for-type-text_time/
-
Admin post tableshttps://wordpress.org/support/topic/admin-post-tables/
-
Bug in text_datetime_timestamp_timezone fieldhttps://wordpress.org/support/topic/bug-in-text_datetime_timestamp_timezone-field/
-
Date formats…https://wordpress.org/support/topic/date-formats-2/
-
text_time field content not deletable after clicking intohttps://wordpress.org/support/topic/text_time-field-content-not-deletable-after-clicking-into/