PHP includes for Blogger
Before writing this post, I looked online to see if others have written about this same topic. This might as well be Michael Gray’s “Integrating Blogger Into Your Website: Part II”, which he never wrote — though he did write Part I.
Step 1: Switch Blogger files to PHP
First, you need to update your publishing settings so that Blogger publishes your posts in PHP, not HTML (thanks to DevDoctor):
- Set up your blog to publish via FTP (on Publishing tab)
- Change the ‘Blog Filename’ to index.php
- Change ‘Archive Filename’ to archive.php (on Archiving tab)
- Finally, delete the old index.html file from the blog directory (otherwise it will probably take precedence over the new index.php)
Once you’ve got Blogger pushing out PHP, you’re able to do PHP stuff with the site:
- Update the entire site at once – When I create a website, I always want to create the structure using PHP includes, so that I can set global site variables, and if I change it in one place, it changes across the whole website.
- Set active navigation – Using SSI includes, I used to have to include a different file for each blog section on the site so that I could up. Here, I just send a variable to the navigation.php file, which tells it what page is active.
Next, I will show how to include these files.
Step 2: Get the absolute path to the includes
Note: the most important part of this code is that it uses an absolute path, not a relative path.
To find the absolute path of the file you are trying to include, you can:
- Run the code below, and the error should show you the path in the error text itself, or;
- Can add this to a file in the same directory as the files you’re trying to include, then go to the file in your browser:
<?php echo realpath(dirname(__FILE__)); ?>
Step 3: Update your Blogger.com template
Once you have the absolute path for the file, you can add the includes to your Blogger template:
$pageID = 1; // You can tell these files what page you're on
Note: One of the items I was going to write about was passing some variables back and forth, but I’ve included only the $pageID variable. If you want to see more code, let me know in the comments below.
- Amp Alarms
- Split Rail Fence Company