Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#18151 closed enhancement (invalid)

Option to return string false for locate_template()

Reported by: griffinjt's profile griffinjt Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.2.1
Component: Template Keywords: needs-patch
Focuses: Cc:

Description

If this is something that can be fixed without editing the core, then I apologize. :) But I've been hunting through and don't think there is a way around this.

locate_template() returns the string of the template filename if successful and nothing if empty. This is nice, but annoying if you do not want to have the filename returned on success. If you want to load the template as part of a template_redirect or the like, then you are stuck with returning the filename in a string as well, which finds itself outputted on the page where the template is called.

load_template() gets around this, but it does not provide the functionality for a child theme because it looks for the file within the parent theme. Only locate_template() (as far as I know) will search the child theme first and then parent theme. This is especially useful for those that want to override a "core" parent template without actually making a core edit.

It would be nice just to add a parameter or something of that nature that will allow you to choose if you want to have the filename returned on success or not when using locate_template(). I hope I am making sense, and if I'm not, tell me and I will try to explain better and show an example.

Change History (9)

#1 @coffee2code
13 years ago

  • Resolution set to duplicate
  • Status changed from new to closed

Check out my solution proposed in #13239. I'm closing this as a duplicate of that ticket since what you are requesting would be possible with my patch. Feel free to reopen (with more details and/or an example of what you're trying to achieve) if you don't feel that ticket is related.

#2 @griffinjt
13 years ago

I believe that ticket is related but in a different manner. All I would propose is adding another parameter such as $return and have by default $return = true, like below:

function locate_template($template_names, $load = false, $require_once = true, $return = true ) {

If it is set to false, simply return nothing. That's what I am getting at. :)

#3 @griffinjt
13 years ago

  • Resolution duplicate deleted
  • Status changed from closed to reopened

I'm going to re-open for the fact that while your solution would work, it would require more code than should be necessary to achieve a seemingly simple solution. Maybe you could add this parameter into your solution? I just hate the thought of having to add in all that code to edit the $load return when you could just add "false" to the accepted parameters of the function.

#4 @kawauso
13 years ago

I don't understand why you're stuck with outputting the template filename if you use locate_template() in the 'template_redirect' hook. Can you provide the use case code for that?

#5 @griffinjt
13 years ago

Here is sample code that I am using:

add_action( 'template_redirect', 'custom_page_template_setup', 9 );
function custom_page_template_setup() {

	global $options, $post, $classes;
	
	if ( $options['default_page_layout'] == 'Full Width' && is_page() ) {
		if ( get_post_meta( $post->ID, '_wp_page_template', true ) == 'default' ) {
			$default_template = add_filter( 'body_class', 'custom_page_body_class' );
			$default_template = locate_template( array( 'full-width-template.php' ), true );
			exit( $default_template );
		}
	}

A user can choose to have a default page template be set for any page that is created. This function takes that into account, checks that no page template has been set for any individual page, and if not, sets the template for the one they specified in the options area. Because locate_template() returns the filename string, it gets outputted at the very end of the page, right before the closing </body> tag. Now load_template() can get around this, but then limits the end user to whatever templatepath I specify. locate_template() would be preferred because it is more forward and would allow for the end user to create their own version of the template and use it throughout their site.

I hope this explains what I am talking about. If there is another way besides template_redirect, then by all means let me know...but the way I see it, this is a pretty good way of doing it.

Last edited 13 years ago by griffinjt (previous) (diff)

#6 @kawauso
13 years ago

Because locate_template() returns the filename string, it gets outputted at the very end of the page, right before the closing </body> tag.

More than likely this is because you're including the $default_template value in your exit(). I can't find any output of the filename in either load_template() or locate_template().

#7 @coffee2code
13 years ago

I agree with @kawauso. You're telling exit() to output the template name by having it as an argument. Just use exit(); without arguments.

#8 @griffinjt
13 years ago

  • Resolution set to invalid
  • Status changed from reopened to closed

Wow, I can't believe I didn't notice that! Epic fail on my end.. I need to take a break from coding :-P

Thanks guys for pointing this out - I'm closing it on account of my ignorance :)

#9 @dd32
13 years ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.