Debug WordPress To See How It is Works

It’s no doubt that WordPress is one of the most powerful CMSs for blogging. Besides being free and open source, it is also user friendly. But do you know how exactly WordPress executes its code? Well, this idea came up in my mind, so I would like to trace how WordPress executes its code.

When visitors visit your WordPress blog, it only serves through index.php, even if your visitors click any links in your blog (post, category, page, blogroll, archive, feed links), it will serve only from index.php. So when I try to open index.php, it only has 2 lines of code to execute.

See the code below:

define('WP_USE_THEMES', true);
require('./wp-blog-header.php');

It’s surprising, isn’t it?

So to trace the code, I’m using XDebug, and place the XDebug code in the index.php:

define('WP_USE_THEMES', true);
xdebug_start_trace('c:/trace/wordpress.txt');
require('./wp-blog-header.php');
xdebug_stop_trace();

And I got so many lines of debug executed code. Then I followed it, and opened wp-blog-header.php. I got this code and also placed the XDebug code in wp() as well:

if ( !isset($wp_did_header) ) {
	$wp_did_header = true;
	require_once( dirname(__FILE__) . '/wp-load.php' );
        xdebug_start_trace('c:/trace/wordpress.txt');
	wp();
        xdebug_stop_trace();
	require_once( ABSPATH . WPINC . '/template-loader.php' );
}

And still I got a bunch of debug executed code, but now it is easier to understand.

So what I learned so far by tracing this code is, it is useless that I put the post ID at the end of my URL (example: [site_url]/see-how-php-web-application-work-with-xdebug/). WordPress will parse the URL with this sequence: get the category, post title, and then post ID last. I should have put the post ID at the beginning, so it will increase WordPress performance since it will get the post ID in the first sequence of URL parsing.

I will test it in the next run by using a timer to see the difference between putting the ID at the beginning and at the end of URL rewriting.

Related reading:
See How PHP Web Application Work With XDebug
Debug Your PHP Application With XDebug