Last active
November 25, 2019 21:05
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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