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?

Modify the path to move your inc folder one level up, to the themes folder:

include dirname(WP_DIR) . '/inc/autoload.php';

or

include WP_DIR . '../inc/autoload.php';
1 Like

Is not there a better way?
Because I might delete a template And again, the files need to be moved.

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

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.