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

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

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

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

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

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

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

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

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

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文件,就很清楚了。后面我们讲到模板的编写时候,也会有对应的模板加载相关的内容。

  

posted on 2015-06-23 21:34  一颗麦粒  阅读(840)  评论(0编辑  收藏  举报

导航