Archived topic
Dynamic Block Not Using Custom Date Output
6 replies · Started by Jim on December 10, 2021
Hello,
I created a Block Element for my post meta to customize the layout. I used "Post Date" for one of the dynamic text types, and "Published Date" for the Date type.
It looks great, but it's no longer using a filter I created to control the date output. Here's my filter:
add_filter( 'generate_post_date_output', function( $output, $time_string ) {
$time_string = '<time class="entry-date published" datetime="%1$s" itemprop="datePublished">on %2$s</time>';
if ( get_field( 'date_updated') ) { // the date_updated variable was created in ACF
$time_string = '<time class="entry-date updated-date" datetime="%3$s" itemprop="dateModified">| Updated %4$s</time>';
}
$time_string = sprintf( $time_string,
esc_attr( get_the_date( 'c' ) ),
esc_html( get_the_date() ),
esc_attr( get_the_modified_date( 'c' ) ),
esc_html( get_field( 'date_updated') )
);
return sprintf( '<span class="posted-on">%s</span> ',
$time_string
);
}, 10, 2 );
I created this filter because I don't want the on-page date to be updated every time I make a minor edit. I prefer to explicitly set the updated date when I feel I've made a substantive change to the post.
How can I get the dynamic date in my Block Element to recognize/use my generate_post_date_output filter?
Thank you!
Hi there,
Dynamic blocks have a different filter. This should work:
add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
$text_type = $block['attrs']['gpDynamicTextType'];
if ( 'post-date' === $text_type ) {
$text = sprintf(
'<time class="entry-date published" datetime="%1$s">%2$s</time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( '', $id ) )
);
$is_updated_date = isset( $block['attrs']['gpDynamicDateType'] ) && 'updated-date' === $block['attrs']['gpDynamicDateType'];
if ( ! empty( $block['attrs']['gpDynamicDateUpdated'] ) || $is_updated_date ) {
if ( get_field( 'date_updated') ) {
$text = sprintf(
'<time class="entry-date updated-date" datetime="%1$s">%2$s</time>',
esc_attr( get_the_modified_date( 'c', $id ) ),
esc_html( get_the_modified_date( '', $id ) )
);
}
}
}
return $text;
}, 10, 2 );
Hope this helps :)
Thanks so much, Tom! I added this via the Code Snippets plugin, but it doesn't seem to have any effect. A post using my dynamic block with a date_updated set to today still displays the original publish date.
I experimented with this a bit and commented a few things out (admittedly I don't know what that code does). I also changed it to actually use my date_updated value. It appears to be working now, but not fully tested. Any reason not to use it as shown below?
add_filter( 'generate_dynamic_element_text', function( $text, $block ) {
$text_type = $block['attrs']['gpDynamicTextType'];
if ( 'post-date' === $text_type ) {
$text = sprintf(
'<time class="entry-date published" datetime="%1$s">%2$s</time>',
esc_attr( get_the_date( 'c', $id ) ),
esc_html( get_the_date( '', $id ) )
);
//$is_updated_date = isset( $block['attrs']['gpDynamicDateType'] ) && 'updated-date' === $block['attrs']['gpDynamicDateType'];
//if ( ! empty( $block['attrs']['gpDynamicDateUpdated'] ) || $is_updated_date ) {
if ( get_field( 'date_updated') ) {
$text = sprintf(
'<time class="entry-date updated-date" datetime="%1$s">%2$s</time>',
esc_attr( get_the_modified_date( 'c', $id ) ),
esc_html( get_field( 'date_updated') ) // this is the value I actually want to display on the page
);
// }
}
}
return $text;
}, 10, 2 );
Nope, that's fine. The part you commented out is the code that checks whether you've chosen to display the updated date inside the options. So with that commented out it will always display the updated date if the post meta is set regardless of the dynamic options.
Perfect! Thank you!
No problem :)