Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eudesgit/6d2aa5578b8f257cc1e9eb5e1ae3496e to your computer and use it in GitHub Desktop.
Save eudesgit/6d2aa5578b8f257cc1e9eb5e1ae3496e to your computer and use it in GitHub Desktop.
WordPress: How to filter the Page Template that is being rendered.
<?php
/**
* template_include Filter callback
*
* Include plugin's template if there's one chosen for the rendering page
*
* @param string $template path
* @return string $template path
*/
public function add_template_filter ( $template ) {
$user_selected_template = get_page_template_slug($post->ID);
// We need to check if the selected template
// is inside the plugin folder
$file_name = pathinfo($user_selected_template, PATHINFO_BASENAME);
$template_dir = $this->template_dir;
if (file_exists($template_dir . $file_name)) {
$is_plugin = true;
}
// If selected template is not empty, it's not the Default Template
// AND if it's a plugin template, we replace the normal flow to
// include the selected template
if ( $user_selected_template != '' AND $is_plugin ) {
$template = $user_selected_template;
}
return $template;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment