6

I'm trying to autoload the vendor/autoload.php from my parent theme into my child theme.

heres what i have tried but to no avail:

function beast_theme_setup() { $var1 = get_theme_root_uri() . '/beast/vendor/autoload.php'; $var2 = get_theme_root_uri() . '/beast/functions.php'; $var = locate_template( array( $var1, $var2), true, false ); var_dump($var); } add_action( 'after_setup_theme', 'beast_theme_setup', 11 ); 

and

include get_theme_root_uri() . '/beast/vendor/autoload.php'; 

Any help on this would be amazing.

Thanks Jake.

8
  • You're using a composer autoloader? where is the project root? Are you the one running composer install, or is this intended to be packaged up and sent elsewhere? You can use get_template_directory to get the parent theme folder path Commented Sep 3, 2016 at 14:53
  • @TomJNowell File structure, yes i'm the one running composer install, get_template_directory seems to only get the child-theme link as i want to require the parent functions.php file / autoload.php file in the child theme Commented Sep 3, 2016 at 15:01
  • get_template_directory should refer to the parent theme, and get_stylesheet_directory should give you the child theme, assuming you've correctly created a child theme. I would avoid using locate_template for non-template files, it's intended for get_template_part, and a composer autoloader is not a template Commented Sep 3, 2016 at 15:03
  • Judging from your autoloader setup, you have a main composer.json and vendor folder at the top level, so an mu-plugin that loads that autoloader would be the way to do it, your theme folders shouldn't have vendor folders in that setup, unless you've been running composer install in multiple places Commented Sep 3, 2016 at 15:04
  • @TomJNowell the composer.json at root level just installs WordPress through composer, then in the parent theme there are packages like Timber / Kirki / tgmpa etc that i dont want to mess with in the child theme, they should be untouched and left in the parent theme until an update is required. What code shall i use for the mu-plugin?? also is there any way to check if the autoloader is working? Commented Sep 3, 2016 at 15:14

1 Answer 1

9

So there are one or two things to keep in mind:

  • There should only be 1 vendor folder
  • There should be a primary composer.json that's in your project root, that would pull in all the dependencies
  • You always check for and load the autoloader in the current directory, there's no guarantee it is or isn't there as you may or may not be a dependency yourself

But importantly, there's more information about WP themes you need to know that simplify your task:

Firstly, get_theme_root isn't necessary, you can use get_template_directory() instead, and it will give you the parent theme directory, so no hardcoding the parent themes name.

This should simplify your code to this:

if ( file_exists( get_template_directory() . '/vendor/autoload.php' ) ) { require get_template_directory() . '/vendor/autoload.php'; } 

But even this isn't necessary, which brings us to the second piece of information:

In WordPress, the child themes functions.php is loaded, then the parent themes functions.php immediately afterwards.

So all you need to do is make sure that the parent theme loads the autoloader, and that all your code is ran on actions as it should be ( except the autoloader itself ). This way you don't need to load the parent themes autoloader at all, it will do it itself

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.