Using PHP includes to easily edit your site’s static content!

Hey, in the following article I’ll show you how to add PHP includes to a static multi-page site, like a personal site or a portfolio, to help edit the site more easily.

For this article, I’ve presented a simple web page layout below. The template contains a header, a navigation, a place for the main content, a static sidebar, and a footer. If I were to add a link to the navigation, I would have to manually go through every page in the site and add a new list item.

However, if I switched to using PHP includes, I would only have to edit one file, which when edited automatically changes on every page. This helps when you need to change the link to a CSS file, add a couple of list items to a navigation bar, change your sidebar, etc.

So how do I do this? It may look a bit complicated but it’s actually only a few steps and you need to know absolutely no HTML or PHP. Here’s a nice little list for you:

  • First, you have to change all of your site files from the .htm or .html extension to the .php extension. This can be done on your local computer or on your server, using an FTP program.
  • Then, figure out what sections of the site are repeated and unchanged page-to-page. In the site template above they will be the header, the navigation, the sidebar, and the footer.
  • Next, create separate files for each of these sections, for example header.php or footer.php, and add the code for each one in the correct file.
  • Finally, take all of your old files and replace the previous sections of hard code with this line of PHP: <?php require('header.php');?> This line of PHP code includes the chosen PHP file into the HTML code. All you have to do is copy the code above and replace the “header.php” with the name of your specific includes.

Once you’re done, your pages’ code should now have a layout similar to this:

As an extra addition, I’ll show you how to hide/show/change certain sections depending on the URL that you’re located at.

Edit: I’ve added a nice little .zip file that shows you a super-simple example on how to use these PHP includes on your site.

You can edit the above code adding lines for each thing you want as a condition, just changing the “index.php” segment to whatever your page url is called (like about.php, etc.) or the required include (like sidebarcontent.php, etc.). Have fun!

Once you’re done, you’ll have a nice easily-editable site. Enjoy!