Skip to content

Instantly share code, notes, and snippets.

@drrobotnik
Created December 11, 2014 20:50
Show Gist options
  • Save drrobotnik/6909ed413f27128b4d0e to your computer and use it in GitHub Desktop.
Save drrobotnik/6909ed413f27128b4d0e to your computer and use it in GitHub Desktop.
wp plugin autoloader
<?php /*
Plugin Name: WP Plugin Autoloader
Plugin URI: http://www.caavadesign.com
Description: Proposed usage of a WP autoloader
Version: 0.0.1
Author: Brandon Lavigne
*/
# Plugin Autoloader
function run_plugin_autoloader() {
global $wp_filter;
$extensions = array(".php", ".class.php", ".inc");
$path = plugin_dir_path( __FILE__ );
foreach( $wp_filter['plugin_autoloader'] as $priority){
foreach ($priority as $filter => $value) {
if( 'run_plugin_autoloader' == $filter)
return false;
$className = ltrim($filter, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $filter) . '.php';
$fileName = $path . $filter . '/' . $fileName;
require $fileName;
}
}
}
add_action('plugin_autoloader', 'run_plugin_autoloader',9999); // Don't be a jerk and load after this...
do_action('plugin_autoloader');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment