Support

Home Forums Event Espresso Premium BUG REPORT: EEH_Template::locate_template doesn't locate templates correctly

BUG REPORT: EEH_Template::locate_template doesn't locate templates correctly

Posted: December 14, 2015 at 7:26 am

Viewing 16 reply threads


internetmotors

December 14, 2015 at 7:26 am

Hi,
inspecting your code I found that the method EEH_Template::locate_template() doesn’t locate templates correctly. If I put some template file in
path/wp-content/uploads/espresso/ folder
originally located in
path/wp-content/plugins/event-espresso-core-reg/somepath/afile.template.php
it search it in
path/wp-content/uploads/espresso//path/wp-content/plugins/event-espresso-core-reg/somepath/afile.template.php
instead of
path/wp-content/uploads/espresso/afile.template.php
or
path/wp-content/uploads/espresso/somepath/afile.template.php.
I’ve verified this with the
/wp-content/plugins/event-espresso-core-reg/shortcodes/espresso_thank_you/templates/thank-you-page-registration-details.template.php
Can you send me a patch for this please?


Tony

  • Support Staff

December 14, 2015 at 10:44 am

Hi there,

It depends on how the call to locate_template() is made, in the example given thank-you-page-registration-details.template.php is called using the templates absolute path, like this:

EEH_Template::locate_template( THANK_YOU_TEMPLATES_PATH . 'thank-you-page-registration-details.template.php', $template_args, TRUE, TRUE );

What will happen is locate_template() will add that absolute path to the first element of the array that EE checks for custom files, once the file has been found it skips all of the other checks so in this example thank-you-page-registration-details.template.php will always load from the original location.

However, we have altered the way in which locate_template() works to allow for custom templates to load for all calls, we are currently testing those changes to confirm no new bugs have been introduced and will then include those changes within an update.


internetmotors

December 14, 2015 at 11:21 am

Hi Tony,
thanks for your fast reply. The example I wrote is the line 544 of EES_Espresso_Thank_You.shortcode.php file. I thought that one aim of the locate_template() method is to override templates, but in this case isn’t working.
Do you know how long (more or less) it will take to release this update?
Thanks
Eduardo


Josh

  • Support Staff

December 14, 2015 at 3:55 pm

Hi Eduardo,

It shouldn’t be long as we’re trying to finish up on testing. If you’d like, you can check out the same branch we are testing here:

https://github.com/eventespresso/event-espresso-core/tree/BUG-8013-locate-template


Josh

  • Support Staff

December 15, 2015 at 2:05 pm

The fix for EEH_Template::locate_template was included in the update that was released today.


internetmotors

December 16, 2015 at 1:38 am

Perfect, thank you Josh!

Eduardo


Tony

  • Support Staff

December 16, 2015 at 5:17 am

Please let us know if you have any problems 🙂

The changes alter the behavior for templates that could not be overridden previously, they will check the sites current theme folder first so can be overridden using that location.


internetmotors

December 17, 2015 at 3:01 am

Thank you Tony,
I have another question. Every file named *.template.php can be overridden? And the EE_Template_Layout instances?
Thank you

Eduardo


Tony

  • Support Staff

December 17, 2015 at 3:06 am

I believe so yes.

EE_Template_Layout should all run the template through locate_template(), they will all be using absolute paths but with the new changes you should be able to override them.


internetmotors

December 17, 2015 at 3:53 am

Hmm so I think there’s an error with the file registration_page_wrapper.template.php .

Debugging a bit I saw that you’re using get_template_directory() (line 266), but this function retrieve the parent theme directory; if a user wants to override a file including it in a child theme, he can’t. I think that it’s better to search in both places. (you can use the get_sylesheet_directory() function for child theme). Another thing I see is that there’s a filter (FHEE__EEH_Template__locate_template__template_folder_paths) to change the path list, but the path list is modified (theme path and original filepath added) after the filter, so the user doesn’t really have the ability to change the path list.

Eduardo


Tony

  • Support Staff

December 17, 2015 at 4:32 am

FHEE__EEH_Template__locate_template__template_folder_paths can be used to add additional paths to search but again it depends on which template file.

but the path list is modified (theme path and original filepath added) after the filter, so the user doesn’t really have the ability to change the path list.

That only happens with templates using an absolute path, the theme is checked for the template file, then the absolute path that was passed to the locate_template() call is used.

You can still load custom versions of the file from within your theme (although I do agree with your point about using get_stylesheet_directory() for child themes)

You can also use ‘FHEE__EEH_Template__locate_template__full_template_paths’ to alter the template paths after all changes have been made. There’s a little extra work involved as you need to use basename() to grab the template filename from one of the paths, then add another full custom path to the array in whichever order you prefer.


internetmotors

December 17, 2015 at 4:39 am

Thanks Tony for your fast reply.
Do you think those behaviors will change in near future? I mean, do you think it’s better for me wait a couple of week, or create my custom path with ‘FHEE__EEH_Template__locate_template__full_template_paths’ filter, assuming that will be the only way i will create my custom path in future?

Eduardo


Tony

  • Support Staff

December 17, 2015 at 6:47 am

We won’t be adding a different way to include custom template paths in the near future locate_template allows for custom versions to load, however I have created a ticket to use get_stylesheet_directory() so that templates locate checks the child theme.

I’ll check in with developers to confirm this and update this thread shortly.


internetmotors

December 17, 2015 at 8:35 am

So, in future too, for files passed with full filepath will be impossible to add custom path with FHEE__EEH_Template__locate_template__template_folder_paths, right?


Tony

  • Support Staff

December 17, 2015 at 9:12 am

I can’t say 100% that we will not modify that function, but currently we have no plans to alter how templates using absolute paths are overridden.

You can load custom templates by placing the custom template (with the same name) within your theme root directory.

So currently if you want to load custom templates within a different directory than root you can use a function like this:

function theme_directory_espresso_locate_template( $full_template_paths ) {

	//Pull a single template path from the array and use that to find the filename currently being searched for.
	$single_template_full_path = reset( $full_template_paths );
	$template_filename = basename( $single_template_full_path);

	//Build the new full template path to check within iced-mocha
	$theme_espresso_template_dir = get_stylesheet_directory() . '/content/espresso/' . $template_filename;

	//Add the new template path to the paths check by locate_template()
	array_unshift( $full_template_paths, $theme_espresso_template_dir );
	
	return $full_template_paths;

}
add_filter( 'FHEE__EEH_Template__locate_template__full_template_paths', 'theme_directory_espresso_locate_template' );

That will load from your child theme within the /content/espresso/ directory.


internetmotors

December 17, 2015 at 9:18 am

ok, thank you Tony!

Eduardo


Tony

  • Support Staff

December 17, 2015 at 9:32 am

You’re most welcome 🙂

Please let us know if you have any further questions.

Viewing 16 reply threads

The support post ‘BUG REPORT: EEH_Template::locate_template doesn't locate templates correctly’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso