- A Guide to WordPress PHP
- What is PHP?
- WordPress PHP
- How PHP Works: WordPress Websites
- PHP and WordPress
- WordPress PHP: Enable Debugging
- The Loop and the Query
- Hooks
- Wrapping Up: WordPress PHP
- What is: PHP
- How Does WordPress Use PHP?
- How Does PHP Work?
- WordPress PHP Versions
- Do WordPress Users Need to Know PHP?
- Additional Reading
A Guide to WordPress PHP
Looking to understand WordPress PHP? In this post, we’ll unpack what PHP is and how it works with WordPress. Understanding PHP and how it integrates with WordPress will help with site maintenance, troubleshooting errors and WordPress development in general.
What is PHP?
PHP, an abbreviation for Hypertext Preprocessor, is a scripting language used by approximately 79% of the web . PHP allows you to do many powerful things, such as interacting with a database, writing conditional statements, obtain WordPress-specific information, pull in media files and much more.
WordPress PHP
PHP is what powers WordPress, as most of the WordPress core files are written in this language. If you look at the file listing of a WordPress website, you’ll see that most files end in .php, meaning the file is PHP file. Most of the WordPress PHP files listed in the screenshot below are necessary for a WordPress website to operate.
How PHP Works: WordPress Websites
Let’s look at a real-world example of how PHP is used on a WordPress website. In this example, we will use a conditional statement to determine whether or not a user is logged in:
Even if you have no knowledge of PHP, you can take away that either a user is logged in or not logged in. We use a conditional (if/else) to check if the user is logged in or not, if they are then we will display a message welcoming them and if they are not then we will ask them to sign in. While this is just a basic example, you can see just how powerful conditionals can be.
You can see that we checked the function, “user_is_logged_in”. We know this is a function since it has the parentheses “()” after the name.
If the function returns false, then when the code is run it will display the message:
“You are not logged in! Please sign in.”
PHP and WordPress
Just from the example above, you will notice that PHP is found everywhere in WordPress. WordPress has subsystems like loops that control the number of posts shown, along with hooks that modify functionality, APIs, and themes and plugins.
WordPress PHP: Enable Debugging
WordPress, by default, will not show any errors/warnings that are generated by PHP. This is a good practice for sites that are in production, but if you’re developing then you want this enabled. To enable debugging, simply change this line in the wp-config.php file, which is found in the root directory of WordPress:
Simply change the constant WP_DEBUG to true, and then save the file. Any errors or warnings hidden will now be displayed, which is what we want.
The Loop and the Query
The query is a system that gathers which posts to show on the page, and the loop is what goes through each post and displays them accordingly. On your homepage, the query will look for the most recent posts and grab the newest 10 posts. On a category page, the query will look for the 10 most recent posts from the given category. You can also modify the query and use it for what you need to get done.
1 ) < echo ''; > the_post(); get_template_part( 'template-parts/content', get_post_type() ); > > elseif ( is_search() ) < ?> __( 'search again', 'twentytwenty' ) ); ?>
The code starts by checking if the function “have_posts()” has any data to loop through. If it does, then it sets a variable $i to 0. It then runs a while loop, which is a loop that will run as long as the condition is true (i.e. has data to return). It first increments the variable $i by 1 “$i++;”, then it runs a conditional statement checking if the variable $i is greater than 1. If so, it displays some HTML to separate the post. After this, obtain the post with the function, “the_posts()” and displays the content of the post with the function, “get_template_part()”.
If this conditional is not met, then it uses the “is_search” function to determine whether or not it was a search. If it was, then it simply renders some HTML and runs a “get_search_form()” function that asks the user to search again.
Hooks
WordPress gives developers the opportunity to modify core functions. However, you do not want to modify core files. This is rule #1 of development, and it is simply bad practice. It can sometimes be difficult depending on the project you are working on, but this is where hooks come in to play. WordPress is all about hooks, and it has two primary hooks that are used for development. These two hooks are action hooks and filter hooks. Action hooks allow you to add customized code, and filter hooks allow you to modify data before it is used.
Let’s customize the wp_footer to add our own code before the closing body tag of the theme. If you open the theme’s functions.php you can add the following code:
function ithemes_footer_code() < ?>This is the footer.
add_action( 'wp_footer', 'ithemes_footer_code' ); // ---- END // ---- BEGIN if ( ! function_exists( 'ithemes_custom_length_excerpt' ) ) < function ithemes_custom_length_excerpt( $length ) < return 50; >> add_filter( 'excerpt_length', 'ithemes_custom_length_excerpt', 999 );
This code creates a function “ithemes_footer_code”, which is used as a callback function used for the “add_action()” function. This function contains the code that we would like to perform on the wp_footer. For add_action’s first parameter, we call ‘wp_footer’ which indicates that this code will be run on the footer. The second parameter is the callback function (the function we just made), and it will run the custom code. This is an example of an action hook.
Let’s modify the custom length of a post excerpt. By default, WordPress will display 57 characters but we can modify the number of characters to be more or less:
if ( ! function_exists( 'ithemes_custom_length_excerpt' ) ) < function ithemes_custom_length_excerpt( $length ) < return 50; >> add_filter( 'excerpt_length', 'ithemes_custom_length_excerpt', 999 );
We first check to see if the function we’re creating already exists by writing a conditional for the “function_exists()” function. If it doesn’t exist, then we create our function and it takes $length as an argument. We then return the value of 50. After this, we run the “add_filter()” function which can hold 4 parameters, but we’re only using three. This is an example of the filter hook.
Wrapping Up: WordPress PHP
As you can see, just learning the basics of WordPress PHP and how PHP is implemented into the WordPress codebase gives you a better understanding of how your site is actually running behind the scenes. Watch a few of our video tutorials here on PHP: Syntax, the Loop in WordPress and WordPress template tags.
Each week, the team at iThemes team publishes new WordPress tutorials and resources, including the Weekly WordPress Vulnerability Report. Since 2008, iThemes has been dedicated to helping you build, maintain, and secure WordPress sites for yourself or for clients. Our mission? Make People’s Lives Awesome.
What is: PHP
PHP is a programming and scripting language to create dynamic interactive websites. WordPress is written using PHP as the scripting language. Just like WordPress, PHP is also open source.
PHP is a server side language, which means that it runs on your web hosting server. Whenever someone visits your website, their browser contacts your server to request the page. The PHP code runs on the server, and generates an HTML page to send to the visitor. The visitor then sees the HTML page in their browser. They can’t see the PHP script because it’s only on the hosting server.
How Does WordPress Use PHP?
PHP code is what makes WordPress work. A WordPress website stores all its data in a MySQL database. This data includes everything from your blog name and blog post content, to your plugin settings and user profile information.
PHP’s job is to get specific information from the database, and piece it together into an HTML web page. For more details on that process, you can see our guide on how WordPress works behind the scenes.
PHP code is contained in files that end in the .php extension.
If you have ever opened a WordPress zip file, you’ll see that most of the files are PHP files.
For example, in the screenshot below, you’ll notice file names like wp-config.php, index.php, and lots more. Each of these files contains the code used to perform different actions in WordPress.
A WordPress theme folder looks very similar. You’ll find files like sidebar.php, header.php, and others. Each of these files makes up a theme’s template hierarchy and displays the different sections of your website.
PHP code needs to be wrapped inside the php opening tag . PHP files can also contain HTML code.
Each PHP tag can retrieve a bit of information from your database. In this example, you can see the PHP code to display your blog name, which is then wrapped in HTML title tags.
How Does PHP Work?
If you’re looking around on the web for information about PHP, you’ll see it’s a “server-side” scripting language. Unfortunately, that short definition isn’t helpful for beginners.
The term “server-side” just means that all those PHP files in WordPress do their work on your web hosting server. So, in simple terms, when someone goes to your website, WordPress accesses the PHP files to get your database information (like your blog post content) to show it to your visitor.
That sounds simple enough. However, there’s a little problem. Web browsers don’t read PHP. They read HTML.
HTML is a type of scripting language used to create website pages. We refer to HTML as a “client-side” scripting language.
Browsers like Chrome, Firefox, and others are called “clients.” Here, a “client-side” language simply means that web browsers do the work of turning HTML code into what you see on your browser window.
So, if browsers only read HTML and your WordPress website is created in PHP, how can people see your website?
Let’s take a moment and see how the PHP code in WordPress works with HTML first, step by step:
- First, someone types in your web address in their browser, or they click on a link.
- This sends a request to your web hosting server.
- Your server then sends the HTML code over the Internet back to their browser.
- Their browser takes that HTML code and translates it into a web page.
However, when you visit a website created with PHP, like WordPress, an additional step takes place.
Before step 3, the server has to run PHP code to create the HTML page, before it can send it to the browser.
WordPress PHP Versions
Just like other programming languages, there are several versions of PHP that you can use. Some versions like PHP 5.5, 7.0, and 7.1 are no longer receiving active support.
As a WordPress website owner, you should use the latest version of PHP. We say this because each new version of PHP fixes security issues and will help to increase your website speed.
Since PHP is such a popular language to make websites, it’s also a target for hackers. So, having the latest version will provide you with the security features and fixes not found in older versions.
We recommend that you use either Bluehost, SiteGround, or WP Engine as your web host because they use the most up-to-date versions of PHP with their WordPress web hosting packages.
Do WordPress Users Need to Know PHP?
WordPress users don’t need to learn PHP to use, operate, or manage a WordPress website. WordPress already has the PHP files you need, and so do themes and plugins so that you can use WordPress with no PHP coding skills.
Most WordPress users never need to know how to code in PHP, but if you’re interested in developing your own plugins, themes, or making advanced customizations to WordPress websites, then you’ll need to learn to use PHP.
We hope this article helped you learn more about PHP. You may also want to see our Additional Reading list below for related articles on useful WordPress tips, tricks, and ideas.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.