Skip to content

Instantly share code, notes, and snippets.

@vanaf1979
Last active April 15, 2024 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vanaf1979/a3c02da99a194e8f2bfe7f228a61cfb2 to your computer and use it in GitHub Desktop.
Save vanaf1979/a3c02da99a194e8f2bfe7f228a61cfb2 to your computer and use it in GitHub Desktop.
Conditionally loading a custom template in WordPress. https://since1979.dev/snippet-006-conditionally-loading-a-custom-template/
<?php
/**
* template_include_callback.
*
* Maybe redirect to a different template file
*
* @see https://since1979.dev/snippet-006-conditionally-loading-a-custom-template/
*
* @uses is_page https://developer.wordpress.org/reference/functions/is_page/
* @uses locate_template https://developer.wordpress.org/reference/functions/locate_template/
*
* @param String $template The path of the template to include.
* @return String
*/
function template_include_callback(String $template)
{
if (is_page(148))
return locate_template('contact.php');
return $template;
}
/**
* Hook: template_include.
*
* @uses add_action() https://developer.wordpress.org/reference/functions/add_action/
* @uses template_include https://developer.wordpress.org/reference/hooks/template_include/
*/
add_filter('template_include', 'template_include_callback', 100, 1);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment