Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Autoloader for Wordpress
<?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.")
);
}
}
@Didweb

This comment has been minimized.

Copy link
Owner Author

commented Jul 10, 2018

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');


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.