. . .
1
2
3
4
5
6
7
8
9
Page Title Here
. . .
This is a bit stripped-down, of course, but it’s serviceable. Notice, though, that our title tag and meta description have static text values. Even if we edit these to be a bit more descriptive of a particular page, we’ll be effectively applying the same title and meta description to every page on the website. Not good. Fortunately, we can make these dynamic values (unique to each page) by using PHP variables. We’ll use the echo construct to place the necessary page-level variables (which we haven’t yet created) in the right spots in header.php.
. . .
1
2
3
4
5
6
7
8
9
. . .
It’s safe to say that we’ll be implementing titles and meta descriptions on each page of the website. What about something like a canonical link element or meta robots markup, though? We might not want these on every page, but rather just select pages. To handle these elements, we’ll use a couple of conditional statements. If the canonical URL and/or robots content is defined for a given page, then we’ll include the element(s) in the
. If the condition within the parentheses is met, then the code within the brackets ({ ... }) is executed.
';
}
// If meta robots content is specified, include robots meta tag
if($pageRobots)
{
echo '';
}
?>
. . .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
';
}
// If meta robots content is specified, include robots meta tag
if($pageRobots)
{
echo '';
}
?>
. . .
Now that we have ourselves a
with elements that rely on page-defined variables, we’ve done most of the “hard” work. Let’s move on to defining these page-specific variables.
2. Define page-specific PHP variables
So, for each page on our site – all individual PHP files – we’ll need to define, at a minimum, two variables ($pageTitle and $pageDescription) before our include(header.php) statement. Ideally, we would write in some conditionals to catch cases where these variables aren’t defined; but for now, we’ll just be extra careful to define them on each page. We have the option of defining two additional variables ($pageCanonical and $pageRobots), as well. To define a variable, we use the syntax: $variable = "This is a string";. Let’s go ahead and assign values to all four of our variables for the home page. We’ll be working with the index.php file. (The topic of our site is Orange Widgets).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Simple enough, right? We’ve defined our title, meta description, canonical URL, and meta robots content with four PHP variables. Let’s see what the top of the source code looks like for index.php.
Orange Widgets | The Best Orange Widgets
. . .
1
2
3
4
5
6
7
8
9
10
11
Orange Widgets | The Best Orange Widgets
. . .
Not too shabby, eh? While it might seem like a pain to declare these variables on a page-by-page basis, it’s really not much different than using All in One SEO for WordPress or something comparable for another CMS. Once you’ve familiarized yourself with the concepts outlined above, you’ll find that editing the files directly isn’t as taxing a process as you might have thought. For those of you working on small PHP-based websites, I highly recommend implementing the dynamic
elements we’ve discussed. What takes minutes to implement could yield years’ of worth.
Any cool tips or tricks when it comes to doing SEO for PHP-based websites/applications? Share ’em in the comments below!