Last active
April 13, 2017 12:34
-
-
Save knoonrx/b738fd3d8da765acb708 to your computer and use it in GitHub Desktop.
Autoloader for wordpress theme
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 | |
/** | |
* WordPress Theme Autoload | |
* Place on Root Theme | |
* Create a folder as classes | |
* This peace of code adds a autoloader into any wordpress theme you have | |
* to use this only follow the simple steps bellow | |
* create all your class files inside the folder named class | |
* and then just include this autoloader.php file in your theme functions.php | |
* require_once TEMPLATEPATH . '/autoloader.php'; | |
* https://github.com/knoonrx | |
* https://gist.github.com/knoonrx | |
**/ | |
add_action ( 'init' , 'class_loader' ); | |
function class_loader () { | |
spl_autoload_register ( 'template_autoload' ); | |
} | |
function template_autoload ( $class ) { | |
try | |
{ $extension = '.php'; | |
$separator = '\\'; | |
$class = str_replace($separator, DIRECTORY_SEPARATOR ,$class); | |
if ( file_exists ( TEMPLATEPATH . DIRECTORY_SEPARATOR . $class . $extension ) ) | |
require_once TEMPLATEPATH . DIRECTORY_SEPARATOR . $class . $extension; | |
} catch(Error $ex) { | |
return 'Error: ' . $ex->getMessage(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment