Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active November 25, 2019 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/86e200915a4f5a9c6452f83bb6829bd1 to your computer and use it in GitHub Desktop.
Save damiencarbery/86e200915a4f5a9c6452f83bb6829bd1 to your computer and use it in GitHub Desktop.
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