WordPress: where are template pages called?

I am trying to integrate wordpress with my previously installed platform.

I was able to edit most of the system as I wish, but I can´t figure out how to intervene in what wordpress does before loading the template file.

Read More

What I want to add is a simple conditional statement that either load a page trough my previous cms, or loads normally wordpress.

However, I was unable to understand which function or file loads the components of the theme.

I understand wp uses template hierarchy, loading different files for different types of pages (posts, category page, and so on).

But what function is making this choices and actually loading the files?

Related posts

Leave a Reply

1 comment

  1. Your question isn’t clear enough in what you’re actually trying to do but /wp-includes/template-loader.php is going to be of particular interest to you.

    This is the file that loads the templates. Just to make it clear I’m not suggesting you modify this file I’m merely pointing out where the magic happens.

    As soon as it’s loaded it runs the action template_redirect. If you wanted to perform some logic then redirect here’s where you’d do it. Here’s an example that would go in your theme’s functions.php:

    function wpse_template_redirect() {
        if ( condition_a() == condition_b() ) {
            wp_redirect( 'url-to-redirect-to' );
            exit;
        }
    }
    add_action( 'template_redirect', 'wpse_template_redirect' );
    

    At the bottom of this file there are conditionals which determine which template file is:

    $template = false;
    if     ( 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_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()        ) :
    elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
    elseif ( is_paged()          && $template = get_paged_template()          ) :
    else :
        $template = get_index_template();
    endif;
    /**
     * Filter the path of the current template before including it.
     *
     * @since 3.0.0
     *
     * @param string $template The path of the template to include.
     */
    if ( $template = apply_filters( 'template_include', $template ) )
        include( $template );
    return;