Site logo

[Resolved] Convert custom field DATE to format set in WordPress settings [used workaround]

Home Forums Support [Resolved] Convert custom field DATE to format set in WordPress settings [used workaround]

Home Forums Support Convert custom field DATE to format set in WordPress settings [used workaround]

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #2476459
    John

    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

    #2476461
    Fernando
    Customer Support

    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

    #2476504
    John

    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.

    #2476514
    Fernando
    Customer Support

    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/

    #2477179
    John

    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?

    #2477441
    Fernando
    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 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.

    #2477446
    John

    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?

    #2477447
    Fernando
    Customer Support

    I see. Glad you found a different approach.

    There are no drawbacks. They function similarly.

    #2477450
    John

    Thanks!!

    #2477452
    Fernando
    Customer Support

    You’re welcome, John!

    #2478229
    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);
    #2480063
    Fernando
    Customer Support

    I see. Thank you for sharing this!

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.