This article is for those curious legends who crave to understand WordPress better. It’s perfect for web designers eager to know what is going on under the hood. Also, if you are a hard-working digital marketer or intermediate-level WordPress developer, you will like it.
But even if you are an experienced web developer, we would love to hear your feedback in the comment section. I will try to make this article easy to understand without too many technical details because there’s a lot of information on the Internet. But our goal today is to cover the most frequently asked questions about PHP in the context of WordPress.
While PHP is the most popular backend programming language in the world, WordPress is the most popular CMS platform in the world. So, this couple is really interesting.
Table of Contents
- What Is PHP?
- How Is PHP Involved in Rendering WordPress Pages?
- Object-Oriented Programming Concept
- What Is a WordPress Theme?
- WP_Query, Main Query, and WordPress Loop
- FAQ
- Takeaway
What Is PHP?
PHP is a programming language; to be precise, it’s a server-side scripting language, which means that it processes data on the server and generates dynamic content before sending it to the client’s browser. It is also often used in conjunction with databases.
For many years, even with the invention of more modern solutions, it still remains one of the most popular languages for web development. Here are some of the reasons why:
- It’s quite fast when it comes to server-side web processing.
- PHP is supported by most of the shared hosting providers, so small and middle-sized projects can rely on it instead of building the whole custom structure from scratch.
- PHP has excellent support for interacting with databases.
- The PHP ecosystem is huge, stable, and mature, and we are talking about frameworks and libraries, as well as tons of tutorials, documentation, and troubleshooting because of the massive community.
- The most popular CMS platforms, such as WordPress, OpenCart, Drupal, Magento, Joomla, and many others, are based on PHP. Also, such websites as Facebook and Wikipedia are based on it.
As of now, PHP is used by almost 77% of all the websites in the world that are based on server-side programming (according to W3Techs), which means that 8 out of 10 websites you visit are running on PHP.
PHP versions
You may have heard a lot about updating the PHP version to improve WordPress performance and security. But what does it actually mean?
PHP is an open-source language, and it’s one of many reasons why there must be a strictly defined lifespan for each release so the language can evolve. That’s why each PHP stable release is supported for two years, which means that bugs and security issues are being fixed regularly (and users receive such updates in point versions). After two years, there’s one more year of only critical updates for security fixes. And then, it reaches the “end of life,” meaning updates for that version are no longer being released.
You can always check the current supported version on the official website of PHP Group.
Not only the security issues are the reason for updating the PHP version. Website speed also depends on the PHP version because each release comes with performance improvements. However, while the difference between the 5.6 and 7.0 versions was the whole 400%, now, this number is smaller, e.g., the 8.1 version is just around 20% faster than 7.0.
Does it mean that the developers stopped working on the optimization? Not at all; they just have done a great job already.
To boil it down, if you continue using the unsupported PHP version, you do it at your own risk, and only you are responsible for security fixes connected to the PHP, as well as the performance drawbacks. Also, some versions come up with some changes in syntax, so plugins and theme developers keep up with such releases and update their code. This is one of the reasons why it’s so important to update plugins and themes regularly.
How Is PHP Involved in Rendering WordPress Pages?
To understand what’s going on under the hood when the user opens a website and how PHP is involved there, let’s break this process down into the main steps. I will not go too deep into technicalities because it’s important to get the idea.
- The user requests the WordPress page by clicking the URL.
The browser parses the HTTP/HTTPS protocol, domain (e.g., mysite.com), path (e.g., /blog), and the specific query parameters (e.g., id=123&hl=en). - The server receives the requests.
It checks it to figure out how to handle it. If the request should be handled by PHP (in most cases for PHP-based sites), it routes the request to the PHP interpreter. - Processing the PHP requests.
For this, the server must load the PHP interpreter, gathering information about the request (whether it’s GET or POST), the query parameters, cookies, session data, files uploaded, etc., so the request can be processed properly.
WordPress also executes an initialization script (wp-config.php) that loads configuration files, sets up error handling, and establishes database connections. - PHP execution.
At this stage, the interpreter executes what was specified in the request: retrieves or writes content from/to the database, renders templates with the dynamic content that PHP snippets (which you can see in any template file that is typically a mix of HTML and PHP) generated, and executes plugins. - Generating the response.
No, the content is being “packed” for sending back to the client (browser), and GZIP compression or encoding is being applied if such was intended. - Sending a response and cleanup of the environment.
Now, it’s time to send the preprocessed HTML files (as well as all others, such as CSS, JavaScript, fonts, images, etc.) to the browser and do a cleanup: close all the database connections and stop using server resources for PHP execution.
Now, it’s the browser’s turn to work on the files it received and render a page you see on the screen.
Object-Oriented Programming Concept
All the necessary information about WordPress classes, objects, template tags, and other important components can be found in the developers’ documentation. But to understand some terms from there, it’s better to have an idea of what object-oriented programming is (and PHP is an object-oriented programming language).
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to structure and organize code. OOP is a powerful and widely used approach for creating modular, maintainable, and reusable code. Here’s an explanation of OOP in PHP.
OOP key concepts explained
Classes
A class is a template that defines the structure, behavior, and properties of objects. It encapsulates data properties (attributes) and functions (methods) that operate on that data. Classes serve as a blueprint for creating objects.
💥 Classes are like a fictional universe with its own rules and superheroes, for example, Marvel, DC, or Harry Potter’s world. Superheroes have particular properties (e.g., in Harry Potter’s world, they own magic wands, have robes, etc.) and methods (actions they are capable of, e.g., flying, using spells, etc.). All of them have these qualities.
Objects
An object is an instance of a class.
Object is a concrete, tangible entity created based on the class’s blueprint. Each object has its own unique state (attributes) and can perform actions (methods) defined by the class.
💥 In a frame of an object, you define what properties and actions the particular superhero objects should have. So now, we are talking about specific superheroes, not all of them. For example, object Harry Potter can speak to snakes and read some of Voldemort’s thoughts.
Or, a Catwoman can be played by Michelle Pfeiffer, Halle Berry, or other actresses, but her “object” properties are pretty much the same: she is wearing a black catsuit, loves diamonds, and can jump very well.
Properties (Attributes)
Properties are variables that store data within a class. They define the characteristics or state of an object.
💥 I’ve already mentioned them: in the Harry Potter universe (class), Harry Potter (object) can have certain properties (magic wand, do magic, glasses, etc.).
Methods
Methods are functions defined within a class that perform specific actions or behaviors associated with objects of that class. Methods allow you to encapsulate functionality related to the class.
💥 Using our example, in the Harry Potter universe (class), Harry Potter (object) can have certain properties (magic wand, do magic, glasses, etc.) and perform certain actions, such as flying on a broom or using spells.
Well, if you don’t like Harry Potter, you may like Superman. So, read this code that determines his world using OOP terms.
To understand it better, please note:
- the dollar sign ($) is used to denote variables, including both object attributes (properties) and standalone variables. Object attributes are accessed using the arrow operator within the context of an object (e.g., $objectInstance->propertyName;), while standalone variables are used independently without an object (e.g., $objectInstance;).
- “echo” returns values of variables, strings, expressions, etc., in a simple, human-readable format.
class Superhero {
// Properties (Characteristics)
public $name;
public $powerLevel;
public $costumeColor;
// Constructor to initialize properties
public function __construct($name, $powerLevel, $costumeColor) {
$this->name = $name;
$this->powerLevel = $powerLevel;
$this->costumeColor = $costumeColor;
}
// Method 1: Flying
public function fly() {
return $this->name . ' is flying through the sky!';
}
// Method 2: Shooting lasers
public function shootLasers() {
return $this->name . ' is shooting powerful lasers from their eyes!';
}
// Method 3: Saving people
public function savePeople($numPeople) {
return $this->name . ' saved ' . $numPeople . ' people!';
}
}
// Create a superhero object
$superman = new Superhero('Superman', 100, 'blue and red');
// Access properties and call methods
echo 'Superhero Name: ' . $superman->name . '<br>';
echo 'Power Level: ' . $superman->powerLevel . '<br>';
echo 'Costume Color: ' . $superman->costumeColor . '<br>';
echo $superman->fly() . '<br>';
echo $superman->shootLasers() . '<br>';
echo $superman->savePeople(5) . '<br>';
Such an OOP structure makes it possible to create well-organized and reusable code without a need to declare and individually write a set of properties, methods, and rules for each particular thing you use in the code.
In WordPress, classes, including core classes like WP_Query and others, are defined within the WordPress core files, and many of these core files can be found in the wp-includes directory.
Cheatsheet: 10 most frequently used PHP functions in WordPress
In WordPress development, several PHP functions are commonly used to interact with the WordPress core, manipulate data, and customize themes or plugins. Here are ten of the most frequently used PHP functions in WordPress, and understanding what they do will help you to get the idea of what’s going on in the code better as a non-developer:
- wp_query()
This function initializes the main query in WordPress. Developers often use it to create custom queries for retrieving posts or other content. - the_title()
Used to display the title of the current post within The Loop. It’s frequently employed in theme templates. - the_content()
Displays the content of the current post within The Loop. It’s used to output the main content of posts and pages. - get_template_part()
Enables you to include reusable template parts in your themes. It’s handy for keeping your code organized and DRY (Don’t Repeat Yourself). - get_permalink()
Retrieves and displays the URL (permalink) of the current post or a specified post by passing its ID as an argument. - get_option()
Used to retrieve values from the WordPress options table, making it helpful for accessing settings and configuration data. - wp_enqueue_script()
Registers and enqueues JavaScript files in WordPress. It’s essential for adding custom scripts to your theme or plugin. - wp_enqueue_style()
Similar to wp_enqueue_script(), this function registers and enqueues CSS stylesheets in WordPress themes and plugins. - add_action()
This function is crucial for adding custom hooks and actions to WordPress. It allows you to execute your own code at specific points in the WordPress lifecycle. - add_filter()
Used to add custom filters, allowing you to modify data or output generated by WordPress, themes, or plugins.
What Is a WordPress Theme?
WordPress theme is a front-end part that determines a website design and the visible content structure. It’s basically a collection of files with templates, layouts, and customization settings. Themes also give you a toolset to slightly edit how the website looks and interacts with a user without having to dig into code. For example, you can modify colors, fonts, some styles of layouts, set up the main page, site identity, etc. The number of such settings available for direct customization is determined by the theme.
WordPress themes hierarchy
When the block (FSE) themes first appeared, they offered a different approach to the templates, where global settings and styles are stored in the theme.json file (which is unique for such block themes).
Just like classic themes, block themes use files for determining different templates. So, understanding the hierarchy is essential, no matter how the theme is built.
Classic themes consist of PHP templates, and this is their hierarchy:
The logic of it is quite simple: to move from right to left. If there are no templates for a particular post type/page, WordPress will use the templates that are one level higher and continue until it reaches the top level.
This structure is efficient because it’s universal and implies fallback solutions if some templates are not there.
As you can see, there are two largest categories here: for singular and for archive. The basis of such templates are the WordPress loop and the WP_Query.
WordPress template tags
If you see something like get_header();, get_content();, or get_footer(); in the theme template, let me introduce you to functions that are called WordPress template tags. They are used to fetch dynamic content within the template and are declared in the wp-includes folder.
They fetch certain content and give theme developers a chance to create dynamic websites instead of duplicating content on every page.
WP_Query, Main Query, and WordPress Loop
When the user opens a page, some data is retrieved from the database. And, as we talked about it before, in WordPress, PHP is responsible for it, and it’s querying that specific information from the database.
WordPress relies heavily on the Loop. It’s a PHP code structure used to retrieve and display a collection of posts or other content from the WordPress database.
The loop gets data from the main query, which is a default query, or wp_the_query. If you see the PHP code structure that begins with if (have_posts()) and contains while (have_posts()) – it is The Loop. It’s used to fetch and display posts.
The main query is determined by the wp_the_query global variable. In simple terms, when you open a website, this is the information that is inquired from the database by default. If you want to customize the output of this query, use WordPress hooks.
WP_Query is a class in WordPress that allows you to create custom queries. This query generator will be really helpful for understanding how it works.
Crocoblock Query Builder
The freedom of fetching and displaying very specific content on particular pages of your website is a must for complex and highly dynamic websites. That’s why Crocoblock developers have created a Query Builder, which is a part of the JetEngine plugin. So now, you can build queries of any complexity without having coding skills. All you have to do is to open the interface and choose what exactly you want to fetch, and then (using JetEngine listings, dynamic tags, and widgets/blocks) define how this content will displayed and style it.
FAQ
PHP, as a programming language, and WordPress, as a CMS platform, are both open-source and completely free.
It’s recommended to use the latest stable version of PHP. Also, make sure that your WordPress and plugin versions are up to date to be compatible with that PHP release. However, the supported versions (released not later than two years ago) are good to use. It’s strictly not recommended to use the versions that reached the “end of life” stage and are no longer supported.
Takeaway
PHP is the basis of the WordPress CMS platform, and it has its own specific classes and functions. Understanding their basics can be really helpful if you are not a developer: it will give you a better understanding of WordPress and how it works. That’s why this article is here to list some of its essential things and outline the idea of how WordPress works under the hood.