Skip to content

Instantly share code, notes, and snippets.

@Didweb
Created July 10, 2018 13:15
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Didweb/c4e8c930d3e612116c3cc710d3d9d642 to your computer and use it in GitHub Desktop.
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
Copy link
Author

Didweb 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