[wp-trac] [WordPress Trac] #43597: Modify template-loader engine
WordPress Trac
noreply at wordpress.org
Mon Mar 26 12:55:04 UTC 2018
#43597: Modify template-loader engine
-------------------------+------------------------------
Reporter: chespir | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Themes | Version:
Severity: normal | Resolution:
Keywords: | Focuses: template
-------------------------+------------------------------
Changes (by chespir):
* keywords: reporter-feedback =>
Comment:
Hi @soulseekah , thanks for your answer and do not worry about the delay.
I understand what you are saying about using WP_USE_THEMES to not get an
answer as it is obtained in a frontend request.
In my case, I'm using a script (a .php file) that gets some parameters
through an AJAX call. I use these parameters to set the global wp_query so
that I can tell Wordpress that I am, for example, in a single or archive.
If I don't set WP_USE_THEMES, Wordpress will not load my single.php, but
if I set WP_USE_THEMES it will load single.php.
Now, what I need in my script is to generate some variables that I want to
get to the template that Wordpress is going to load, which is possible if
I could get the template that Wordpress is going to load and I could load
it myself with an include, just like the Wordpress core does right now. It
should be noted that I try to avoid the use of global.
I think this could be fixed if I could access the Wordpress template
decision, which is made before the template_include filter through a
function. I also think this option would add versatility to the loading of
the Wordpress core.
My proposed solution would be something like following lines.
{{{#!php
<?php
//Function to decide which template will Wordpress load
function wordpress_decission_template(){
$template = false;
if ( is_embed() && $template = get_embed_template()
) :
elseif ( is_404() && $template = get_404_template()
) :
elseif ( is_search() && $template = get_search_template()
) :
elseif ( is_front_page() && $template =
get_front_page_template() ) :
elseif ( is_home() && $template = get_home_template()
) :
elseif ( is_post_type_archive() && $template =
get_post_type_archive_template() ) :
elseif ( is_tax() && $template =
get_taxonomy_template() ) :
elseif ( is_attachment() && $template =
get_attachment_template() ) :
remove_filter('the_content', 'prepend_attachment');
elseif ( is_single() && $template = get_single_template()
) :
elseif ( is_page() && $template = get_page_template()
) :
elseif ( is_singular() && $template =
get_singular_template() ) :
elseif ( is_category() && $template =
get_category_template() ) :
elseif ( is_tag() && $template = get_tag_template()
) :
elseif ( is_author() && $template = get_author_template()
) :
elseif ( is_date() && $template = get_date_template()
) :
elseif ( is_archive() && $template = get_archive_template()
) :
else :
$template = get_index_template();
endif;
return $template;
}
//Load the template
$template = wordpress_decission_template();
if ( $template = apply_filters( 'template_include', $template ) ) {
include( $template );
} elseif ( current_user_can( 'switch_themes' ) ) {
$theme = wp_get_theme();
if ( $theme->errors() ) {
wp_die( $theme->errors() );
}
}
?>
}}}
Then in my script I would call the wordpress_decission_template function,
then I would set my desired variables, and I would include the template.
Thank you very much.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/43597#comment:3>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list