Learn how WordPress source code works





Before we jump into working with advanced WordPress like approach WordPress Core and use it, we should at least have a good understanding of how the WordPress source code does things like processing data, receiving access requests (when someone visits or does something on the website called 1 request). Understanding these basic concepts is very helpful for you to know what WordPress really is, and what we can do in WordPress.

But now, we should wonder what the WordPress source code can do? At a glance, we see it can do:

  • Personal blog.
  • News site, magazine.
  • Sales page.
  • Real estate website.
  • Classifieds page.
  • Electronic portfolio.
  • ….

But do we feel that the above types of websites all have one thing in common: storing input, managing content and allowing visitors to view content? We call it CMS – Content Management System (content management system). And WordPress is a PHP source code of type CMS.

How does the data work?

In the WordPress source code, it has been programmed with many features to help itself interact with databases such as MySQL to help users store soft data on the website. All the soft data stored in the database will include the text content entered into the website, the settings (because the settings will be saved as a data type) and some other miscellaneous data. other.

Learn how WordPress 15 source code works

If you have access to the database through phpMyAdmin or similar applications, you will see that WordPress has many data tables to hold the data to be saved to.

wp-database-table

Then, the features in the source code will interact with those data tables and output them to the browser to display as HTML through files that have been templated in the theme (we call them templates). These things you may not understand now but gradually you will understand it in this series.

See more:  Many popular WordPress plugins have security flaws

see more: WordPress database structure and optimization

Learn how WordPress 15 source code works

What does WordPress do every time someone visits the website?

When a person visits the website, WordPress will automatically handle the following process:

Step 1 – Launch the source code

File index.php in the source code (not of the theme or plugin) is loaded, then it will retrieve other core files like wp-config.php capital to connect to the database, wp-settings.php…. You can open the index.php file to view and follow the embedded files to know the process it loads in the order of the files.

In this step, WordPress will connect to the database set up in wp-config.phpthen will proceed to load the features in the source code like /wp-include/functions.php, /wp-include/options.php… to preload the necessary functions to be able to receive the data.

And also in this step, all the data is saved to the table wp_options has value in column autoload to be true will be pulled out, whether or not that setting is used. The purpose of this step is to make available the website options and plugins to support the following steps, especially the plugin loading step to make it work correctly. That’s why in the article on optimizing the wp_options table, I recommend that you delete the unused data rows in this table to reduce the load time.

Learn how WordPress 15 source code works

Step 2 – Activate the plugin

The plugins that you are activating in your website will be loaded as soon as the WordPress source code has finished bootstrapping. Because the features in WordPress Core used in the plugin will usually be attached to the hook init (you temporarily understand it as an anchor point to trigger a certain scenario) so it will load as soon as WordPress starts.

Step 3 – Execute the functions.php file in the theme

At this point, WordPress will proceed to probe to the functions.php file in the active theme to load the features that the theme creator has declared there. So how can WordPress understand which theme the website is using? That is in step 1, it connected to the database and based on the current_theme key in the option_name column in the wp_options table.

WordPress determines more current through database

WordPress determines more current through database

Step 4 – Query Analysis and Query Initialization

This is an important step for your website to be able to display content to the outside, because the content will be returned after the queries are sent to the database. If you want to know how it analyzes then see here.

Query means a command that is sent to the database to get the information that the query needs to see. The query here is the SQL query sent to MySQL Server.

WordPress will first run the function wp() set in /wp-include/functions.php capital to call the method $wp->main() for query setup purposes. Subject $wp generated by the WP class in /wp-include/class-wp.php.

Learn how WordPress 15 source code works

/**
 * Set up the WordPress query.
 *
 * @since 2.0.0
 *
 * @param string|array $query_vars Default WP_Query arguments.
 */
function wp( $query_vars = ” ) {
    global $wp, $wp_query, $wp_the_query;
    $wp->main( $query_vars );

    if ( !isset($wp_the_query) )
        $wp_the_query = $wp_query;
}

At this point, the method $wp->parse_request() will be started for the purpose of analyzing the query based on the website’s path. Since you know that WordPress will automatically generate the query based on the path, for example when we enter http://domain.com/?p=123 then WordPress will send a query to the database to get the data of the post with the ID number 123.

See more:  JetPack Carousel Guide

After the query is parsed, the next thing WordPress will do is to set up the conditional functions via the . method $wp_query->parse_query(). It will then convert the generated queries into SQL commands that are sent to MySQL to retrieve the article data using the $wp_query->get_posts(). If the database has data, the articles will be retrieved after submitting the query and it will be saved to the object $wp_query for it to use in loops for displaying posts.

During the submission of this query, if it does not find any data, it will analyze and display a 404 error.

Learn how WordPress 15 source code works

And finally it has data, then it will set the variable $post for use in the loop. Variable $post is the object containing the data of the article through attributes. In this section, we’ll dig deeper into the in-depth understanding of queries and loops.

Step 5 – Execute the template files in the theme

After it has the article data and related data that it did in step 4, it will proceed to process the theme’s template files through the template structure. The content and homepage of the website will then be displayed based on the template tags in the form of HTML. We will learn more about this in the next post.

What can we do with the WordPress source code?

A question arises, what can we do to interfere with the WordPress source code to control it to work as we want, or in other words to create more features and change the display interface.

See more:  Make a multilingual WordPress website with PolyLang

It is not natural that I detailed the WordPress data processing process above, but it will make it easier for you to understand in this section.

Learn how WordPress 15 source code works

First, we can interfere with WordPress features and recreate it which was handled by WordPress in step 1 as above. The reason we can do it is because the source code of WordPress is written in such a way that we can indirectly interfere with it and can reproduce it (through classes). And the features we create ourselves from tampering with the source code can be declared as a plugin and it will be used as soon as WordPress starts up the necessary functions.

Second, we can interfere with the class WP_Query to create your own query to get any data in the database (many queries can be used by a function, like get_post_meta() such as). We apply WP_Query most on getting data of articles (including post, page, attachment, …).

Third, we can create the theme, then manually customize the template files in it so that the WordPress content displays according to our intentions. Because after WordPress sends a query to get data, the display is decided by the templates, and in the template will be the PHP code combined with HTML/CSS to display it on the website.

Those are the things we can do.

Learn how WordPress 15 source code works

So how?

Good question, we already know how WordPress loads when a traffic comes in, then functions are executed sequentially and knows what we can do. As for how, we will learn later in this Advanced WordPress Learning series.


Source: Learn how WordPress source code works
– TechtipsnReview

, , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *