Skip to content

Instantly share code, notes, and snippets.

@paulchubatyy
Created July 13, 2010 11:20
Show Gist options
  • Save paulchubatyy/473743 to your computer and use it in GitHub Desktop.
Save paulchubatyy/473743 to your computer and use it in GitHub Desktop.
Sample Autoload function for WordPress plugin
<?php
/**
* Plugin autoloader. Coverts class name to path and loads it
* @param string $className
*/
function autoload($className)
{
if (class_exists($className))
return;
if (self::$pluginAbsolutePath == NULL)
self::$pluginAbsolutePath = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR
. 'foo' . DIRECTORY_SEPARATOR;
$medium = str_replace('bar', 'classes', strtolower($className));
$path = str_replace('_', DIRECTORY_SEPARATOR, $medium);
// Putting altogether
$path = self::$pluginAbsolutePath . $path . '.php';
if (file_exists($path) and is_readable($path))
require_once $path;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment