Skip to content

Instantly share code, notes, and snippets.

@cdils
Last active July 19, 2023 17:40
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 cdils/7630064f181e63a4b22869dd07bd617d to your computer and use it in GitHub Desktop.
Save cdils/7630064f181e63a4b22869dd07bd617d to your computer and use it in GitHub Desktop.
<?php
/**
* Get a reusable block by its title.
*
* @param string $title The post title of the reusable block.
* @param bool $filter_results Whether the results should be filtered through the_content filter.
* @return string|null The reusable block content if found, null otherwise.
*/
function get_reusable_block( $title, $filter_results = true ) {
$query = new WP_Query(
array(
'post_type' => 'wp_block',
'title' => $title,
)
);
if ( empty( $query->post ) ) {
return null; // No reusable block found.
}
$reusable_block = $query->post;
if ( $filter_results ) {
return filter_reusable_block_content( $reusable_block->post_content );
}
return $reusable_block->post_content;
}
/**
* Apply the_content filter to the given reusable block content.
*
* @param string $content The reusable block content.
* @return string The filtered content.
*/
function filter_reusable_block_content( string $content ): string {
return apply_filters( 'the_content', $content );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment