-1

I am following WordPress's naming convention where a class My_Class should reside in the file named class-my-class.php. I used this autoloader for WordPress, written by Rarst. If I print out the $class_name variable, I see that the prefix class is appended to the folder name and not the class file. I had the same issue with some other autoloader I used earlier. I can do a little bit of string manipulation and get what I want but I want to know what is the exact issue.

What could be wrong?

Rutwick Gangurde
  • 4,772
  • 11
  • 53
  • 87
  • What is your code looking like? How do you implement the autoloader class for wordpress? The autoloader itself only prepends a directory, if given in the constructor. Otherwise it 's the actuell directory the autoloader is located in. I guess you 're using the wrong classnames for instance. – Marcel Jul 17 '15 at 10:02
  • I used an autoloader which is supposed to follow WP's coding standards. It works either ways. The classnames are right. – Rutwick Gangurde Jul 17 '15 at 12:34

2 Answers2

0

I just had a look at this autoloader you linked to, I would say it should be on line 21 something like this :

$class_path = $this->dir . '/class-' . strtolower( str_replace( '_', '-', basename( $class_name ) ) ) . '.php';

basename takes only the file part + file extension of the path.

You need also to check where this autoloader file is, because $this->dir is set to DIR, which is the directory where the autoloader file is.

Zimmi
  • 1,599
  • 1
  • 10
  • 14
0

Use a flexible loader. try this one.

function TR_Autoloader($className)
{
$assetList = array(
    get_stylesheet_directory() . '/vendor/log4php/Logger.php',
    // added to fix woocommerce wp_email class not found issue
    WP_PLUGIN_DIR . '/woocommerce/includes/libraries/class-emogrifier.php'
    // add more paths if needed.
);

// normalized classes first.
$path = get_stylesheet_directory() . '/classes/class-';
$fullPath = $path . $className . '.php';

if (file_exists($fullPath)) {
    include_once $fullPath;
}

if (class_exists($className)) {
    return;
} else {  // read the rest of the asset locations.
    foreach ($assetList as $currentAsset) {
        if (is_dir($currentAsset)) {
            foreach (new DirectoryIterator($currentAsset) as $currentFile) {
                if (!($currentFile->isDot() || ($currentFile->getExtension() <> "php")))
                    require_once $currentAsset . $currentFile->getFilename();
            }
        } elseif (is_file($currentAsset)) {
            require_once $currentAsset;
        }

    }
}
}

spl_autoload_register('TR_Autoloader');
Hugo R
  • 2,613
  • 1
  • 14
  • 6