Last active
April 15, 2024 10:42
-
-
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/
This file contains 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 | |
/** | |
* 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