For years now I’ve been using a technique that most programmers rely on for bringing content to their website. The PHP include functions. You simply add:
<?php
include(‘filename.php’);
?>
To your site, when you need to introduce a new element that would be present in several locations. For years this technique served me well, and made it quick to add in headers. All of the information was the same across the board, and for much larger sites, made little updates easier. I remember one of my first jobs in the industry required a link needing to be removed from the main menu. I ended up going into the site and changing over 50 pages and thinking to myself “There must be an easier way”. Shortly thereafter, I learned the power of the PHP include. Fast forward a few years later, and I get a call from one of my clients. The PHP include was great, but the SEO team he’d taken his website to had an issue with the way my site had been built. They wanted each page to have custom keywords, descriptions, and titles to each individual page. My client informed me he’d still pay me for my services, and that he’d still work with me in the future but I’d need to find a solution for this issue.
I looked at the code and thought to myself once again “There must be an easier way to do this”. It was at this point did I decide to try something new. A PHP class that would house all the information I needed. It would not just have the header information, but carry my template files such as the footer information, and navigation information too.
So I went to task and created a SiteManager.php class that would allow me to pass the variables I required in order to get the meta data set up. Once I got to work on it, it was pretty easy to do.
<?php
require_once(‘functions/SiteManager.php’);
$site = new SiteManager();
?>
The first thing I did was call the function I’d be creating into the file I needed. By using the require_once function, I insure that my class is loaded into the document. I also decided set up $site as the variable to manage all of the functions in my class.
<?php
require_once(‘functions/SiteManager.php’);
$site = new SiteManager();
// This defines the variables that will be needed for the header function.
$keywords = “Andrew Kelly, Andrew, Kelly, Pristine Pixels, Web Design, Web Development, PHP Programming”;
$description = “This is a website for Andrew Kelly, a web designer and developer just outside of the GTA”;
$title = “Andrew Kelly – Web Design and Development”;
?>
Next I defined the variables I needed in order to pass to my PHP function. These variables would be placed on every page and would have the ability to be changed depending on the page content to best reflect what the page is talking about.
<?php
require_once(‘functions/SiteManager.php’);
$site = new SiteManager();
// This defines the variables that will be needed for the header function.
$keywords = “Andrew Kelly, Andrew, Kelly, Pristine Pixels, Web Design, Web Development, PHP Programming”;
$description = “This is a website for Andrew Kelly, a web designer and developer just outside of the GTA”;
$title = “Andrew Kelly - Web Design and Development”;
// Call the header function, and pass the variables required.
echo $site->header($keywords, $description, $title);
?>
I then call my header function, and pass the variables I’ve set up. Once I’ve done this, everything I need to do for the page with the content is done. All of the information has been set up. Next I would create the PHP class that would handle all of the data.
<?php
class SiteManager {
function __construct() {
// Here you would set anything might be needed in all the functions. In this case, nothing is required to go here.
}
}
?>
The next step is to create the class. First I set the __construct() function to activate the function. You don’t need to set anything up inside this function, but the function itself is always required.
<?php
class SiteManager {
function __construct() {
// Here you would set anything might be needed in all the functions. In this case, nothing is required to go here.
}
function header($keywords, $description, $title) {
$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"';
$html .= '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
$html .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
$html .= '<head>';
$html .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>';
$html .= '<meta name="description" content="empty />';
$html .= '<meta name="keywords" content="empty" /> ';
$html .= '<title>untitled</title>';
$html .= '</head>';
return $html;
}
}
?>
I proceeded to add my header content into one large variable called $html. This way way when I return the variable all of my information is showcased where I requested it to be. When you use an echo in a function, the text you’re using can jump around and may display above other content. By using a variable to house the information, you can ensure that everything loads correctly when requested.
<?php
class SiteManager {
function __construct() {
// Here you would set anything might be needed in all the functions. In this case, nothing is required to go here.
}
function header($keywords, $description, $title) {
$html = ‘<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”’;
$html .= ‘“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>’;
$html .= ‘<html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>’;
$html .= ‘<head>’;
$html .= ‘<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8”/>’;
$html .= ‘<meta name=”description” content= “’.$description.’”/>’;
$html .= ‘<meta name=”keywords” content=”’.$keywords.’” /> ‘;
$html .= ‘<title>’.$title.’</title>’;
$html .= ‘</head>’;
return $html;
}
}
?>
Finally I break drop the variables in by inserting them into the php where required and everything needed is done.
By using this technique, you can still have all of the information stored in one location, but the php functions allow you to pass the variables required to make everything dynamic. The code is clean, still easy to read, and makes a lot of people happier.
Since I started using this approach I haven’t had any more issues with any SEO teams **knock on wood** and my clients are just as happy as ever. Hopefully this helps someone else out there struggling with PHP includes and SEO issues.