Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Filter the Date in a Dynamic Block Element

  • I bought an old website and I’m slowly updating old posts. In the meantime, I don’t want old dates to be displayed. I was using a filter that I adapted from this that uses generate_post_date_output to modify the date output based on some conditional logic.

    However, now I’m using a block element with the Post Meta Template and my filter stopped working. I think I need to update my filter to use generate_dynamic_text_post_date, but I’m not sure how.

    Can you you get me pointed in the right direction on filter that works with a dynamic block element to conditionally display the publish date (or modified date…I’m using “Replace with updated date,” fyi.)?

    I want to display the published date (or modified date if it’s more recent), but only if it’s after a given date. If it’s before the specified date, don’t display a date at all. Here’s my conditional logic:

    $relaunch_date = strtotime( "2021-09-24" ); //date of site relaunch
     $modified_date = strtotime( get_the_modified_date("Y-m-d") );
    
     if ( ($modified_date < $relaunch_date) ) {
       	[DON’T SHOW THE DATE AT ALL]
    	}

    Hopefully this makes sense, but happy to clarify as needed.

  • Hi James,

    We may need a couple of custom PHP codes for this.

    To clarify, if the post has an “updated” date more recent than the “date of site relaunch”, will this updated date be shown?

  • To answer your question: yes.

    Since I set the date element to “Replace with updated date,” I’m assuming GP has already worked out how to determine whether to display the original publish or updated date. That seems to work fine.

    The point of the filter is to keep it from displaying any date at all if the both the publish and modified dates are older than the “date of site relaunch.”

  • Hi there,

    you can use the render_block filter to alter a blocks $block_content

    Heres an example:

    
    add_filter( 'render_block', function( $block_content, $block ) {
        if ( 
            !is_admin() && 
            ! empty( $block['attrs']['className'] ) &&
            strpos( $block['attrs']['className'], 'custom-date' ) !== false 
        ) {
            $relaunch_date = strtotime( "2021-09-24" ); //date of site relaunch
            $modified_date = strtotime( get_the_modified_date("Y-m-d") );
    
            if ( ($modified_date < $relaunch_date) ) {
                return '';
            }
    
        }
    
        return $block_content;
    }, 10, 2 );

    Note this line:
    strpos( $block['attrs']['className'], 'custom-date' ) !== false

    Here we set the custom-date as a string that we look for in the blocks CSS Classes.
    So select your date block and add that to its Advanced > Additional CSS Class(es)

    If your condition is met we return nothing in the block content.

  • That worked perfectly. Thanks so much!

  • Glad to hear that!

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