Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Filter Gutenburg block before display - Search and replace some tags in a block to dynamically insert the title and excerpt https://www.damiencarbery.com/2019/11/filter-gutenburg-block-before-display/
<?php
/*
Plugin Name: Filter Gutenburg block before display
Plugin URI: https://www.damiencarbery.com/2019/11/filter-gutenburg-block-before-display/
Description: Search and replace some tags in a block to dynamically insert the title and excerpt.
Author: Damien Carbery
Version: 0.1
*/
add_filter( 'render_block', 'dcwd_block_tag_substitution', 10, 2 );
function dcwd_block_tag_substitution( $block_content, $block ) {
if ( is_singular() ) {
$title = get_the_title();
// Get excerpt from post object otherwise there is an infinite loop using
// get_the_excerpt() as it will call 'render_block' and around and around
global $post;
$excerpt = $post->post_excerpt;
$tags = array( '~TITLE~', '~TAGLINE~' );
//$replacements = array( get_the_title(), get_the_excerpt() );
$replacements = array( $title, $excerpt );
return str_replace( $tags, $replacements, $block_content );
}
return $block_content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.