Archived topic
Convert custom field DATE to format set in Wordpress settings [used workaround]
11 replies · Started by John on December 28, 2022
Hi GenerateTeam :)
I have an event custom post type with an event_start_date custom 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_date appears in GB Query Loops and within posts, in GP Element block templates. I basically want to the event_start_date dates to be converted to the default Wordpress settings wherever they show up on the site.
Is that possible? Thank you for your help
Hi John,
It's possible.
What you can do is retrieve the return value of event_start_date through 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
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.
I see. The code above is more of a manual change in format. You can replace l F j, Y with 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/
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?
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 of convert-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-HERE in the code.
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?
I see. Glad you found a different approach.
There are no drawbacks. They function similarly.
Thanks!!
You're welcome, John!
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);
I see. Thank you for sharing this!
