Upcoming LunaMetrics Events
San Francisco, Apr 28-30 Los Angeles, May 12-16 New York City, May 19-23 Chicago, Jun 16-18

PHP SEO: Page-Level Titles, Meta Descriptions, & More

When it comes to updating title tags, meta descriptions, canonical link elements, etc. on a page-by-page basis, we often rely on the power of the client’s CMS. Whether we’re using WordPress plugins or Drupal modules to get the job done, we generally have a process that is efficient and feasible. No tinkering with template files. No scouring the web for alternative solutions. Simple implementation – just the way we like it.

Content Management Systems with built in SEO utilities are great. What happens, though, when you’re tasked with implementing all of the pertinent HTML elements page-by-page on a PHP based website with a static <head>? Let’s dive right in.

1. Make that <head> dynamic!

In most cases, each static PHP file, be it index.php, contact.php, what have you, will reference the same header.php file via an include statement:

<?php include('header.php'); ?>

The include statement tells the server that any code within header.php should also be included in the file being requested. This way, we don’t have to write a lot of the same HTML on every content page. Instead, we have this one static file from which we can pull the necessary code. Note that the header.php file doesn’t necessarily contain only the HTML <head>. Generally, it will include any code that is reusable at the top of the HTML document throughout the website (including the logo, navigation, banner, etc.). Let’s look at an example of code we might find in header.php:

<html lang="en">
<head>
<title>Page Title Here</title>
<meta name="description" content="Meta description goes here.">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
. . .

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.

<html lang="en">
<head>
<title><?php echo $pageTitle; ?></title>
<meta name="description" content="<?php echo $pageDescription; ?>">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
. . .

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 <head>. If the condition within the parentheses is met, then the code within the brackets ({ ... }) is executed.

<html lang="en">
<head>
<title><?php echo $pageTitle; ?></title>
<meta name="description" content="<?php echo $pageDescription; ?>">
<?php
// If canonical URL is specified, include canonical link element
if($pageCanonical)
{
echo '
<link rel="canonical" href="' . $pageCanonical . '">';
}
// If meta robots content is specified, include robots meta tag
if($pageRobots)
{
echo '
<meta name="robots" content="' . $pageRobots . '">';
}
?>

<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
. . .

Now that we have ourselves a <head> 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).


<?php

// Define variables for SEO
$pageTitle = 'Orange Widgets | The Best Orange Widgets';
$pageDescription = 'Visit AwesomeOrangeWidgets.com to find the best orange widgets in all the lands (Oz included).';
$pageCanonical = 'http://www.awesomeorangewidgets.com/';
// We don't want the search engines to see our website just yet
$pageRobots = 'noindex,nofollow';

// Include header file
include('header.php');

. . .

?>

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.

<html lang="en">
<head>
<title>Orange Widgets | The Best Orange Widgets</title>
<meta name="description" content="Visit AwesomeOrangeWidgets.com to find the best orange widgets in all the lands (Oz included).">
<link rel="canonical" href="http://www.awesomeorangewidgets.com/">
<meta name="robots" content="noindex,nofollow">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="wrapper">
. . .

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 <head> 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!

http://www.lunametrics.com/blog/2013/02/19/php-seo/

12 Responses to “PHP SEO: Page-Level Titles, Meta Descriptions, & More”

Antti Nylund says:

Dropping my 2 cents

Don’t know if this is the most efficient solution server-wise, but if the site is less than 100 pages I favor switch clause.

switch ($pageurl) {
case ‘homepage.php’:
$title = “”;
$metadescription = “”;
$canonicalURL = “”;
$whateverYouWantToDefine = “”;
break;
case ‘someotherpage.php’:
$…
break;
default:
$title = “My default title”;

}

For the sake of upkeep you might want to consider by default connect page main heading (h1) and title and small insert with meta description.

Travis Loncar says:

Thanks for the example, Antti. My first reaction was that a long switch statement could get a bit confusing (if the URLs aren’t especially indicative of the page content). However, with a smaller site, I could see a commenting convention making this easier to work with. (In fact, I guess the same issue exists when editing individual page files.) This is where mapping content to URLs in a spreadsheet pays off.

Nice text – all in one place. I think another good practice is to handle some constant parts of title and/or description. For example:

$pageTitleCommonSuffix = ‘The Best Orange Widgets’;
$pageTitle = ‘Orange Widgets’ . ‘|’ . $pageTitleSuffix;

Travis Loncar says:

Thanks, Mikolaj. Good point. If you find that it is important to brand your titles, defining the common suffix in the header could be a time-saver. I think it’s important to be mindful of character length when you’re doing this. It’s easy to forget that the suffix is being appended.

I was just talking about using conditional formatting on static PHP headers. Your code came out 95% the same as mine. Nice work!

If i use the same page with different seo friendly urls changing title and meta description accordingly will it help seo ( Provided i put rel=canonical to the primary page)

Wiles, E says:

I am rather new to website administration and I need a few clarifications.

I am trying to make my website easier to manage and your content is very interesting and applicable to what I am looking for.

Will removing this line make the page visible or do you have to do something else to it?
$pageRobots = ‘noindex,nofollow’;

According to google support (link below), the canonical information is for SEO, preventing numerous extremely similar hits (when pages are very similar). Am I getting this right? I am guessing that ‘ . $pageCanonical . ‘ (in the header) looks for the second entry on the given page? I’m guessing that if I do not have multiple extremely similar pages, I do not need these entries to put SEO information within the page?

Header entry:
echo ”;

page entry:
$pageCanonical = ‘http://www.awesomeorangewidgets.com/’;

Google info on rel=”canonical”
http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394

Tahir Raza says:

Nice,Was Working on a project and was looking for something similar.

Venkatesh says:

Nice tutorial Travis. This article helped me very much. Thank you

agus says:

thank you..
i have been setting my new site and need this treatment

Hello Travis Loncar,

I tried your code and it works great for variables.

How do you do HTML blocks like say I wanted to include a div or something a little bigger than just a variable?

Thanks again.

Boni Satani says:

Hi Travis,

I tried the code you have provided but its not working.

Dont know why. Little help appreciated