Navigation Made Easy With PHP Includes
Today’s quick tip is all about saving time on menial tasks like updating your website header and footer with new links, graphics, etc. by using PHP includes. You can then use this time on money making activities! Of course, if you use WordPress for your sites (which in many cases I highly recommend) this doesn’t apply, but for static sites, common header and footer files are lifesavers.
This is especially important if you are having a site built for you. Demanding that it be built using PHP includes will save you time and money on updates in the future.
Normally if you wanted to change your header image, you would then need to change not only one page but depending on how big your site is, you might have to make changes to hundreds, even thousands of pages.
This is definitely not something fun, nor it is a necessary use of your time. With PHP includes, you can save yourself hours, even days of your time.
What Exactly Is a PHP Include?
This is a method of including one PHP file inside another PHP file, which results in specific elements of your website being consistent on all pages. This means you can update one file, and all the other pages that include it will be updated also.
What’s Good About Using It?
You save a huge amount of time and frustration each time you update. This strategy only works when pages on your website shares common elements. For instance, if you want a header or footer to display the same on some or all pages of your site, using PHP includes is a smart move.
You could also use this for banner or Adsense ads you want to appear on all pages of your website. Then anytime you want to rotate banners or if you want to stop using Adsense, all you need to do is edit one file to implement the change site wide.
PHP includes are also powerful for navigation menus. For sites that have regular changes to the menu, using PHP includes can really be useful.
Navigation Made Easy With PHP Includes!
To set up your navigation in the most painless way possible follow these quick steps:
- Check to see if your website hosting server supports and allows PHP. If it doesn’t, then you should seriously consider getting a new host.
- You will need to know how to read and edit HTML code.
Step One – Navigation File Creation
- Open one of the pages that has the navigation on it, let’s assume this is page.html.
- Viewing the source code of the page, copy and paste the piece of code that determines the navigation into a new HTML file.
- Make sure all the paths used in the navigation are correct. This is especially important if your website contains many directories.
- Save this brand new file as “navigation.php”. Save it in the same directory as the pages that will be using the navigation.
Step Two – Put The Navigation Into Your Page
All the code will be in navigation.php, so you will need to get rid of the navigation code from first file page.html. Warning! Make sure you delete only the information that you copied into navigation.php. Replace the navigation code in page.html with:
<?php include ("navigation.php"); ?>
Then save the original page as page.php (page.html becomes page.php, make sure that your links in the navigation reflect this!)
Step Three – Test It
Upload your page and test to see if it works. Make sure both files are uploaded and always check that you’re not overwriting anything that you shouldn’t. Also clear your cache to ensure that you are seeing the updated version of your site. If you have done this correctly, you probably will not notice anything different. All that has happened is that the navigation is now being controlled by one file. This test will confirm this.
Step Four – Repeat For All Other Pages
The navigation on all the other pages will need to be deleted and replaced with the
<?php include ("navigation.php"); ?>code. All paths must be relative to the document.
Step Five – Use PHP Includes For Any Other Elements You Want Greater Control Over
As mentioned earlier, any element you want to display across all your pages should be controlled by PHP includes as this makes it far easier to update in the future.
Any questions? Let me know in the comments below.