3

We have core classes created for theme and we are moving those classes to client-mu-plugins. Is there any way we can load all of the classes without adding require_once function for every single files? I tried spl_autoload_register() function in every single ways but It's not loading.

File structure in plugin now

plugin_folder
  -inc
      - frontend
         -class-post.php
         -class-design.php
         -class-video.php
      - modules
         -class-query.php
         -class-config.php
      - queries
  - plugin-name.php

All of the class files also have Namespace defined. I'm not getting an idea on how to include those in custom plugins so it would work like it was working in theme folder. Could we use autoloader in someway? Appreciate your comments and help.

Namespace are defined on class files are like:

Foo\Frontend
Foo\Modules
Foo\Queries

Tried: https://github.com/tommcfarlin/simple-autoloader-for-wordpress/

Thanks

4
  • 2
    Perhaps show the code you are using in your autoloader - doing this from a namespace in a plugin is both possible and a good solution - this can be done by string matching the namespace and loading based on that.
    – Q Studio
    Commented Feb 12, 2021 at 22:16
  • @QStudio I'm trying this autoload file. Initially thought it would be relatively simple by using just spl_autoload_register(). I've also added namespace of class files. Commented Feb 13, 2021 at 13:17
  • Did you customize the code from the autoloader, of use it as is?
    – Q Studio
    Commented Feb 13, 2021 at 18:08
  • Not actually and it supposed to detect class files from plugin where we put it. I did tried custom code using spl_autoload_register() but I'm not getting any Namespace not sure how to debug that as well. Commented Feb 13, 2021 at 18:25

1 Answer 1

0

Placed in the theme (e.g. in functions.php), something like the below tweaked for your context might work.

I assumed you want to include all the .php files from specific directories but only for the specified plugins when they are active:

add_action('after_setup_theme', 'add_child_plugins');

function add_child_plugins() {
    $include_plugins = array('plugin1',); //plugin folder names
    //relevant folders inside those plugin, assuming consistent
    $include_subfolders = array('inc/frontend', 'inc/modules',);
    foreach ($include_plugins as $plugin) {
        $plugin_str = $plugin . '/' . $plugin . '.php';
        // asssume only need from activated plugins
        //if (is_plugin_active($plugin_str)) { uncomment for normal non mu-plugins
            foreach ($include_subfolders as $subfolder) {
                $path = WPMU_PLUGIN_DIR . "/$plugin/$subfolder/*.php";
                foreach (glob($path) as $file) {
                    include_once $file;
                }
            }
        //}uncomment for normal non mu-plugins
    }
}

I'm sure there are alternative more elegant or sophisticated ways :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.