黄聪:走进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'); }
在这个函数里面,就把主题的模板给装载进来了,如何操作的?马上来~~
黄聪:走进wordpress 详细说说template-loader.php的更多相关文章
- 黄聪:《跟黄聪学WordPress插件开发》
续<跟黄聪学WordPress主题开发>之后,又一个作品完成!<跟黄聪学Wordpress插件开发>,国内最好的Wordpress插件开发视频教程!! 目录预览: WordPr ...
- 黄聪:《跟黄聪学WordPress主题开发》
又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...
- 黄聪:wordpress教程
又一个作品完成!<跟黄聪学Wordpress主题开发>,国内最好的Wordpress主题模版开发视频教程!! 目录预览: WordPress官方源文件层式结构讲解 WordPress数据库 ...
- 黄聪:wordpress工作原理
WP初始化的过程:当你输入<yourlink>/wordpress对wordpress进行初始化时,wordpress默认会找根目录下的index.php页面,看一下index.php页面 ...
- 黄聪:Wordpress 模版技术手册 - WordPress Theme Technical manuals
WordPress基本模板文件 一套完整的WordPress模板应至少具有如下文件: style.css : CSS(样式表)文件 index.php : 主页模板 archive.php : Arc ...
- 黄聪:wordpress源码解析-数据库表结构(转)
如果是一个普通的用户,不需要了解wordpress数据库的结构.但是,如果你正在写一个插件,你应该会对wordpress如何处理它的数据和关系感兴趣.如果你已经尝试使用已经存在的wordpress a ...
- 黄聪:wordpress中remove_action、add_action、 do_action()的hook钩子都有哪些
原文地址:http://codex.wordpress.org/Plugin_API/Action_Reference muplugins_loaded After must-use plugins ...
- 黄聪:WordPress 多站点建站教程(四):获取子站点相关信息(站点的注册时间,修改时间,总文章数,URL等)
1.获取子站点blogs表里面的内容信息 $blog_details = get_blog_details(1); echo 'Blog '.$blog_details->blog_id.' i ...
- 黄聪:Wordpress二级域名共享用户cookie出现错误解决方案及WP的Cookie机制
在若干年以前,我刚开始折腾Wordpress没多久的时候,就自己摸索过 多个Wordpress网站共享一份数据表的实现方法 .这种看起来好像很高大上的类SSO功能,能够给用户在多个网站之间提供快速.无 ...
随机推荐
- MACOS,LINUX,IOS上可用的毫秒级精度时间获取
二话不说,先上代码 void outputCurrentTime(uint32_t seq,const char* type){ struct timeval t_curr; gettimeofday ...
- RaidoGroup+RadioButton模拟android下拉框弹出List
引用 <上面的Hello world!是居左的,但是下面的文字却怎么都不能靠边.试了各种方法都不行.最后,无意中给RadioButton添加一个backgroud属性即可:<RadioBu ...
- php parse_url 函数教程
[导读] php parse_url 函数教程parse_url ( PHP 4中, PHP 5中) parse_url -解析URL并返回其组成部分 描述 混合parse_url (字符串$网址[摘 ...
- PHP避免刷新页面重复提交
PHP避免刷新页面重复提交 2013-07-09 15:27 匿名 | 浏览 3567 次 编程语言 情景:从html提交数据到x.php 在x.php中$_POST数据写库并且显示,当x.php刷新 ...
- UVA10305 拓扑序
题意:给出多个任务,以及一系列任务的关系表示某个任务必须在某个任务前完成,问一个合理的任务完成顺序 拓扑序的裸题,一开始用大白书的写法,后来发现并不好用,就换了BFS又A了一遍. 原: #includ ...
- Java设计模式之简单工厂设计模式
简单工厂将业务逻辑部分和界面逻辑部分分离开来,降低了界面逻辑和业务逻辑的耦合度,符合面向对象迪米特法则.下面以一个加法减法运算器为例,各位读者可以自行按照这种设计方式设计出一个小小的运算器. 1.业务 ...
- ADC 分辨率和精度的区别
分辨率和精度这两个,经常拿在一起说,才接触的时候经常混为一谈.对于ADC来说,这两样也是非常重要的参数,往往也决定了芯片价格,显然,我们都清楚同一个系列,16位AD一般比12位AD价格贵,但是同样是1 ...
- Java的位运算 待整理
位运算:二进制运算 Java的异或运算^ 真^假=真 假^真=真 假^假=假 真^真= 假,这四个是在网上copy的例子,真是1,假是0 但它却是说明了Java异或运算的基本法则,那就是:只要两个条件 ...
- android 二维码扫描
了解二维码这个东西还是从微信 中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,不可思议啊,那时候还不了解二维码,呵呵,然后做项目的时候, 老板说要加上二维码扫描功 ...
- event.srcElement兼容处理
在IE下,event对象有srcElement属性,但是没有target属性:Firefox下,even对象有target属性,但是没有srcElement属性.. 解决方法:使用obj(obj = ...