How WordPress Works

Before diving into the details of plugins, let’s very briefly review at a high-level how WordPress handles requests.

WordPress is implemented in PHP which is a server-side scripting language. This means the web server invokes the WordPress PHP code for each web page view. The server and PHP engine use various optimizations to provide good performance. All data is stored in a database on the server set up for WordPress. Again, optimizations are done for performance reasons and often a cached version of the data is stored in memory.

WordPress defines action and filter hooks as a way to change the built-in behavior. Hooks are also used to deal with ordering issues. For example, the code for the plugins is loaded BEFORE the current user is known. So plugins that act differently based on the user need to finish initialization using the 'init' action hook. This action hook is called after all of WordPress is loaded and the user determined. There are many hooks available to developers.

WordPress is designed such that everything is a query of the database and the results of that query determines the response sent back to the browser. That response is HTML or CSS or JavaScript; the PHP code never leaves the server.

If you have plain permalinks enabled then your URL will end with a query–such as ?p=123. The item with an ID of 123 is retrieved from the WordPress database and used to create the HTML response. Most sites use “pretty” permalinks, which are search-engine friendly, and your code can use the function url_to_postid() to figure out the actual query ID.

To view a web page, the user’s browser does an HTTP(S) GET request for the web page. This GET causes the web server to invoke the WordPress index.php file to begin creating the response to go back to the user’s browser. The index.php file in turn loads wp-config.php (which defines your site configuration). wp-config.php in turn loads wp-setting.php which then loads the various main WordPress php files, loads all active plugins, sets up the web page query based on the URL, and loads the active parent and child theme. When everything (such as users and roles) is setup the 'init' action is called followed by the 'wp_loaded' action.

At this point, everything has been setup and the query can be processed to create the response.