Skip to content

Commit

Permalink
Framework: Inject dynamic template into theme template wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Jan 25, 2018
1 parent 864df38 commit d4b5f5e
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,27 @@
* @package gutenberg
*/

/**
* Given template output content, returns the markup of the theme's header and
* footer output, where template output replaces the theme's own body content.
*
* @param string $content Template output content.
*
* @return string Theme content with template output injected.
*/
function gutenberg_get_theme_content( $content ) {
ob_start();
get_header();
get_footer();
$theme_wrapper = ob_get_clean();

return preg_replace(
'/(<body[^>]*>)(.*)<\/body>/s',
sprintf( '$1%s</body>', $content ),
$theme_wrapper
);
}

/**
* Returns true if the passed template name is for a dynamic template, or false
* otherwise. A template is considered dynamic if it is not a file path, and
Expand Down Expand Up @@ -46,7 +67,8 @@ function gutenberg_template_include( $template ) {
$template_posts = $template_post_query->get_posts();
$template_post = $template_posts[0];

echo apply_filters( 'the_content', $template_post->post_content );
$content = apply_filters( 'the_content', $template_post->post_content );
echo gutenberg_get_theme_content( $content );

// End of page lifecycle, abort to prevent theme error messages.
exit;
Expand Down

0 comments on commit d4b5f5e

Please sign in to comment.