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

3.1 Create a PHP File

In this lesson, we'll create our first PHP file. I'll show you how to create a PHP file and hook it up with WordPress.

Related Links

3.1 Create a PHP File

Hello, and welcome back to this Tuts+ course on coding PHP in WordPress. In this part of the course, I'm gonna show you how to create a PHP file within your theme and add some PHP to it. So let's get started. So here's my theme with all of the template files in it. And I'm gonna create a new page template file. And this gives me an alternative way of displaying some of the pages in my site. Don't worry too much about the specifics of what type of file we're creating now and what it does. The important learning point here is creating a PHP file and adding code to it. So let's start by creating a new file. And obviously, the way you do that will be different depending on which code editor you're using. I'm using Coda for Mac. So here's my file, and I will give it a name. Demo-page.php. And you see Coda gives it that little flag to show that it's a PHP file. Now, at the moment, it's not gonna operate as a PHP file. We need to add an opening PHP tag to it. But we don't need to add a closing tag. WordPress doesn't require you to do that. So let's start by adding some code to it. Now, because this is a page template file, I need to add some commented out text at the top to tell WordPress that it is a template file and what it's called. So that sets it up as a page template file. Now, the first thing I need to do is use a template tag within WordPress to fetch the contents of the header.php file. And I do that by typing get_header. Now notice that whenever you use a function in PHP, you always put those braces after it, followed by a semicolon. The braces indicate that it's a function, and that's also somewhere where you'd put any parameters that you needed to within there. Here I don't, so I just put get_header. Now, the next thing to do is to add a loop. And again, I'm using a template tag there, so I put those braces after it. I've got two sets of braces, one outside the have_posts statement, and the other immediately after have_posts because of that being a template tag. In fact, I need to start that with if have_posts. So the end of my line, I put a semicolon, but I put colons in between those different functions. So here it's checking if there are posts, and then while there are posts, get the post. Now the next thing to do is to add some template tags and some HTML to output the content of the page. But I'm gonna do that in the next passed the_post because that will be about adding HTML. So I'll finish things off by putting endwhile, And endif. So that ends the while and the if. And note that I've got those the other way around because I need to end my while first because that came last. And so that's nested within the if. Now, you could alternatively write it like this. Now, if all you had was one line in each case, you wouldn't actually put all these line spaces in. But I'm doing that so you can see what I'm doing. That actually fulfills exactly the same function as this here. And sometimes it can be quite useful, particularly using if have_posts. Because if there are posts, you might want to output some content other than just what's in the loop of the post content itself. You might want to output on a search page, for example, a heading for that. And you can also do if, so we've got if have_posts here, and if Anthony posts, I can do an else statement. So here, I would put in what happens if there are no posts, not of there are no posts. And you can see another thing I'm doing here, that's another way of writing comments. So multiple line comments in PHP have the slash and then the asterisks. And you can just use one asterisk, or you can use as many as you want. Because often what I do is this because it makes it really, really obvious where I've got a new block of code. So you need to make sure that the slashes are at the beginning and at the end. Type that up. So for example, in my functions file, you can see that I've done that. So each function has got a big section of commented out text here that's nice and obvious because of all those asterisks that I've used. So let's go back to our demo page. And this is a single line comment. And you can put that after sim.php if you want. So you can see my PHP works here. I'm echoing something out, which is a way of within PHP adding some HTML. So I could type in, para, there. Or I can add it outside the PHP tags, which I'll show you in more detail on the next part of the course. So that's how we start adding some PHP to our file. Let's add a couple more. So I'll add get_sidebar, And get_footer. Now, within a theme template file, those will fetch the sidebar.php and the footer.php files. And the reason that we use that is to make sure we only have to code those once in our theme. We don't have to keep coding the header, the sidebar, and the footer in every theme template file. So this is the bare bones of a PHP file within our theme. So the main elements that you need are your opening PHP tags up here, and then your PHP template tags and functions, and sometimes conditional statements as well, coded correctly using the coding standards. And you can see here I've also used indentation. So I'm gonna stick with this version here because that demonstrates the indentation. I'll remove this one because we don't want two loops, and I'll save that. And then in the next part of the course, I'll show you how to add some HTML to your file. See you next time and thanks for watching.

Back to the top