Make WordPress Core

Opened 10 years ago

Closed 10 years ago

#30288 closed feature request (duplicate)

set import directory for get_template_part()

Reported by: billy-mathews's profile Billy Mathews Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1
Component: Themes Keywords: has-patch 2nd-opinion dev-feedback
Focuses: template Cc:

Description

Either a function or an action hook in functions.php that allows you to set an import directory for get_template_part()

This would either simply prepend the import directory to the file name passed to get_template_part(), or add additional paths to the array of paths where the function looks for the file. Probably the latter?

Attachments (1)

30288.patch (1.3 KB) - added by Billy Mathews 10 years ago.

Download all attachments as: .zip

Change History (10)

#1 @valendesigns
10 years ago

You want to be able to alter the path so it looks for files outside of the themes root directory?

The issue wouldn't be solely with get_template_part() then, it would be with locate_template() too, as that is the function that does the file locating using either STYLESHEETPATH or TEMPLATEPATH.

An option to fix this issue would be to add a boolean$force_path variable to both function that allows for alternative paths outside of the root of a theme when set to true. Basically, it uses the explicit path supplied from the get_template_part() to find the file and bypass the global theme paths.

Why do you need to get files outside of the theme? This sounds like plugin territory to me.

#2 @johnbillion
10 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Thanks for the report. The aim of this is suitably similar enough to #22355 that we can consider it a duplicate.

#3 @Billy Mathews
10 years ago

  • Resolution duplicate deleted
  • Status changed from closed to reopened

To clarify, not necessarily outside the themes directory. I'd like to be able to separate my page templates from my page partials (such as the search form, sidebar, or parts within the sidebar such as social media icons etc.) within a theme. I would put these files in a "partials" folder inside my theme directory. Using add_import_directory("partials") in functions.php would allow me to use get_template_part('searchform') to use files in the partials directory.

This is different to the other ticket which is to do with child theme file inheritance.

#4 follow-up: @valendesigns
10 years ago

That can be accomplished simply by doing get_template_part('partials/searchform').

#5 in reply to: ↑ 4 @Billy Mathews
10 years ago

Yes, I know. But if all of my partials are in the same folder I think it makes sense to be able to tell wp to look there whenever you ask for a template part.

#6 @Billy Mathews
10 years ago

  • Keywords has-patch added

Here is an initial patch to show the functionality I am talking about.

I have stepped through and tested this code and it works as expected. Using the following code in my functions.php I am able to use template parts in a "partials" folder inside my theme directory (as well as in the theme's directory itself).

function add_import_dirs($dirs){
    $dirs[] = 'partials';
    return $dirs;
}
add_filter('template_part_import_directories', 'add_import_dirs', 10, 1);

Not sure if it may be worth making this a wrapper function?

#7 @Billy Mathews
10 years ago

Also I suppose some other functions may have to inherit this functionality - such as get_search_form() and even get_header() and get_footer()

I guess this is a different thing but shouldn't get_search_form() allow you to pass a $name as you can with get_header()? And shouldn't they both (and any others) allow you to pass 'folder/filename' as you can with get_template_part()?

#8 @Billy Mathews
10 years ago

  • Keywords 2nd-opinion dev-feedback added

#9 @SergeyBiryukov
10 years ago

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

Duplicate of #15086.

Note: See TracTickets for help on using tickets.