Last active
July 19, 2023 17:40
-
-
Save cdils/7630064f181e63a4b22869dd07bd617d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
/** | |
* 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