Find out how the wordpress works behind the scenes.167

how wordpress works
Author: InfoBeep - September 6, 2017 | subject to copyright

We all know WordPress just as a site which makes us easy to build and maintain our websites. It had made most of our lives simple as we do not need to code each and everything we want in our websites. In this article, we will try to provide you the information about the things that happens behind the scenes which is the reason website building has been a simple task for most of us.

Why you should learn how WordPress works?

WordPress is an open source software. Being an open source software, anyone can learn its code, understand it and can create their own plugins and templates. Which means you can learn it and help yourselves to make your site look better.

Below are the list of few things which we think are substantial:

  • Load wp-config.php file

It is the first file WordPress loads. Being the configuration file for WordPress, it contains your site database information and sets global variables for a WordPress site.

  • Setup Default Constants

In this stage, WordPress will look after the information such as WordPress upload location, maximum file sizes, and other default constants set in wp-config.php file.

  • Load advanced-cache.php file

This is the third stage and here WordPress checks if advanced-cache.php file exist or not. This file acts as a drop-in fileand is used mainly by popular WordPress caching plugins. If your site uses this file you may see Drop-ins

  • Load wp-content/db.php file

If you have tried to improve database performance of your WordPress site creating your own database abstraction layer then you may have db.php file inside wp-content folder which WordPress will load in this stage. This is the final step where WordPress checks for files.

  • Connect MySQL and select database

After WordPress has all the files required, it will now try to connect to the MySQL server and select the database. If connected it will move to the next step else it will show “Error establishing database connection” and quit right here.

  • Load object-cache.php or wp-incudes/cache.php file

Now WordPress will look for object-cache.php file. If it is not available it will load wp-includes/cache.php file.

  • Load wp-content/sunrise.php file

If you have a WordPress multisite network, then WordPress will now look for sunrise.php file which is available inside wp-content folder.

  • Load Localization Library

WordPress will now load l10n.php library available in the wp-includes folder which loads WordPress localization system, loads translations, sets locales, etc.

  • Load Multisite Plugins

Now if you have a multisite network, WordPress will load the multisite plugins:

  • Do Action ‘muplugins_loaded’

Now, the action muplugins_loaded is run by WordPress. This action is only available on multisite network for network activated plugins.

  • Load Active Plugins

WordPress will now look and load all the active_plugins entry in the options table of your WordPress Database. As it only looks for activated plugins, it helps WordPress to ignore plugins which are not activated but installed.

  • Load pluggable.php file

WordPress in this stage looks for pluggable.php file which contains function that can be redefined by WordPress. Here, WordPress looks for function inside the file and if not defined, WordPress will define those functions itself.

  • Do action ‘plugins_loaded’

Now, WordPress runs the action ‘plugins_loaded”. This function allows developer to hook their function after all other active plugins have been loaded.

  • Load Rewrite Rules

WordPress will now load rewrite rules which helps WordPress use SEO friendly URLs.

  • Instantiate $wp_query, $wp_rewrite, $wp

Here, WordPress loads 3 objects:

$wp_query: This object informs WordPress about the content requested in typical WordPress query format.

$wp_rewrite: This object contains your rewrite rules and functions that tells WordPress which URL to be used to display the requested content.

$wp: This object contains the functions that will parse your request and perform the main query.

  • Do action ‘setup_theme’

‘setup_theme’ action runs before WordPress theme is loaded.

  • Load child theme’s functions.php file

If you use a child theme in your website, WordPress will now load functions.php of your child theme else it will load your current active theme’s function.php file. This file acts as a plugin and is used to add theme specific features to your site.

  • Load parent theme’s function.php file

After child theme is loaded, WordPress will now load parent theme’s function.php file.

  • Do action ‘after_setup_theme’

After WordPress setup, all the theme and load their functions, it runs this action.

  • Setup current user object

WordPress in this stage loads the current user object. Here, WordPress manage the request according to user’s role and capabilities.

  • Do action ‘init’

Until this stage, WordPress has loaded all the crucial information needed.

Now in this stage, it fires ‘init’ action which allows developers to add code which is to be executed.

These codes are to be executed after WordPress loads all the information about which we have discussed above.

  • Do action ‘widget_init’

This action helps developer to do 2 things at the same time. It allows to register widgets and run code simultaneously.

  • Run wp ()

This function is located in wp-includes/function.php file. It sets up WordPress query

globals $wp, $wp_query, $wp_the_query and then cals $wp->main
  • Parse request

Until here, WordPress has all the required information to parse user request. Now it checks the rewrite rules to match the user’s request and finally it runs query variable filters, request action hook and sends header request.

  • Run Query

Now, WordPress will load query variables.

At first, it runs

WP_Query->get_posts()

Secondly, it fires DO_ACTION_REF_ARRAY ‘pre_get_posts’ action with WP_Query object.

Thirdly, it runs apply_filters to clean up query and run some final checks.

And finally, it takes out posts from the database and applies posts_result and the_post filters.

In case if there is no post that match the query, it will set _404 variable.

  • Do action ‘template_redirect’

This action runs before WordPress finalizes the template page to load.

  • Load feed template

WordPress will load this template, if you have requested this content in RSS feed.

  • Load template

Here, WordPress will load file based on template hierarchy. It generally loads template which contains WordPress loop.

  • Do action ‘shutdown’

After done with everything, right from generating codes to generating user’s requested web page, WordPress fires the final action called shutdown.

After this your webhosting server replies to your user, sending them page that contains HTML, CSS and Javascript code which is the instruction for user’s browser that says what to display as well as how to display in the screen.