Created
June 24, 2015 23:18
-
-
Save andrewwoods/f91100cb3a4ed66961a7 to your computer and use it in GitHub Desktop.
WordPress Plugin Autoloader
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
/* | |
* Include this in your primary plugin file | |
* | |
* Create a 'classes' directory in your plugin directory. | |
* To load a class nameed So_Awesome, it should be in classes/class-so-awesome.php | |
*/ | |
function your_plugin_autoloader( $class_name ) { | |
$slug = sanitize_title_with_dashes( $class_name, '', 'save' ); | |
$slug = str_replace('_', '-', $slug); | |
$file = 'class-' . $slug . '.php'; | |
$file_path = plugin_dir_path( __FILE__ ) . 'classes/' . $file; | |
if ( file_exists( $file_path ) ) { | |
include_once $file_path; | |
} | |
} | |
spl_autoload_register( 'your_plugin_autoloader' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment