Last active
December 8, 2018 18:58
-
-
Save webmandesign/29c25b036179816a25b26a6fba418ee2 to your computer and use it in GitHub Desktop.
Can we use `locate_template()` for other than "Template files"?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* We need to load this file only if it exists: | |
* - first checking in child theme, | |
* - then trying parent theme. | |
*/ | |
$file_path_relative = 'assets/css/for-processing.css'; | |
/** | |
* If we could use `locate_template()` function for any file | |
* (and not just for "Template files") in WordPress themes, | |
* our job is much easier: a single line of code. | |
* | |
* @link https://developer.wordpress.org/reference/functions/locate_template/ | |
* @link https://developer.wordpress.org/themes/basics/template-files/ | |
*/ | |
locate_template( $file_path_relative, true, false ); | |
// ...OR... | |
/** | |
* On the other hand, if `locate_template()` file should be used | |
* for "Template files" only, we are stuck with this code instead: | |
*/ | |
$file_path_absolute = array( | |
'in_child_theme' => trailingslashit( get_stylesheet_directory() ) . $file_path_relative, | |
'parent_theme_fallback' => trailingslashit( get_template_directory() ) . $file_path_relative, | |
); | |
if ( file_exists( $file_path_absolute['in_child_theme'] ) ) { | |
require $file_path_absolute['in_child_theme']; | |
} elseif ( file_path_absolute_exists( $file_path_absolute['parent_theme_fallback'] ) ) { | |
require $file_path_absolute['parent_theme_fallback']; | |
} | |
// ...OR... | |
/** | |
* We could actually optimize more: | |
*/ | |
$file_path_absolute = get_theme_file_path( $file_path_relative ); | |
if ( file_exists( $file_path_absolute ) ) { | |
require $file_path_absolute; | |
} |
For reference: Asked for opinion in #themereview WP Slack.
Alternative solution is using a dedicated file loading/locating PHP class in your WordPress theme.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is
locate_template()
meant for "Template files" only?I use my custom WordPress theme framework (and starter theme) in all of my themes. Recently I've run into an issue with ThemeForest WordPress guidelines/review.
As the same issue is affecting my theme in WPORG repo, I'm wondering what's the situation there:
WordPress.org themes
In my Reykjavik theme I'm using
locate_template()
to load CSS file content (to output buffer) for processing with PHP and subsequently passing the processed CSS intowp_add_inline_style()
(see the GIST code above for simplification of the actual process related tolocate_template()
and possible solutions when not using the function). The theme passed the review process without any issue regarding usinglocate_template()
.But is it possible that this usecase is not allowed in WPORG either and the review team just has't spotted the code during the review process?
ThemeForest themes
Due to word "template" in
locate_template()
function name, ThemeForest WordPress guidelines does not allow using the function to load any other than WordPress theme "Template file".So, which is correct?
Should I use it for "Template files" only?
Is it even possible to use the function to check for (any, none template) file existence in child/parent theme?
Or should I not use
locate_template()
function at all in my themes? (I don't think this is the case.)Additional info
There is no other function in WP that reproduces functionality of
locate_template()
that I can use to load any file from the theme: