Skip to content

Instantly share code, notes, and snippets.

@knoonrx
Last active April 13, 2017 12:34
Show Gist options
  • Save knoonrx/b738fd3d8da765acb708 to your computer and use it in GitHub Desktop.
Save knoonrx/b738fd3d8da765acb708 to your computer and use it in GitHub Desktop.
Autoloader for wordpress theme
<?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