Skip to content

Instantly share code, notes, and snippets.

@Fleuv
Last active September 16, 2018 18:11
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 Fleuv/fbdbcdc822be428486a349036efc51dd to your computer and use it in GitHub Desktop.
Save Fleuv/fbdbcdc822be428486a349036efc51dd to your computer and use it in GitHub Desktop.
<?php
if (!function_exists('render_template')) {
/**
* Setup replacements and find and replace them in a specific template
*
* @param string $filename the filename of the template including extension
* @param string $dir (optional) the directory where the file is hosted relative to your theme
* @param string $open the opening wrapper for the placeholder
* @param string $close the closing wrapper for the placeholder
* @return string
*/
function render_template($filename, $dir = '', $open = '', $close = '') {
// Put all common post data in the list of replacements
$replacements = [
'post_id' => get_the_ID(),
'post_title' => get_the_title(),
'post_content' => get_the_content(),
'post_excerpt' => get_the_excerpt(),
'post_permalink' => get_permalink(),
'post_thumbnail' => get_the_post_thumbnail(),
'post_date' => get_the_date(),
'post_format' => get_post_format(),
'post_status' => get_post_status(),
'post_edit_link' => get_edit_post_link(),
];
// Retrieve the post's custom meta and filter out the WordPress internally used custom meta
$post_meta_keys = get_post_custom_keys();
$post_meta_keys = array_diff($post_meta_keys, array_filter($post_meta_keys, function ($v) {
return substr(trim($v), 0, 1) === '_';
}));
// Add the retrieved post custom meta to the list of replacements if they aren't already
foreach ($post_meta_keys as $meta_key) {
$replacements_key = 'post_meta_' . trim($meta_key);
if (array_key_exists($replacements_key, $replacements)) continue;
$replacements[$replacements_key] = get_post_meta(get_the_ID(), $meta_key, true);
}
/**
* Filter replacements for a specific template
*
* @since 4.9.9
*
* @param array $replacements Array of replacements for the template file, the key is a descriptive
* placeholder and the value is the actual value to be rendered as well.
* @param string $filename The filename of the template to be rendered including the extension.
*/
$replacements = apply_filters('render_template_replacements', $replacements, $filename);
/**
* Filter replacements for a specific template
*
* @since 4.9.9
*
* @param array $replacements Array of replacements for the template file, the key is a descriptive
* placeholder and the value is the actual value to be rendered as well.
*/
$replacements = apply_filters("render_template_replacements_{$filename}", $replacements);
// Load the template from the a sub directory, define the constant to change the default
$template_dir = !empty($dir) ? $dir : (defined('WP_TEMPLATE_DIR') ? WP_TEMPLATE_DIR : 'tpl');
// Determine what file to load preferring the
$filename = '/' . $template_dir . '/' . $filename;
$file = (file_exists(get_stylesheet_directory() . $filename) ?
get_stylesheet_directory() : get_template_directory()) . $filename;
// Load the template
if (array_key_exists('wp_render_templates', $GLOBALS)
&& is_array($GLOBALS['wp_render_templates'])
&& array_key_exists($file, $GLOBALS['wp_render_templates'])) {
$template = $GLOBALS['wp_render_templates'][$file];
} else {
ob_start();
if (file_exists($file)) {
require_once $file;
}
$template = ob_get_clean();
$GLOBALS['wp_render_templates'][$file] = $template;
}
// Prepare the replacement placeholders and values
$keys = array_keys($replacements);
$values = array_values($replacements);
// Setup the placeholder identifiers
$open = !empty($open) ? $open : (defined('WP_TEMPLATE_OPEN') ? WP_TEMPLATE_OPEN : '{');
$close = !empty($close) ? $close : (defined('WP_TEMPLATE_CLOSE') ? WP_TEMPLATE_CLOSE : '}');
$keys = array_map(function ($v) use ($open, $close) {
// Escape the un-escaped '/'
$v = preg_replace("/(?<!\\\\)(?:\\\\\\\\)*\K\\//", '\/', $v);
// Format the key to a regex pattern enclosed in curly braces
return '/' . $open . $v . $close . '/';
}, $keys);
return preg_replace($keys, $values, $template);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment