The problem with using PHP includes to make maintaining larger websites easier is it means sacrificing certain customisations (i.e. custom page headers) that would be possible with individual pages. As specific page titles are vital for relevant listings in search engines, not having them means sacrificing page rank too. Or so you might have thought! Fortunately there are a couple of methods to make customising page titles easy.
Method One – Derived from Filenames
The first method involves naming your pages descriptive names and deriving the individual subtitles from this.
Take a file, and name it based on the content. For example, I might save this tutorial as “custom_page_titles.php”. We can then take that name and, using PHP, turn it into something more useful for your title tag. To turn this into a ‘proper’ title we’ll need to strip out the extension (.php) and any special characters (in this case, underscores). Getting the page is easy with the SERVER superglobal, so let’s grab it and assign it to the $page variable:
$page = basename($_SERVER['SCRIPT_FILENAME']);
Now to clean it up! We’ll be using the str_replace() function to replace special characters and the redundant extension at the end:
$page = str_replace('_',' ',$page);
$page = str_replace('.php','',$page);
“$page” should now will now look like this: “custom page title”. You can tidy this up even further by capitalising the first letter of each word (for extra professionalism):
$page = ucfirst($page);
Altogether now, this looks like this:
<?php
$page = basename($_SERVER['SCRIPT_FILENAME']);
$page = str_replace('_',' ',$page);
$page = str_replace('.php','',$page);
$page = ucfirst($page);
?>
Place the above coding anywhere in your header include file, before your <title> tag. Now, to actually implement it we simply echo the $page variable after our main title (and a separating character), like so:
<title>
MAIN TITLE » <?php echo $page; ?>
</title>
Method Two – Setting a Variable
This second method is perhaps slightly quicker and probably ideal if you already have lots of pages (because changing page names breaks the web and is a bad idea!) This idea is the most simple of the two shown here.
Open the pages that you wish to have custom subtitles for, and before the header include is declared, add a variable called subtitle with your custom subtitle, like so:
<?php
$subtitle = "custom subtitle";
include('header.php');
?>
Now you need to open the header include, and add a bit of fancy stuff. First, we need to check if the custom subtitle is set — echoing the $subtitle variable before it is set will spring up some errors. To check, we need an if statement:
if (isset($subtitle)) {
echo $subtitle;
}
Pretty simple when you read it to yourself in “structured English”: if is set, echo subtitle. Of course, if you want a default subtitle for when a custom one is not set we need an if/else statement (although it’s not necessary):
if (isset($subtitle)) {
echo $subtitle;
} else {
echo "default subtitle";
}
Now we just need to stick this in with the title in the header include (I’ve compressed it a little to save space:
<title>
MAIN TITLE » <?php if (isset($subtitle)) { echo $subtitle; } ?>
</title>
Both simple, both good for your search engine ranking.




It seems, however, that if you use you first solution, then you will not be able to use punctuation in your page title. Am I correct?
#1 Love this site,
#2 Thank you for this. Works like a charm.
XOXO.
Hi, I know this is a very old post – but wondering how you get round the W3C page validation issues with your second technique. i.e. If I put the title tag into a PHP variable, when you validate the page you get a W3C error – because it cant see a tag in the element.
Does this matter?
Can I just safely ignore the validation test for this item?
Tim, if you put the ‘if’ statement in between , then it is in between tags, isn’t it?
Thanks…It really worked on my project….gr8 work…keep it up…
Hi there, I tried this, but the guy who built the site originally, didnt have a header.php he has some template.php file. and so It wont work. I am not sure what to do? The main one wher i put it in the main title works, But it wont echo the other pages having a different title it just puts it in normal text on the top of the page. Not in the title tab.
Absolutely great!!!!!!!!!!
Thanks you.
Thanks Jem for your great tutorial, it’s what I was looking for. :) Thanks there’re girls who geek.
Nice and clean tutorial. This made me look like a pro. Thanks.
6 years later and it’s still useful!
Thanks Jem, I used the second method.
I can’t thank you enough. This has been an enormous help!
Thank you so much, finally got the titles to work because of your help!
Thank you! Perfect!
The best solution to my problem. THANKS!!!
Wow! So clear and easy to follow. You should teach others how to explain things. This is great. Thanks a lot!
Dan
Thanks!!!
Worked great, Thanks a lot for the tutorial
Great work!!! and Great site
Just what i needed.. and i learned several things useful for other projects..
Thanks, works fine. Nice website.
This was just what I needed, thanks for posting!