7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

2.1 Why WordPress Uses PHP

What is PHP? In this lesson, we'll take a look at the PHP WordPress programming language and why it's used for database-driven websites.

Related Links

2.1 Why WordPress Uses PHP

Hello, and welcome back to this Tuts+ course on PHP and WordPress. In this part of the course I'm gonna show you what PHP is and why it's used in WordPress. So let's start with what PHP is. PHP is a programming language used in websites. Which is pretty obvious really, seeing as we're talking about using PHP to build websites with WordPress. But what's specific about PHP is it's what's called a server-side language. Now, websites use two types of language. One is a client side language, and another would be a server-side language. And the difference is that HTML and other server-side languages, including JavaScript, do their work on the computer of the person who's looking at your website, so on the client computer. Whereas PHP does its work in the server, so where your website is hosted. And the reason WordPress uses PHP is because that makes it possible to interact with the database, and to fetch data. So WordPress uses PHP to fetch things from the database and then to output them as HTML. And the various PHP tags and functions provided by WordPress are what enable this to happen on your page. So here you can see php.net, which is a website that teaches you all about PHP. And there's a huge amount of detail in here, which I recommend looking at once you've had a chance to tinker with PHP and have a look at the more WordPress specific guidance to it. But this is a good place to get to know how PHP itself, disconnected from WordPress, works. Because what you'll find is that there are a lot of functions that you use in PHP with WordPress but aren't actually PHP functions as such, but they are functions that are written specifically for WordPress. So here is another page that might be quite useful, this defines PHP in Wikipedia. So here, for example, we have what PHP stands for. Now, I very much doubt that many PHP programmers would know this, cuz this is a pretty obscure fact, but it stands for personal home page forms interpreter. So personal home page. So remember when we used to talk about web pages, not websites. Well, you might not remember, depending on how long you've been doing web development. But there used to be a time when you had a web page. And every single page you created was coded completely separately using static HTML. Now, combining PHP and a database with that HTML gives you much more flexibility. Because with WordPress you don't have to create each page in your site separately using HTML. What you can do is create templates and plug-ins, and those will interact with your database to produce as many pages as are needed in your website. So for example, on a WordPress site you would have a web page for every single static page in your site, and also for every single post, and for every single archive page. And that's generated dynamically by a bunch of PHP tags that are used within your theme files. This can all be a little bit confusing, so I'm gonna demonstrate it by use of my own website. So here on my site, I'm on my home page, I've got a link to my latest post on WordPress, and I'll click through to that. So here is some mock-up, which produces a web page about getting started with WordPress. And you can see there's text in there, and there's a lists, and so forth. Now, in a traditional HTML site that somebody might have coded 10, 15 years ago, before WordPress was even invented, and before CMSs, or content management systems like WordPress became popular, you would have coded each of these with some static HTML. And let me show you the HTML that is generated by this page, or by the template file in order to create this page. So you can see here, let's go up, I've got the body, which is the whole page. And then there's a header, there's a banner, there's my menu. And here, this part here is a div called main. And within that I've got my content, and I've got here, entry content. So you can see within here I've got a whole bunch of p tags, for example, for paragraphs, and a ul tag for an unordered list. Now, it would be really laborious if you had to actually generate all of this using static HTML for each of your pages, but the beauty with PHP is that you don’t. So let me show you the template file in WordPress that generates this mock-up. So here is my template file in my theme for a single post, and that’s called single.php, as you can see. Now, the WordPress template hierarchy is used to determine which template file is used for a given page in your theme. And I'm not gonna go into that in much detail here, but I will provide a link to a resource in the notes for this course, in case you need that. But here I've got the single.php file, which is generating all of the content for that page here that we are looking at. You can see here, this is the part that we're dealing with the content, section class="entry-content". So here, section class="entry-content", and within that in my output HTML there's p tags, ul, li, and so forth. There's a div in there as well, that's generated by a plugin, by Jetpack. Now, none of that is here. All there is instead is this tag here. And this is a template tag, which is a function within WordPress, but it's provided for you to use within your theme template files in order to access data from the database. So what this does is it pulls the content of this particular post from the database and outputs it. So if I go back to my browser and I open the post editing screen in my admin, you can see here is the content that it's fetching from the database. So I add that via the admin screens in WordPress, that goes into the database, and then it's output using this, the content tag. And you can see there are other template tags here as well. So for example, post_class, that's a template tag. And what that does is generate a bunch of classes for the CSS. And here another one, the_ID, which outputs the ID of the current post. So let's take a look at that in the generated mock-up. So if I go back up, I've got article id="post-3129", and that's been fetched with this template tag here. And then class=, and there's a whole list of classes here. Because what this does is it generates a list of classes for your post based on what post type it is, what it states, its format, what tags and categories its got, and much more. But as you can see, by typing in just this short template tag post_class, I'm able to generate all this. And that's the beauty of using PHP. Because PHP will interrogate the database, it'll find out what all those attributes are, what tags, categories, post types and so forth this post is, and using that template tag, it'll output that in your html. So that's why PHP is so helpful with WordPress, because it is your link between your web page and the database. So what happens when somebody views that page is that WordPress, on the server-side, where your site is kept, rather than where the visitor is, on the server-side, it will interact with the database and it will output this page. Now, there's one disadvantage with that. Because PHP is a server-side language it means that you can't dynamically update the page based on any changes from the database. And that's one of the reasons that the REST API has been developed. Because the REST API uses JavaScript in order to interact with the database. And JavaScript is a client-side language, so that means that it can be constantly be doing things, refreshing, and carrying out tasks in the user's browser. So you can use it to create an app-like website. Now, we're not gonna worry about that too much here because this course is about PHP. And if you're going to learn to code with WordPress, PHP is where you need to start. I wouldn't recommend delving into the REST API until you've got familiar with PHP and template tags, for example. So just to go back to my example with the database, here's my database for my website. So what it's doing is it's going to this wup_posts table, it's finding that specific post. I'm not gonna worry about finding that now, because there are so many posts in there. And then it's outputting this HTML that is within the database for the post content on my page. It's back here on my page, you see? So that's how PHP is used in WordPress, and that's why it's so useful. In the next part of this course I'm gonna move on to having a look at the PHP coding standards. And that's a set of rules as to how you should use PHP within WordPress in order to write quality code that's consistent with other code that might be provided by other themes and plug-ins that you're using in your site. See you next time and thanks for watching.

Back to the top