wordpress代码结构分析

26点 林涛 7061℃ 0评论

作为一个“资深”的懒惰程序员,我承认我喜欢copy,虽然此法不妥,但能提高效率。这里复制了两位高人对wordpress源码的分析。拿来共享:

Wordpress是一个单入口的文件,所有的前端处理都必须经过index.php,这是通过修改web服务器的rewrite规则来实现的。这种做法的好处是显而易见的,这样URL更好看,不必为每一个url新建一个文件。

        我们看看wp大致的文件调用是什么样子的。

         

        wordpress可以分为3个阶段,一是初始化阶段,即初始化常量、环境、加载核心文件等等;二是内容处理阶段,即根据用户的请求调用相关函数获取和处理数据,为前端展示准备数据;三是主题应用阶段,在这个阶段,需要展示的数据已经准备完毕,需要根据用户的请求加载相应的主题模板,即对主题进行路由。经过这三各阶段,用户请求的页面就可以完全的展现出来了。从上图可以看到wp的初始化阶段相当的繁琐。

         Index.php文件实际上没啥内容. 定义了常量WP_USE_THEMES和加载了文件wp-blog-header.php

 

 view plaincopy

 

 

 

 

  1. <?php  
  2. /*index.php*/  
  3. //该常量定义为false时,不使用主题,站点会显示为空白,为true时,则正常显示。  
  4. define('WP_USE_THEMES', true);  
  5. //加载wp-blog-header.php  
  6. require('./wp-blog-header.php');  
  7. ?>  

       wp–blog-header里面是什么样子呢?

 

 

 view plaincopy

 

 

 

 

  1. <?php  
  2. /*wp-blog-header.php */  
  3. //wp_did_header变量,相当于一个flag,确保每次刷新时,wp-blog-header.php文件只执行一次。  
  4. if ( !isset($wp_did_header) ) {  
  5.   
  6.     $wp_did_header = true;//置为true,确保只执行一次。  
  7.     //初始化阶段。加载wp-load.php,  
  8.     require_once( dirname(__FILE__) . '/wp-load.php' );  
  9.     //内容处理阶段 wp()位于functon.php中  
  10.     wp();  
  11.     //主题应用阶段。ABSPATH wordpress目录的绝对路径  
  12.     require_once( ABSPATH . WPINC . '/template-loader.php' );  
  13.   
  14. }  

      wp-blog-header任然很简单,但是可以看到wp的三个明显的阶段,初始化阶段、内容处理阶段、主题应用阶段。





     wp-load.php的功能很简单,就是加载wp-config.php. wp-config.php是需要用户自己进行设置的。设置DB设置,密钥,路径,语言

 

 

 view plaincopy

 

 

 

 

  1. <?php  
  2. /*wp-load.php 
  3.  */  
  4.   
  5. // 定义常量 wordpress根目录的绝对路径   
  6. define( 'ABSPATH', dirname(__FILE__) . '/' );  
  7. //设置错误报告模式 这个if的分支 差别在E_RECOVERABLE_ERROR ?  
  8. if ( defined('E_RECOVERABLE_ERROR') )  
  9.     error_reporting(E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR);  
  10. else  
  11.     error_reporting(E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING);  
  12. //加载wp-config.php  
  13. if ( file_exists( ABSPATH . 'wp-config.php') ) {  
  14.   
  15.     /** The config file resides in ABSPATH */  
  16.     require_once( ABSPATH . 'wp-config.php' );  
  17.   
  18. elseif ( file_exists( dirname(ABSPATH) . '/wp-config.php' ) && ! file_exists( dirname(ABSPATH) . '/wp-settings.php' ) ) {  
  19.   
  20.     /** The config file resides one level above ABSPATH but is not part of another install*/  
  21.     //如果wp-config.php存在于绝对路径的上一级,而且不是另一个wordpress的一部分  
  22.     require_once( dirname(ABSPATH) . '/wp-config.php' );  
  23.   
  24. else {  
  25.   
  26.     // wp-config.php 不存在时,  
  27.   
  28.     // Set a path for the link to the installer  
  29.     if ( strpos($_SERVER['PHP_SELF'], 'wp-admin') !== false )  
  30.         $path = '';  
  31.     else  
  32.         $path = 'wp-admin/';  
  33.   
  34.     // Die with an error message  
  35.     require_once( ABSPATH . '/wp-includes/class-wp-error.php' );  
  36.     require_once( ABSPATH . '/wp-includes/functions.php' );  
  37.     require_once( ABSPATH . '/wp-includes/plugin.php' );  
  38.     $text_direction = /*WP_I18N_TEXT_DIRECTION*/'从左到右'/*/WP_I18N_TEXT_DIRECTION*/;  
  39.     wp_die(sprintf(/*WP_I18N_NO_CONFIG*/'看起来似乎没有 <code>wp-config.php</code> 文件。我们需要这个文件来让一切开始,可以查看<a href=\'http://codex.wordpress.org/Editing_wp-config.php\'>更多帮助</a>。 那么现在您可以通过这个 Web 界面创建一个 <code>wp-config.php</code> 文件,但并非所有主机都支持,安全的做法是手动创建。</p><p><a href=\'%ssetup-config.php\' class=\'button\'>试试创建一个配置文件</a>'/*/WP_I18N_NO_CONFIG*/$path), /*WP_I18N_ERROR_TITLE*/'WordPress › 错误'/*/WP_I18N_ERROR_TITLE*/array('text_direction' => $text_direction));  
  40. }  
  41. ?>  

 

来看看wp-config.php是什么样子的。

 

 view plaincopy

 

 

 

 

  1.  <?php  
  2. /*wp-config.php */  
  3.   
  4. // ** MySQL 设置 – 具体信息来自您正在使用的主机 ** //  
  5. /** WordPress 数据库的名称 */  
  6. define('DB_NAME', SAE_MYSQL_DB);  
  7.   
  8. /** MySQL 数据库用户名 */  
  9. define('DB_USER', SAE_MYSQL_USER);  
  10.   
  11. /** MySQL 数据库密码 */  
  12. define('DB_PASSWORD', SAE_MYSQL_PASS);  
  13.   
  14. /** MySQL 主机 */  
  15. define('DB_HOST', SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT);  
  16.   
  17. /** 创建数据表时默认的文字编码 */  
  18. define('DB_CHARSET''utf8');  
  19.   
  20. /** 数据库整理类型。如不确定请勿更改 */  
  21. define('DB_COLLATE''');  
  22.   
  23. define('WP_USE_MULTIPLE_DB', true);  
  24.   
  25. $db_list = array(  
  26.         'write'=> array(  
  27.             array(  
  28.                 'db_host' => SAE_MYSQL_HOST_M.':'.SAE_MYSQL_PORT,  
  29.                 'db_user'=> SAE_MYSQL_USER,  
  30.                 'db_password'=> SAE_MYSQL_PASS,  
  31.                 'db_name'=> SAE_MYSQL_DB,  
  32.                 'db_charset'=> 'utf8'  
  33.                 )  
  34.             ),  
  35.         'read'=> array(  
  36.             array(  
  37.                 'db_host' => SAE_MYSQL_HOST_S.':'.SAE_MYSQL_PORT,  
  38.                 'db_user'=> SAE_MYSQL_USER,  
  39.                 'db_password'=> SAE_MYSQL_PASS,  
  40.                 'db_name'=> SAE_MYSQL_DB,  
  41.                 'db_charset'=> 'utf8'  
  42.                 )  
  43.             ),  
  44.         );  
  45. $global_db_list = $db_list['write'];  
  46.   
  47. /**#@+ 
  48.  * 身份密匙设定。 
  49.  * 
  50.  * 您可以随意写一些字符 
  51.  * 或者直接访问 {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org 私钥生成服务}, 
  52.  * 任何修改都会导致 cookie 失效,所有用户必须重新登录。 
  53.  * 
  54.  * @since 2.6.0 
  55.  */  
  56. define('AUTH_KEY',         hash_hmac('sha1', SAE_ACCESSKEY . 'AUTH_KEY', SAE_SECRETKEY ));  
  57. define('SECURE_AUTH_KEY',  hash_hmac('sha1', SAE_ACCESSKEY . 'SECURE_AUTH_KEY', SAE_SECRETKEY ));  
  58. define('LOGGED_IN_KEY',    hash_hmac('sha1', SAE_ACCESSKEY . 'LOGGED_IN_KEY', SAE_SECRETKEY ));  
  59. define('NONCE_KEY',        hash_hmac('sha1', SAE_ACCESSKEY . 'NONCE_KEY', SAE_SECRETKEY ));  
  60. define('AUTH_SALT',        hash_hmac('sha1', SAE_ACCESSKEY . 'AUTH_SALT', SAE_SECRETKEY ));  
  61. define('SECURE_AUTH_SALT', hash_hmac('sha1', SAE_ACCESSKEY . 'SECURE_AUTH_SALT', SAE_SECRETKEY ));  
  62. define('LOGGED_IN_SALT',   hash_hmac('sha1', SAE_ACCESSKEY . 'LOGGED_IN_SALT', SAE_SECRETKEY ));  
  63. define('NONCE_SALT',       hash_hmac('sha1', SAE_ACCESSKEY . 'NONCE_SALT', SAE_SECRETKEY ));  
  64.   
  65. /**#@-*/  
  66.   
  67. /** 
  68.  * WordPress 数据表前缀。 
  69.  * 
  70.  * 如果您有在同一数据库内安装多个 WordPress 的需求,请为每个 WordPress 设置不同的数据表前缀。 
  71.  * 前缀名只能为数字、字母加下划线。 
  72.  */  
  73. $table_prefix  = 'wp_';  
  74.   
  75. /** 
  76.  * WordPress 语言设置,默认为英语。 
  77.  * 
  78.  * 本项设定能够让 WordPress 显示您需要的语言。 
  79.  * wp-content/languages 内应放置同名的 .mo 语言文件。 
  80.  * 要使用 WordPress 简体中文界面,只需填入 zh_CN。 
  81.  */  
  82. define ('WPLANG''zh_CN');  
  83.   
  84. /** 
  85.  * 开发者专用:WordPress 调试模式。 
  86.  * 
  87.  * 将这个值改为“true”,WordPress 将显示所有开发过程中的提示。 
  88.  * 强烈建议插件开发者在开发环境中启用本功能。 
  89.  */  
  90. define('WP_DEBUG', false);  
  91.   
  92. /* 好了!请不要再继续编辑。请保存该文件。 */  
  93.   
  94. /** WordPress 目录的绝对路径。 */  
  95. if ( !defined('ABSPATH') )  
  96.     define('ABSPATH', dirname(__FILE__) . '/');  
  97.   
  98. /** 设置 WordPress 变量和包含文件。 这个文件内容很多啊,分析较困难*/  
  99. require_once(ABSPATH . 'wp-settings.php');  

外一篇:

 

index.php
设置使用主题define(‘WP_USE_THEMES’, true);
加载wp-blog-header.php

wp-blog-header.php
加载wp-load.php
执行wp()
加载include/template-loader.php

wp-load.php
设置绝对路径:define( ‘ABSPATH’, dirname(__FILE__) . ‘/’ );
检测并加载wp-config.php,如果该文件不存在,提示安装WordPress,安装路径为wp-admin/setup-config.php

wp-config.php
设置数据库(数据库名,数据库帐号密码,数据库服务器ip)以及身份密钥
加载wp-settings.php

wp-settings.php
加载include/load.php
加载include/default-contants.php
加载include/version.php
初始化常量:wp_initial_constants( ),设置内存上限(单站32M多站64M)
关闭字符串转义:set_magic_quotes_runtime( 0 );
@ini_set( ‘magic_quotes_sybase’, 0 );
设置时区:
if ( function_exists( ‘date_default_timezone_set’ ) )
date_default_timezone_set( ‘UTC’ );
取消全局变量:
wp_unregister_GLOBALS();
保证以下全局变量与WordPress不冲突:
unset( $wp_filter, $cache_lastcommentmodified, $cache_lastpostdate );
标准化$_SERVER变量与值:
wp_fix_server_vars();
检查mysql与php版本:
wp_check_php_mysql_versions();
如果请求的是favicon.ico(因为如果favicon.ico存在,则不会请求至index.php),则返回。
wp_favicon_request();
检测是否在维护状态(检测.maintenance文件是否存在,并且时间是否在10分钟之内):
wp_maintenance();
设置起始运行时间点值:
timer_start();
检测是否是调试模式:
wp_debug_mode();
设置cache:
if ( WP_CACHE )
WP_DEBUG ? include( WP_CONTENT_DIR . ‘/advanced-cache.php’ ) : @include( WP_CONTENT_DIR . ‘/advanced-cache.php’ );
设置语言目录:
wp_set_lang_dir();
加载php兼容性支持文件:include/compat.php
加载公共方法文件:include/functions.php
加载WordPress核心类:include/classes.php
加载数据库访问对象:
require_wp_db();
?wp_set_wpdb_vars();
?wp_start_object_cache();
加载插件处理文件:include/plugin.php
加载默认过滤器:include/default-filters.php
加载语言操作文件:include/pomo/mo.php(po=>portable object,mo=>machine object)加载本地化处理文件:include/l10n.php

加载

include/formatting.php
include/capabilities.php
include/query.php
include/theme.php
include/user.php
include/meta.php
include/general-template.php
include/link-template.php
include/author-template.php
include/post.php
include/post-template.php
include/category.php
include/category-template.php
include/comment.php
include/comment-template.php
include/rewrite.php
include/feed.php
include/bookmark.php
include/bookmark-template.php
include/kses.php
include/cron.php
include/deprecated.php
include/script-loader.php
include/taxonomy.php
include/update.php
include/canonical.php
include/shortcodes.php
include/media.php
include/http.php
include/class-http.php
include/widgets.php
include/nav-menu.php
include/nav-menu-template.php

如需转载请注明: 转载自26点的博客

本文链接地址: wordpress代码结构分析

转载请注明:26点的博客 » wordpress代码结构分析

喜欢 (0)
发表我的评论
取消评论

表情