Lumberjack doesn't see archive-{post_type}.php with proper controller class
What are the steps to reproduce this issue?
- Add WooCommerce
- Create archive-product.php file
- Create ArchiveProductController class in this file
- Go to /product-category/some-category/
What happens? Lumberjack doesn't see archive-product.php, it is looking for archive.php or index.php. It's not in line with the templatee hierarchy (please look at https://developer.wordpress.org/themes/basics/template-hierarchy/#custom-post-types)
What were you expecting to happen? Lumberjack should looking for archive-product.php and ArchiveProductController class.
What versions of software are you using?
Operating System: Win10, Apache (XAMMP)
PHP Version: 7.2
Lumberjack Version: latest
Hey! We've had somebody else run into this issue once before. It's caused by some inflexibility in the WP core itself, which meant we had to implement the Lumberjack Controller stuff in a particular way. This method relies on the "template_include" filter, which both Lumberjack and WooCommerce both use.
In short, due to some of the complexities in Lumberjack, the WooCommerce's "template_include" filter doesn't run.
We've put in a ticket into the WP core so in the future we can use something other than "template_include": https://core.trac.wordpress.org/ticket/46652
We would like to work out a patch for this, however we have not done anything with WooCommerce ourselves so it would be great to work with someone to get this tested and resolved.
Would you be able to try adding in return $template at the end of the handleTemplateInclude() method in Lumberjack's WordPressControllersServiceProvider class please, and let us know if that resolves the issue?
Here's a link to that method for reference: https://github.com/Rareloop/lumberjack-core/blob/v4.2.0/src/Providers/WordPressControllersServiceProvider.php#L21
Hey! Thanks for your quick response. This is my first contact with Lumberjack, I decided to use it when my patience for the spaghetti code in WP was over. I just have to do a simple shop based on Woo, so I'm hoping for Lumberjack.
Would you be able to try adding in
return $templateat the end of thehandleTemplateInclude()method in Lumberjack'sWordPressControllersServiceProviderclass please, and let us know if that resolves the issue?
Unfortunately, nothing happened, Lumberjack still reads from archive.php or index.php. But I found a temporary solution that works - I created a taxonomy-product_cat.php file with a TaxonomyProductCatController class controller and now reads the data correctly.
We would like to work out a patch for this, however we have not done anything with WooCommerce ourselves so it would be great to work with someone to get this tested and resolved.
I am pleased to be able to contribute at least a little to the development of this project by testing it. I hope I won't encounter any more problems and the WP team will fix soon this inflexibility in the WP Core. I can share my impressions when I finish my shop.
@MrkKr Glad you found a work around. Thank you for giving that a test.
We are currently exploring an alternative approach to the way we use template_include which should resolve this issue.
Hi, I think I ran in the same problem with another plugin. The plugin has its own subfolder in the theme with an archive and single template. This caused a problem with converting the template to a controller. I wanted a way to disabled templates to be converted to controllers so that we can use old school Timber::render or even older the_post.
First I've created my own WordPressControllersServiceProvider and replaced the Lumberjack WordPressControllersServiceProvider in de app.php:
'providers' => [
Rareloop\Lumberjack\Providers\RouterServiceProvider::class,
// Rareloop\Lumberjack\Providers\WordPressControllersServiceProvider::class,
App\Providers\WordPressControllersServiceProvider::class,
Rareloop\Lumberjack\Providers\TimberServiceProvider::class,
Rareloop\Lumberjack\Providers\ImageSizesServiceProvider::class,
Rareloop\Lumberjack\Providers\CustomPostTypesServiceProvider::class,
Rareloop\Lumberjack\Providers\MenusServiceProvider::class,
Rareloop\Lumberjack\Providers\LogServiceProvider::class,
Rareloop\Lumberjack\Providers\QueryBuilderServiceProvider::class,
Rareloop\Lumberjack\Providers\SessionServiceProvider::class,
Rareloop\Lumberjack\Providers\EncryptionServiceProvider::class,
// Application Providers
App\Providers\AppServiceProvider::class,
],
Then I've create a new config file named controllers.php with an config array with a key exclude_templates:
<?php
return [
'exclude_templates' => [
'/subfolder/archive.php',
'/subfolder/single.php',
],
];
This is what's in my own WordPressControllersServiceProvider:
<?php
namespace App\Providers;
use Rareloop\Lumberjack\Providers\WordPressControllersServiceProvider as LumberjackWordPressControllersServiceProvider;
use Rareloop\Lumberjack\Facades\Config;
use Stringy\Stringy;
class WordPressControllersServiceProvider extends LumberjackWordPressControllersServiceProvider
{
public function handleTemplateInclude($template)
{
if (in_array(Stringy::create($template)->removeLeft(get_template_directory()), Config::get('controllers.exclude_templates'))) {
return $template;
}
parent::handleTemplateInclude($template);
}
}
I think this is an easy way to disabled some templates to be converted to controllers. Hope this helps.