走进wordpress 详细说说template-loader.php

简介:

再看template-laoder.php,这个文件总共只有45行。它的作用是基于访问的URL装载正确的模板.

文件第六行,也是第一条语句,如下:

if ( defined('WP_USE_THEMES') && WP_USE_THEMES )     do_action('template_redirect');

首先判断是否使用Themes,这个WP_USE_THEMES常量在index.php中第一句就被设置为true。因此条件成立,会执行do_action(‘template_redirect’)语句。

do_action('template_redirect')都做了什么?wordpress默认的有如下:

‘template_redirect’的Action(动作)在include下的文件中出现。除了这里的do_action外,还有其他三个文 件中:default-filters.php,和ms-default-filters.php中以及canonical.php中出现。不包括wp- content目录下面出现的。

canonical.php (411行):  add_action(‘template_redirect’, ‘redirect_canonical’);

default-filters.php(200行): add_action( ‘template_redirect’,   ‘wp_shortlink_header’,11, 0 );

default-filters.php(243行):  add_action( ‘template_redirect’, ‘wp_old_slug_redirect’);

ms-default-filters.php(32行):  add_action( ‘template_redirect’, ‘maybe_redirect_404′ );

ms-functions.php(1353行):  add_action( ‘template_redirect’, ‘redirect_mu_dashboard’ );

default-filters.php文件设置了wordpress大部分默认的filter和action。文件内容都是诸如下面的形式。

add_action( ‘xxx’,'xxxx’);

ms-default-filters.php是多站点时候设置的,内容类似default-filters.php。wordpress默认情况下没有开启多站点。如果需要开启,请按照wordpress的指导文件操作。

add_action( 'template_redirect',   'wp_shortlink_header',11, 0)




wp_shortlink_header位于文件link-template.php 2230行。
作用是如果当前页面定义了短连接,就在header中发送个短链接.形容:
<link rel='shortlink' href='http://localhost/?p=1234' />
这样的缩短网址。
add_action( 'template_redirect', 'wp_old_slug_redirect');

wp_old_slug_redirect() 在query.php2807行,slug是什么?slug是url的一部分。在wordpress后台的永久链接设置(/wp-admin /options-permalink.php)里,用户可以自定义链接格式。绝大多数自定义的用户喜欢在url中包含由文章标题生成的一串字符,这一串 字符就是post slug。这个函数功能是重定向old slug到正确的链接。

接下来是个判断语句;

// 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;

is_robots函数判断当前查询是否是robot。位于query.php491行。

do_robots函数位于functions.php1779行。作用:显示robot.txt的内容。

 

然后是个大的if语句:

if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :     $template = false;     if     ( is_404()   && $template = get_404_template() ) :     elseif ( is_search() && $template = get_search_template()) :     elseif ( is_tax() && $template = get_taxonomy_template()) :     elseif ( is_front_page() && $template = get_front_page_template()) :     elseif ( is_home() && $template = get_home_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;      if ( $template = apply_filters( 'template_include', $template ) )         include( $template );     return; endif;

这个大if语句中形如get_xxxx_template()的函数都定义在theme.php中。

以get_index_template为例:作用在当前或父模板下检索index模板路径,位置在theme.php725行。

function get_index_template() { return get_query_template('index'); }

在这个函数里面,就把主题的模板给装载进来了,如何操作的?马上来~~




本文转自黄聪博客园博客,原文链接:http://www.cnblogs.com/huangcong/p/4741046.html,如需转载请自行联系原作者

相关文章
|
前端开发 PHP 开发者
Wordpress主题开发之index.php
本文介绍了 WordPress 主题开发中页面结构与模板文件的使用方法。通过 header.php、sidebar.php、footer.php 和 index.php 等模板文件,实现网站模块化设计,便于统一管理和代码重用。Header 部分包含 logo、导航条等;Content 展示主体内容;Side bar 显示推荐信息或广告;Footer 则呈现版权和备案信息等内容。文章还提供了各模板文件的具体代码示例,帮助开发者快速理解和应用 WordPress 模板机制。
348 9
|
安全 网络安全 PHP
WordPress PHP Everywhere <= 2.0.3 远程代码执行(CVE-2022-24663)
WordPress PHP Everywhere <= 2.0.3 远程代码执行(CVE-2022-24663)
|
Ubuntu 关系型数据库 MySQL
ubuntu apt 安装wordpress所需所有的 一键脚本 扩展您的PHP似乎没有安装运行WordPress所必需的MySQL扩展。
ubuntu apt 安装wordpress所需所有的 一键脚本 扩展您的PHP似乎没有安装运行WordPress所必需的MySQL扩展。
337 0
ubuntu apt 安装wordpress所需所有的 一键脚本 扩展您的PHP似乎没有安装运行WordPress所必需的MySQL扩展。
|
安全 数据安全/隐私保护
屏蔽修改wp-login.php登录入口确保WordPress网站后台安全
WordPress程序默认的后台地址wp-login.php,虽然我们的密码设置比较复杂,但是如果被软件一直扫后台入口,一来影响网站的速度增加服务器的负担,二来万一被扫到密码,那就处于不安全的境地。所以,我们最好将后台地址入口隐藏屏蔽起来,我们可以通过下面的命令实现隐蔽wp-login.php入口。
657 0
|
安全 关系型数据库 MySQL
WordPress建站之wp-config-sample.php文件
WordPress建站之wp-config-sample.php文件 WordPress 包含名为wp-config-sample.php的文件,让新用户了解常规 WordPress 配置文件的外观。下面北京六翼信息技术有限公司的开发工程师带我们看一下每个标准 WordPress 安装都包含的 WordPress 配置文件示例,并讨论可能的其他选项。查看文件中的注释,了解有关每一行实际含义的更多信息joowp。com。
WordPress建站之wp-config-sample.php文件
|
关系型数据库 MySQL API
如何修复“PHP 安装缺少 WordPress 所需的 MySQL 扩展”的错误?
在WordPress建站,WordPress定制开发过程中,开发者容易遇见“您的 PHP 安装似乎缺少 WordPress 所需的 MySQL 扩展”的错误提示,如果出现这样的情况,应该怎么办?北京六翼信息有限公司的开发工程师指出,要修复错误“您的 PHP 安装似乎缺少 WordPress 所需的 MySQL 扩展”,您需要确保您的 PHP 安装已正确安装和配置 MySQL 数据库驱动程序 (mysqlnd) 和 mysqli 扩展。只有这样,您才能恢复 WordPress 和 WordPress 数据库之间的正常连接,并让您的网站重新运行。
如何修复“PHP 安装缺少 WordPress 所需的 MySQL 扩展”的错误?
|
应用服务中间件 PHP Apache
wordpress轻量应用服务器升级php版本
轻量应用服务器wordpress镜像默认使用php5.6版本,wordpress使用4.8.1。但是近期wordpress更新了版本,要求PHP版本不低于7.3,导致无法正常使用新版本wordpress,本文介绍如何手动编译更新PHP到7.3。
19632 3
|
消息中间件 监控 网络协议
SaltStack安装Apache/Mysql/PHP部署Wordpress
SaltStack是一个服务器基础架构集中化管理平台,具备配置管理、远程执行、监控等功能,基于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq、PyCrypto、Pyjinjia2、python-msgpack和PyYAML等)构建。 SaltStack 采用 C/S模式,server端就是salt的master,client端就是minion,minion与master之间通过ZeroMQ消息队列通信。 master监听4505和4506端口,4505对应的是ZMQ的PUB system,用来发送消息,4506对应的是REP system是来接受
319 0
|
安全 PHP
PHP Everywhere 三个 RCE 漏洞威胁大量 WordPress 网站
PHP Everywhere 三个 RCE 漏洞威胁大量 WordPress 网站
418 0

相关实验场景

更多