[SEO] How Wordpress load a post withPermalink / Pretty URL?

It is crucial to know how WP loads your URL in terms of SEO

Here is how it works

Assume that we have a URL like this:  http://my_domain/jw萬豪酒店加送多項婚宴優惠/

When Wordpress (v5.4) first load your post, the files are loaded like this:

- /index.php, then load
- /wp-blog-header.php

And wp-blog-header.php called a function called wp(), which leads to the following code:
	public function main( $query_args = '' ) {
		$this->init();
		$this->parse_request( $query_args );
		$this->send_headers();
		$this->query_posts();
		$this->handle_404();
		$this->register_globals();

		/**
		 * Fires once the WordPress environment has been set up.
		 *
		 * @since 2.1.0
		 *
		 * @param WP $this Current WordPress environment instance (passed by reference).
		 */
		do_action_ref_array( 'wp', array( &$this ) );
	}
Now if you add log:

	public function main( $query_args = '' ) {
		$this->init();
		$this->parse_request( $query_args );
		$this->send_headers();
		$this->query_posts();
		$this->handle_404();
		$this->register_globals();
                // Add log here
		error_log(var_export(['test_query_vars' => $this->query_vars], true));
		/**
		 * Fires once the WordPress environment has been set up.
		 *
		 * @since 2.1.0
		 *
		 * @param WP $this Current WordPress environment instance (passed by reference).
		 */
		do_action_ref_array( 'wp', array( &$this ) );
	}
You will see this:

array (
  'test_query_vars' => 
array ( 'page' => '', 'name' => 'jw%E8%90%AC%E8%B1%AA%E9%85%92%E5%BA%97%E5%8A%A0%E9%80%81%E5%A4%9A%E9%A0%85%E5%A9%9A%E5%AE%B4%E5%84%AA%E6%83%A0', ), )
As you can see, the "Slug" becomes the name of the WP query object.  Once WP has these info, all it is doing is to do a search and return the content.

The key?

The key is this line in wp-includes\class-wp.php:160

$rewrite = $wp_rewrite->wp_rewrite_rules();

If you dump the $rewrite, you will see a list of how WP change the permalink to a normal query string.

If you want more details, start by studying class-wp.php.

Hope it helps someone.

Comments

Popular posts from this blog

TCPDF How to show/display Chinese Character?

How to fix fancy box/Easy Fancybox scroll not work in mobile

Wordpress Load balancing: 2 web servers 1 MySQL without any Cloud services