wordpress学习二:源码目录结构和启动流程

摘要:
Wordpress安装后的文件目录如下:主目录和文件用法如下:wp-admin:用于博客后台设置的功能目录wp content:Wordpress的主题、插件和本地化存储目录wp包括:存储Wordpress某些类和公共功能文件的目录。根目录中的index.php是大多数wordpress函数的入口模块。Wp-load.php进行一系列函数调用,设置全局变量,并引用必要的php文件。template-loader.php实现根据不同的url参数加载不同的模板。

wordpress安装后的文件目录如下:

wordpress学习二:源码目录结构和启动流程第1张

其中的主要目录和文件用途介绍如下:

wp-admin:用于进行博客后台设置的功能目录

wp-content: wordpress的 主题,插件和本地化的存储目录

wp-include: wordpress一些类和公共函数文件的存放目录。

根目录下的index.php是大部分wordpress功能的入口模块。

大概看了一下入口代码的调用关系,画图如下:

wordpress学习二:源码目录结构和启动流程第2张

上图中只绘制了主要的流程。流程主要分为三个部分,分别是全局的初始化,数据读取和模板渲染。 

wp-load.php中进行了一串的函数调用,定了了全局变量,引用必要的php文件。

wp()函数主要调用wp-include/class-wp.php中的类方法,实现不同请求的预处理,加载对应的数据到全局对象中,以后后续的模板页面来展示。

template-loader.php中实现按照不同的url参数,来加载不同的模板。

 先熟悉下模板的加载流程:

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

    if ( $template = apply_filters( 'template_include', $template ) )
        include( $template );
    return;

以上代码很清楚能看出根据不同的页面类型,加载不同的模板php文件。上面代码中的get_home_template函数定义如下: 

function get_home_template() {
	$templates = array( 'home.php', 'index.php' );
	return get_query_template( 'home', $templates );
}

  上面代码可以看出默认的home页面会加载的页面是home.php或者index.php文件.

get_query_template能够获取到指定类型的模板文件路径。

function get_query_template( $type, $templates = array() ) {
	$type = preg_replace( '|[^a-z0-9-]+|', '', $type );

	if ( empty( $templates ) )
		$templates = array("{$type}.php");

	$template = locate_template( $templates );
	/**
	 * Filter the path of the queried template by type.
	 *
	 * The dynamic portion of the hook name, `$type`, refers to the filename
	 * -- minus the extension -- of the file to load. This hook also applies
	 * to various types of files loaded as part of the Template Hierarchy.
	 *
	 * @since 1.5.0
	 *
	 * @param string $template Path to the template. See {@see locate_template()}.
	 */
	return apply_filters( "{$type}_template", $template );
}

  locate_template函数判断出对应的模板文件是否存在,如果存在,$load为真时加载对应的模板文件,否则仅仅是返回模板文件的路径。

function locate_template($template_names, $load = false, $require_once = true ) {
	$located = '';
	foreach ( (array) $template_names as $template_name ) {
		if ( !$template_name )
			continue;
		if ( file_exists(STYLESHEETPATH . '/' . $template_name)) {
			$located = STYLESHEETPATH . '/' . $template_name;
			break;
		} elseif ( file_exists(TEMPLATEPATH . '/' . $template_name) ) {
			$located = TEMPLATEPATH . '/' . $template_name;
			break;
		}
	}

	if ( $load && '' != $located )
		load_template( $located, $require_once );

	return $located;
}

 上面的几个函数主要是选择哪个模板,模板路径如何得来,加载的过程展示。有了这些代码的基础,我们对url的不同参数,应该出什么模板,对应的模板应该找什么php文件,就很清楚了。后面我们讲到模板的编写时候,也会有对应的模板加载相关的内容。

  

免责声明:文章转载自《wordpress学习二:源码目录结构和启动流程》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇java枚举的定义标准格式IEEE1588精确网络时钟同步协议简介 I下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Jupyter修改默认目录

Jupyter修改默认目录 (windows版本) 1、打开Anaconda Prompt 在菜单栏中搜索即可 2、打开Jupyter安装目录(以下安装目录是我的安装目录,作为示例) cd D:PythonAnaconda3Scripts 3、输入以下命令,生成Jupyter_notebook_config.py 文件 jupyter notebook -...

eclpise的Navigator和Package的区别 &tomcat、java环境变量详解

本来只想写写几个视图的区别,感觉配置服务器这种事情经常重复。比较简单,但有时候挺麻烦,记录下来,学习的同时,方便以后查找。 一、eclipse两大视图区别 Navigator 这个就是Navigator的视图,source以及resource是以文件夹为单位形式整合显示的。其实这个就是文件(类)在电脑上的物理路径,这种形式也更贴切编码的路径(一个包的形式...

修改apk里面的源码

1.解压apk文件,获取classes.dex并拷贝到资源根目录(使用zip或其他解压工具即可) 2.使用baksmali工具将classes.dex转为smali文件,在命令行定位到资源根目录并执行: java -jar baksmali-2.0.3.jar -x classes.dex 执行完后会在当前目录下生成out目录,目录结构跟源码相同,在对应...

Linux SVN 操作详解(转)

1、将文件checkout到本地目录 svn checkoutpath(path是服务器上的目录)例如:svn checkout svn://192.168.1.1/pro/domain简写:svn co 2、往版本库中添加新的文件 svnaddfile例如:svnaddtest.php(添加test.php)svnadd*.php(添加当前目录下所...

不知道怎么提高代码可扩展性?来看看优秀框架源码中的这几种设计模式吧!

为什么要提高代码扩展性 我们写的代码都是为了一定的需求服务的,但是这些需求并不是一成不变的,当需求变更了,如果我们代码的扩展性很好,我们可能只需要简单的添加或者删除模块就行了,如果扩展性不好,可能所有代码都需要重写,那就是一场灾难了,所以提高代码的扩展性是势在必行的。怎样才算有好的扩展性呢?好的扩展性应该具备以下特征: 需求变更时,代码不需要重写。 局部...

Django第三章、下载和基本配置

目录 三. Django下载及简单配置 一.django下载 1、下载Django: 2、创建一个django project 3、在mysite目录下创建应用 4、启动django项目 基于Django实现一个简单的示例 url控制器(第一步) 三. Django下载及简单配置 一.django下载 Django官网下载页面 1、下载Djang...