1. Web Design
  2. WordPress
  3. WordPress Themes

Creating Your First PHP File for WordPress

Scroll to top

In this tutorial and screencast, I'll show you how to create a PHP file within a WordPress theme. I’ll also show you how to add some PHP code to the file.

This video is taken from my course Learn PHP for WordPress. You can watch the entire course for free here on Envato Tuts+.

WordPress Theme Files

I’ll start by locating the WordPress theme with all of the template files in it. I’ll then create a new page temple file. The purpose of this is to give an alternative way of displaying some of the pages in a site.

The WordPress theme files as shown in CodaThe WordPress theme files as shown in CodaThe WordPress theme files as shown in Coda
The WordPress theme files as shown in Coda

It’s not terribly important what type of file is being created nor what it does. The important thing is how to create a PHP file and add relevant code to it.

Create a New File

Start by creating a new file.

Creating a new file in CodaCreating a new file in CodaCreating a new file in Coda
Creating a new file in Coda

The way in which you’d do that may be different depending on what code editor you’re using. I’m using Coda for Mac.

Once the file has been created, give it a name—for example, demo-page.php.

Coda gives it a little flag to denote the fact that it’s a PHP file. But that alone does not mean it will operate as a PHP file—it needs some code. That means an opening PHP tag: <?php. There’s no need to add a closing PHP tag as WordPress does not require this.

Add Code to the File

Now we can add some code to the file.

Adding code to the fileAdding code to the fileAdding code to the file
Adding code to the file

This is a page template file. That means it requires some commented-out text at the top to tell WordPress what it is and what it's called.

The first thing to include is a template tag within WordPress to fetch the contents of the header.php file. That’s done by typing get_header

Whenever you use a function in PHP, you must always include the braces (brackets) after the function name. This is completed with a semicolon.

The braces indicate that it’s a function, and it’s also somewhere that would include any necessary parameters. No parameters are required in this example.

Add a Loop

Adding a loop in the codeAdding a loop in the codeAdding a loop in the code
Adding a loop in the code

Next, add a loop. Again we'll use a template tag.

if (have_posts() ) : while( have_posts() ) : the_post();

The have_posts template tag lets us know if there are posts to process in the loop. Our loop says, if there are posts to start with, run the the_post() function as long as there are posts remaining. 

To output the content to the page requires more template tags and some HTML. I’ll show you how to do that in another tutorial.

We finish off our loop using endwhile and endif.

Another way of writing this loop is by indenting the if and while statements on their own lines with { curly braces. This style is often easier to read, especially if you have to do more coding inside the loop.

1
if( have_posts() ) {
2
    while( have_posts() ) {
3
        the_post();
4
    }
5
}

There are lots of other things you could add to this loop. If there are posts, you might wish to output a header, for example, or a search bar.  You can also use an else statement to handle the case where there are no posts.

Adding Comments

Adding comments in the codeAdding comments in the codeAdding comments in the code
Adding comments in the code

Multiple line comments in PHP have the slash and then the asterisks. You may use just one asterisk or as many as you want.

It’s useful to do this where there is a new block of code. 

Ensure that the slashes are at the beginning and the end: /* and */. For example, in the functions file, each function has a big section of commented-out text and, with the number of asterisks used, it is very visible.

We can also add single-line PHP comments by starting the comment with //.

Single line PHP commentSingle line PHP commentSingle line PHP comment

So I've added an else statement to the loop, to handle the case when there are no posts. Then, I've used // to add a comment as a placeholder. Here I'm just using the comment as a reminder to myself, and to anyone reading the code, what is supposed to happen in the else statement.

Experimentation

Experimenting with the codeExperimenting with the codeExperimenting with the code
Experimenting with the code

To experiment with a couple more template tags, add get_sidebar and get_footer to the bottom of the file.

Within a theme template file, those comments fetch the sidebar.php and footer.php files. A reason to do this, instead of just adding the code for a sidebar right in your template, is to ensure those pieces of code only need to be written once in the theme. That way, if you change your sidebar, for example, you don't have to go back and update every single file in your theme!

With that, you have a bare-bones PHP file within the theme. The main elements required are the opening PHP tags, then the PHP template tags and functions. There may be conditional statements as well. As you can see, the code is indented for readability.

If you follow along with the course, in the next lesson, I’ll show you how to add some HTML to your PHP file.

The Best WordPress Themes and Plugins on Envato Market

Explore thousands of the best WordPress themes ever created on ThemeForest and leading WordPress plugins on CodeCanyon. Purchase these high-quality WordPress themes and plugins, and improve your website experience for you and your visitors. 

Here are a few of the best-selling and up-and-coming WordPress themes and plugins available for 2020.


Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Web Design tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.