Wordpress: we need to require several shared files

Sometimes we need to write some duplicate functions/classes to the themes and include in functions.php for a custom template. I decided to create a directory called inc (for example: wp-content/themes/A/inc ) and build my classes there and include it in functions.php.I also created the autoload class.

// functions.php
include WP_DIR . '/inc/autoload.php';

But there is a problem. When I want to create another template(into wp-content/themes ), I have to create the inc (for example: wp-content/themes/B/inc ) directory again and copy the previous classes and autoload.php . In fact, templates use several duplicate common classes .

  • What do you suggest to prevent rewriting?
  • Is it possible to write these classes(autoload.php, …) as a plugin in the wp-content/plugins directory and enable it? Does it make sense? Is this correct?
  • Can I create a directory within the theme directory for shared classes?
  • What is the WordPress solution to this issue?

I have to use the child theme scenario. This is the best way.

I’m not familiar with autoload.php, but are you adding your functions to different custom themes? That is what it sounds like. I’m confused by what you mean “…create another template”. If A is one theme and B is another theme, then yes you need to create an “inc” folder in the same location and copy your code/file into every theme.

As for different template files in the SAME theme, then you would create your classes in your inc folder, and then call it from each and every template where you want that function, though I can’t remember running a function in a template file. Or even better, you would call the function in the appropriate files in the template-parts folder, though I’ve seen that folder also called partials.

Did I understand you correctly?

1 Like

Theme or child theme, you have to create an inc folder or whatever folder name for each theme/child theme.

1 Like

Thank you for taking the trouble to help me.I do appreciate it.

Hello!

I know this has been solved, but maybe these options may also help.

This is an option (creating a plugin), tough you would need to create a dependency and force users to have it previously installed. I wouldn’t use this approach if the plugin is just a set of helper functions that doesn’t provide anything to the user. Also, if the user forgets that this is a requirement and deletes the plugin, they will complain that it doesn’t work (even though you could alert them).

If you mean to share classes among themes (not just one theme and not between a parent and child theme), then yes, it could be accomplished, though it presents the same problems as the plugin: a theme that doesn’t add anything to the user. Also it would add overhead to the development and maintenance.

I would say that there isn’t a native solution except for a parent/child theme, but then again, you would be forcing your users to use both of your themes, where the parent/child themes are meant to be a solution for updates.

Composer (or any other dependency manager) would help with this too. You can create a library of common functions to be shared among themes, then, when you create a new theme you just install the library (require your library as a dependency). You can setup a private github repository with your library and use that as a repository for composer.

And last option I can think of is to have a starter theme or theme template, where the project would be ready to start working.

Hope it helps :slight_smile:

1 Like

You could create the file in the must use plugins folder, though you will probably have to create that folder. That is how I created custom post types. You are going to have to read up on it in the developer docs but just create a folder called mu-plugins and put it in wp-content folder. That way even if the user changes themes or plugins, that functionality remains.

For example, that is where I created my custom post types plugin which was simple to do. I just used the register_post_type function and add_action. I can’t rememeber if I did something in functions.php or not… What isn’t simple is writing the code to have your custom post types show up in search results.

2 Likes