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
    Feb 12 '21 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. Feb 13 '21 at 13:17
  • Did you customize the code from the autoloader, of use it as is?
    – Q Studio
    Feb 13 '21 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. Feb 13 '21 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.