Created
November 7, 2014 14:43
-
-
Save rxnlabs/5e3ecd21438a8942065d to your computer and use it in GitHub Desktop.
PHP - autoload extra code based on the WordPress template being used. Useful when separating your template functionality into multiple files.
This file contains hidden or 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 | |
add_filter( 'template_include', array( $this, 'template_additional_code') ); | |
/** | |
* Load page specific code for each template. | |
* | |
* Load additional code on a per template basis. Code in files are only useful for the particular template. | |
* | |
* @author De'Yonte W.<https://github.com/rxnlabs> | |
* @return string Path to file used to render the post | |
*/ | |
function template_additional_code( $page_template ){ | |
global $post; | |
$template_filename = explode('/',$page_template); | |
$template_filename = $template_filename[count($template_filename) - 1]; | |
// load the template specific for each template loaded | |
if( file_exists(__DIR__.'/template-specific-code/'.$template_filename) ){ | |
require __DIR__.'/template-specific-code/'.$template_filename; | |
} | |
return $page_template; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment