Created
July 10, 2018 13:15
Star
You must be signed in to star a gist
Simple Autoloader for Wordpress
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 | |
spl_autoload_register('name_your_plugin_autoloader'); | |
function name_your_plugin_autoloader($class_name) | |
{ | |
$plugin_name = 'name_your_plugin'; | |
if (false === strpos($class_name, $plugin_name)) { | |
return; | |
} | |
$file_parts = explode('\\', $class_name); | |
$namespace = '/'; | |
for ($i = 1; $i < count($file_parts); $i++) { | |
$current = strtolower($file_parts[ $i ]); | |
$namespace .=$current.'/'; | |
} | |
$namespace = substr($namespace, 0, -1); | |
$filepath = dirname(dirname(__FILE__)) . '/' . $plugin_name.'/'.$namespace.".php"; | |
if (file_exists($filepath)) { | |
include_once($filepath); | |
} else { | |
wp_die( | |
esc_html("[Autoloader Alert!] - The file attempting to be loaded at $filepath does not exist.") | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use:
Place the file in the root of the plugin and change the text
name_your_plugin
by the corresponding one.In the main file and in the rest of the pages that are needed.
require_once(dirname(__FILE__) . '/autoloader.php');
Wie zu verwenden:
Legen Sie die Datei in das Stammverzeichnis des Plugins und ändern Sie den Text
name_your_plugin
durch den entsprechenden.In der Hauptdatei und im Rest der Seiten, die benötigt werden.
require_once(dirname(__FILE__) . '/autoloader.php');
Forma de uso:
Colocar el archivo en la raíz del plugin y cambiar el texto
name_your_plugin
por el que corresponda.En el archivo principal y en el resto de páginas que se necesite.
require_once(dirname(__FILE__) . '/autoloader.php');