看wp源码时看到以下:
if ( !isset($wp_did_header) ) {
$wp_did_header = true;
require_once( dirname(__FILE__) . '/wp-load.php' );
wp();
require_once( ABSPATH . WPINC . '/template-loader.php' );
}
当请求开始时$wp_did_header永远都没有设置过,那么这个if语句就没有存在的必要了吧?
It's a global variable that can be checked by user functions and filters to see whether or not WordPress has already sent headers. It's just there to help.
-- http://wordpress.stackexchange.com/questions/20616/whats-the-purpose-of-wp-did-header
但是,我更赞同下面的解释:
对$wp_did_header进行赋值,这样如果代码块已经执行过,判断就会失败,代码块就不会再执行。这种做法可以确保wp-blog-header.php文件只执行一次(重复执行的话会出现函数名冲突、变量重置等,WordPress会精神分裂的!);
-- http://www.ecdoer.com/post/wordpress-source-analysis.html
通过在源码中进行全局搜索,也可以看出:
$ grep wp_did_header ./ -nR
./wp-blog-header.php:8:if ( !isset($wp_did_header) ) {
./wp-blog-header.php:10: $wp_did_header = true;
./wp-includes/template.php:495: global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;