WordPress文件调用加载顺序(含矢量示意图)

操作环境

操作系统信息:

master@ubuntu:~$ lsb_release -a
No LSB modules are available.
Distributor ID:    Ubuntu
Description:    Ubuntu 16.04.6 LTS
Release:    16.04
Codename:    xenial

WordPress 信息:

主程序版本:WordPress 5.3.2
主题及版本:Twenty Sixteen(版本:2.0, 由 WordPress 团队开发)

用到的PHP函数

  • 引入 PHP 文件,若出现错误,则程序停止。
require();
  • 引入且只引入一次 PHP 文件,若出现错误,则程序停止。
require_once();

文件调用流程矢量图

图 1

点击这里可以查看大图。

文件作用说明

index.php

无论我们访问 WordPress 站点的那个页面,WordPress 默认都会先加载根目录下的 index.php 文件。该文件除了加载 wp-blog-header.php 之外什么也不做。

关键代码:

<?php
define( 'WP_USE_THEMES', true );

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

wp-blog-header.php

该文件用来初始化 WordPress 的运行环境,通过调用相应的文件完成数据库的连接以及插件和模板的激活。

关键代码:

<?php
if ( ! isset( $wp_did_header ) ) {

        $wp_did_header = true;

        // Load the WordPress library.
        require_once( dirname( __FILE__ ) . '/wp-load.php' );

        // Set up the WordPress query.
        wp();

        // Load the theme template.
        require_once( ABSPATH . WPINC . '/template-loader.php' );
}

wp-load.php

该文件主要用于找到 wp-config.php 文件。一般情况下,如果我们在同一个目录下只安装一个 WordPress, 则 wp-config.phpwp-load.php 都在统一目录下,但是在安装多个 WordPress 的情况下,wp-config.php 可能需要移到其他目录,以避免 WordPress 错误地使用了其他网站的 wp-config.php 文件。

关键代码:

if ( file_exists( ABSPATH . 'wp-config.php' ) ) {

        /** The config file resides in ABSPATH */
        require_once( ABSPATH . 'wp-config.php' );

} elseif ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {

        /** The config file resides one level above ABSPATH but is not part of another installation */
        require_once( dirname( ABSPATH ) . '/wp-config.php' );

} else {... ...}

wp-config.php

该文件为 WordPress 基础配置文件,其中包含数据库信息,如数据库主机地址、数据库用户名、数据库密码、数据库名称等。

关键代码 1:

// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define( 'DB_NAME', 'wp' );

/** MySQL数据库用户名 */
define( 'DB_USER', 'root' );

/** MySQL数据库密码 */
define( 'DB_PASSWORD', '123456' );

/** MySQL主机 */
define( 'DB_HOST', 'localhost' );

/** 创建数据表时默认的文字编码 */
define( 'DB_CHARSET', 'utf8mb4' );

/** 数据库整理类型。如不确定请勿更改 */
define( 'DB_COLLATE', '' );

关键代码 2:

/** 设置WordPress变量和包含文件。 */
require_once( ABSPATH . 'wp-settings.php' );

wp-settings.php

该文件会调用一些 WordPress 正常运行所必须的文件和类库、注册时区、检查所需文件是否正常加载、检查是否处于 DEBUG 模式和调用已激活的插件等。

关键代码:

// Load active plugins.
foreach ( wp_get_active_and_valid_plugins() as $plugin ) {
        wp_register_plugin_realpath( $plugin );
        include_once( $plugin );

        /**
         * Fires once a single activated plugin has loaded.
         *
         * @since 5.1.0
         *
         * @param string $plugin Full path to the plugin's main file.
         */
        do_action( 'plugin_loaded', $plugin );
}
unset( $plugin );

template-loader.php

该文件不在根目录下,而是在 /wp-includes 目录下。该文件主要的作用是加载站点首页、文章页和分类页等处需要用到的模板文件。

关键代码:

if ( wp_using_themes() ) {

        $tag_templates = array(
                'is_embed'             => 'get_embed_template',
                'is_404'               => 'get_404_template',
                'is_search'            => 'get_search_template',
                'is_front_page'        => 'get_front_page_template',
                'is_home'              => 'get_home_template',
                'is_privacy_policy'    => 'get_privacy_policy_template',
                'is_post_type_archive' => 'get_post_type_archive_template',
                'is_tax'               => 'get_taxonomy_template',
                'is_attachment'        => 'get_attachment_template',
                'is_single'            => 'get_single_template',
                'is_page'              => 'get_page_template',
                'is_singular'          => 'get_singular_template',
                'is_category'          => 'get_category_template',
                'is_tag'               => 'get_tag_template',
                'is_author'            => 'get_author_template',
                'is_date'              => 'get_date_template',
                'is_archive'           => 'get_archive_template',
        );
... ...

EOF


荒原之梦网全部内容均为原创,提供了涵盖考研数学基础知识、考研数学真题、考研数学练习题和计算机科学等方面,大量精心研发的学习资源。

意见反馈 | 内容纠错 | 微信 | QQ | 公众号 | 知乎 | 微博 | 博客园 |CSDN | B 站 | 电子邮件
豫 ICP 备 17023611 号-1 | 公网安备 - 荒原之梦 豫公网安备 41142502000132 号 | SiteMap
Copyright © 2017-2024 ZhaoKaifeng.com 版权所有 All Rights Reserved.

Copyright © 2024   zhaokaifeng.com   All Rights Reserved.
豫ICP备17023611号-1
 豫公网安备41142502000132号

荒原之梦 自豪地采用WordPress