The header of each page in any website must be unique, this is one of the most important aspects of SEO, but when you’re running a big site with hundreds of articles having a unique header for every page will be impossible without a dynamic header.
I would like to share a snippet I sometimes use to accomplish this task. The function consists of the HTML tags we use in every page such as the meta, title, and favicon tag. Important: Notice that I do not close the
tag, this is because some pages use different styles or scripts. Feel free to modify the function.Function Description
We begin by creating a class called ” Page “, we’ll save the file as ‘PageClass.php’
class Page
{
// the function code will go here
}
The function takes an array called ” headerParams ” for the parameter. This parameter consists of the following keys; “title”,”keywords” and “description”.
This is the class and function put together.
class Page
{
// a function with common header tags
function header($headerParams)
{
echo'
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-language" content="en" />
<meta name="description" content="'.$headerParams['description'].'" />
<meta name="keywords" content="'.$headerParams['keywords'].'" />
<title>'.$headerParams['title'].'</title>
<link rel="shortcut icon" href="favicon.ico" />
';
}
}
The keys will be given values in your page as follows:
$header['title']='welcome WebHole'; //remember that keywords are separated with comas $header['keywords']='seo, web development, free code'; $header['description']='We are a website built to help web developers. Find free code, reviews and news';
Let’s put it all together
// include the class we created include 'PageClass.php'; // assign values to the header array $header['title']='welcome WebHole'; // remember that keywords are separated with comas $header['keywords']='seo, web development, free code'; $header['description']='We are a website built to help web developers. Find free code, reviews and news'; // create a Page object $Page=new Page(); // out put the header: // we pass the $header array to the function header of the object Page // the result will be the header code inside the header() function $Page->header($header);
I hope you found this script useful.