Bill: Go into WordPress detailed talk about template-loader.php

Source: Internet
Author: User

Looking at template-laoder.php, this file is only 45 lines in total. Its role is to load the correct template based on the URL of the access.

The sixth line of the file is also the first statement, as follows:

if defined (' Wp_use_themes ') && wp_use_themes)     do_action (' Template_redirect ');

First judge whether to use themes, the Wp_use_themes constant is set to true in the first sentence of index.php. As a result, the do_action (' template_redirect ') statement is executed.

What did Do_action (' Template_redirect ') do? WordPress Default has the following:

The Action (action) of ' Template_redirect ' appears in the file under include. In addition to the do_action here, there are three other files: default-filters.php, and ms-default-filters.php in and canonical.php. Does not include the Wp-content directory that appears below.

canonical.php (411 lines): Add_action (' template_redirect ', ' redirect_canonical ');

default-filters.php (200 lines): Add_action (' template_redirect ', ' Wp_shortlink_header ', 11, 0);

default-filters.php (243 lines): Add_action (' template_redirect ', ' wp_old_slug_redirect ');

Ms-default-filters.php (32 lines): Add_action (' template_redirect ', ' maybe_redirect_404′ ');

ms-functions.php (1353 lines): Add_action (' template_redirect ', ' Redirect_mu_dashboard ');

The default-filters.php file sets most of the default filter and action for WordPress. The contents of the file are in the form of the following.

Add_action (' xxx ', ' xxxx ');

Ms-default-filters.php is set at multiple sites, and the content is similar to default-filters.php. WordPress does not turn on multiple sites by default. If you need to turn it on, follow the instructions in WordPress.

Add_action (' Template_redirect ',   ' Wp_shortlink_header ', 11, 0)




Wp_shortlink_header is located in the file link-template.php 2230 line.
The function is to send a short link in the header if the current page defines a short connection. Description:
<link rel= ' shortlink ' href= ' http://localhost/?p=1234 '/>
Such a shortened URL.
Add_action (' Template_redirect ', ' wp_old_slug_redirect ');

Wp_old_slug_redirect () on query.php2807 line, what is slug? Slug is part of the URL. In the WordPress background of the permanent link settings (/wp-admin/options-permalink.php), users can customize the link format. Most custom users like to include a string of characters generated by the title of the article in the URL, which is the post slug. This function is to redirect old slug to the correct link.

Next is a judgment statement;

Process feeds and trackbacks even if not using themes. if (Is_robots ()):     do_action (' do_robots ');     Return ElseIf (Is_feed ()):     do_feed ();     Return ElseIf (Is_trackback ()):     include (Abspath. ' wp-trackback.php ');     Return endif

The Is_robots function determines whether the current query is robot. Located on the query.php491 line.

The Do_robots function is located on the functions.php1779 line. Function: Displays the contents of the Robot.txt.

Then there is a large if statement:

if (defined (' Wp_use_themes ') && wp_use_themes): $template = false; if (is_404 () && $template = Get_404_template ()): ElseIf (Is_search () && $template = Get_sear Ch_template ()): ElseIf (Is_tax () && $template = Get_taxonomy_template ()): ElseIf (Is_front_page () &amp ;& $template = Get_front_page_template ()): ElseIf (Is_home () && $template = Get_home_template ()): E Lseif (Is_attachment () && $template = Get_attachment_template ()): Remove_filter (' the_content ', ' Prepend_     Attachment '); ElseIf (Is_single () && $template = Get_single_template ()): ElseIf (Is_page () && $template = Get_pag E_template ()): ElseIf (Is_category () && $template = Get_category_template ()): ElseIf (Is_tag () &&am P $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 () &A          mp;& $template = Get_comments_popup_template ()): ElseIf (is_paged () && $template = Get_paged_template ()     ): Else: $template = Get_index_template ();      endif     if ($template = apply_filters (' template_include ', $template)) include ($template); Return endif

The functions of this large if statement in the form of get_xxxx_template () are defined in theme.php.

Take Get_index_template as an example: The role retrieves the index template path under the current or parent template, located in the theme.php725 row.

function Get_index_template () {return get_query_template (' Index ');}

In this function, the template of the theme is loaded in, how to operate it?

Bill: Go into WordPress detailed talk about template-loader.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.