WordPress: wp-blog-header.php

0
565
Wordpress wp-glob-header.php

In this article, I look at WordPress’ wp-blog-header.php file.

In a prior article I talked about the WordPress index.php file. We saw that inside that two lines of code, that the wp-blog-header.php file was included to kick things off. Now’s the time we take a look inside this source file to see whats in it.

It’s a short file, no more than a few lines of PHP source:

  1. if (!isset($wp_did_header) ) {
  2.   $wp_did_header = true;
  3.   require_once( dirname(__FILE__) . '/wp-load.php' );
  4.   wp();
  5.   require_once( ABSPATH . WPINC . '/template-loader.php' );
  6. }

$wp_did_header is a variable that is tested to see if it was set with a non null value. This serves as a semaphore to only execute the code in the block. After doing a complete search through the source code, however, $wp_did_header never is set prior other than the following line in WordPress core. That doesn’t mean though that it can’t be useful as a global variable to plugins and themes.

In template.php, it is recognized in load_template():

function load_template( $_template_file, $require_once = true ) {
  global $posts, $post, $wp_did_header, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
 
  if (is_array($wp_query->query_vars)) {
    extract($wp_query->query_vars, EXTR_SKIP);
  }
  if (isset($s)) {
    $s = esc_attr($s);
  }
  if ($require_once) {
    require_once($_template_file);
  } else {
    require($_template_file );
  }
}

What this does is make $wp_did_header (as well as the rest of the variables) globally recognized within the load template function call prior to the template file being loaded via require_once/require.

Continuing, we see that the wp-load.php file is then included. I won’t get into detail of this file and leave this for further discussion but basically it is WordPress’ bootstrap file that has a whole lot in it. After, the wp() function is called. The wp() function exists in /wp-includes/functions.php and was included as part of wp-load.php execution. It looks like this:

  1. function wp($query_vars = '') {
  2.   global $wp, $wp_query, $wp_the_query;
  3.   $wp->main($query_vars);
  4.  
  5.   if (!isset($wp_the_query))
  6.     $wp_the_query = $wp_query;
  7. }

$wp is a global variable that references the WordPress class. This class represents the WordPress operating environment. What it does is call up the WordPress main method() that sets up this WordPress operating environment. Therefore, a context exists for each URL requested.

A check is then made if the $wp_the_query variable was set in the $wp->main() operation, it is set to a global variable, $wp_the_query.

Finally, the template-loader.php file is loaded. What this does is look at the URL and include a template file to be associated with the HTTP request. This can be things like the 404, search, page, category, front, home, and other template pages.