Home › Forums › Support › Convert custom field DATE to format set in WordPress settings [used workaround]
- This topic has 11 replies, 2 voices, and was last updated 3 years, 3 months ago by
Fernando.
-
AuthorPosts
-
December 28, 2022 at 10:50 pm #2476459
John
Hi GenerateTeam 🙂
I have an
eventcustom post type with anevent_start_datecustom field. I’m trying to convert the date format in the front-end to whichever format is set in the WordPress admin settings as default date format.Throughout the course of my research, I have come to the conclusion that it is best practice to store the date in the database into a database-friendly format rather than a human-friendly format, and then convert the format on the fly for display in the front-end (if you disagree let me know, but unless I hear otherwise this is my premise).
My goal is therefore to save the date in the default database format, and convert it on the fly on the front-end to whichever format is selected into the WordPress admin settings. That way my users can change that format from the settings without having to touch the code. The important part of my challenge is that I’m not trying to convert the date to a specific format, rather I’m trying to make it match the default format set in the WordPress settings.
The
event_start_dateappears in GB Query Loops and within posts, in GP Element block templates. I basically want to theevent_start_datedates to be converted to the default WordPress settings wherever they show up on the site.Is that possible? Thank you for your help
December 28, 2022 at 10:56 pm #2476461Fernando Customer Support
Hi John,
It’s possible.
What you can do is retrieve the return value of
event_start_datethrough a GB Headline Block’s dynamic feature.Then, give it a class of
convert-to-fjy. Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/Then, add this PHP snippet:
add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){ if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'convert-to-fjy' ) !== false ) { $timestamp = strtotime($content); $my_date = sprintf( '<time datetime="%1$s">%2$s</time>', date('c', $timestamp), date('l F j, Y', $timestamp) ); return $my_date; } return $content; }, 10, 3);Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
December 28, 2022 at 11:49 pm #2476504John
Thanks Fernando, but that seems to just convert the date to
l F j, Y. What if the next admin changes the WordPress date format settings through the admin UI to something else? Now I have inconsistent date formats throughout the site.That’s why I’m looking to set the date format for the custom field to the same date format as what’s defined in the WordPress settings. Not the specific date format currently set in the settings, but the same ‘variable’ so to speak, so that when the date format is changed in the settings, it changes accordingly for the display of that custom field.
December 29, 2022 at 12:00 am #2476514Fernando Customer Support
I see. The code above is more of a manual change in format. You can replace
l F j, Ywith your preferred format.I’m not sure if changing it based on the format set in the General Settings is possible though. I can’t seem to find a document regarding this.
It may be good to raise a topic here as well: https://wordpress.org/support/forum/wp-advanced/
December 29, 2022 at 10:27 am #2477179John
Ok fair enough, thank you. I will raise a topic on that forum – thank you for that resource.
In the meantime I will ‘hard code’ the format in the code snippet then. The snippet works to change the date format in a GB Query Loop, but it doesn’t work in the post template I created (using GP Dynamic Options instead of GB Dynamic Data). Is there a quick fix to that code snippet to make it work with Dynamic Options as well by any chance?
December 29, 2022 at 5:29 pm #2477441Fernando Customer Support
For that code, there isn’t a way since it’s a GB filter.
To make it work for both, you can try these steps:
1. Disable Dynamic data.
2. Add “placeholder” as the text.
3. Give the Headline a class ofconvert-to-fjy
4. Add this snippet:add_filter( 'render_block', function( $block_content, $block ) { if ( !is_admin() && ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'convert-to-fjy' ) !== false ) { $myreplace1 = 'placeholder'; $post_meta_date = get_post_meta( get_the_ID(), 'ADD-META-FIELD-NAME-HERE'); $timestamp = strtotime($post_meta_date); $my_date = sprintf( '<time datetime="%1$s">%2$s</time>', date('c', $timestamp), date('l F j, Y', $timestamp) ); $myinsert1 = $my_date; $block_content = str_replace( $myreplace1, $myinsert1 , $block_content ); } return $block_content; }, 10, 2 );5. Add the date meta field slug/name here
ADD-META-FIELD-NAME-HEREin the code.December 29, 2022 at 5:37 pm #2477446John
Oh boy, that’s pretty convoluted, that’s a bit more complicated than I want my users to have to deal with in the future.
Fortunately, I was able to simply replace the GP Dynamic option by the GB Dynamic data to retrieve the same custom field, and it is now converted appropriately. Phew!
Is there any drawback to using GB’s Dynamic data vs. GP’s?
December 29, 2022 at 5:43 pm #2477447Fernando Customer Support
I see. Glad you found a different approach.
There are no drawbacks. They function similarly.
December 29, 2022 at 5:47 pm #2477450John
Thanks!!
December 29, 2022 at 5:52 pm #2477452Fernando Customer Support
You’re welcome, John!
December 30, 2022 at 10:07 am #2478229John
Revisiting this, as I’ve found a way to set the date format to “whatever-is-defined-in-the-Wordress-settings”, which I find a much more elegant solution than hardcoding it.
Here is my code snippet, if anyone is looking for the same thing:
add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){ if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'human-friendly-date' ) !== false ) { // check for css class to identify elements with date to convert. Here 'human-friendly-date' $timestamp = strtotime($content); $df = get_option('date_format'); $hol_date = sprintf( '<time datetime="%1$s">%2$s</time>', date('c', $timestamp), date($df, $timestamp) ); return $hol_date; } return $content; }, 10, 3);January 1, 2023 at 5:01 pm #2480063Fernando Customer Support
I see. Thank you for sharing this!
-
AuthorPosts
- You must be logged in to reply to this topic.