Writing Unique Metadata for Your PHP Website

Written by 9leafs

On February 17, 2019

So, you have planned to launch a website and it will probably have thousands of pages. That sounds great! Hopefully you are going to develop the website in PHP right? Because if you have decided to develop the website in plain HTML, it means you will have to go through all the web pages in case you might have to change something in the header section like – adding a new menu or changing the footer text.

This is the reason why most of us prefer to develop website in PHP as this enables us to create separate header and footer files and fetching those files in all web pages by using PHP’s popular include function.

But the main problem that most of us have to face is to write unique Metadata for each single PHP page. Since we will be using the same header and footer file against all web pages, all pages are going to have the same Title and Meta Description tag.

From SEO’s standpoint, this is just horrible. You just can’t let this happen. So, what you are going to do enable unique Title and Description tag for each PHP page?

Feeling clueless? Don’t worry as we are going to show you how you can do this easily by writing some simple PHP code –

First you will have to include the below line of code in the .header.php file –

<title><?php echo $pageTitle; ?></title> 
<meta name=”description” content=”<?php echo $pageDescription; ?>”/> 
<?php if($pageCanonical) {
echo ‘<link rel=”canonical” href=”‘ . $pageCanonical . ‘”>’;
} ?>

So, you have successfully created three variables –

  • Variable for Title Tag – $pageTitle
  • Variable for Meta Description – $pageDescription
  • Variable for Canonical – $pageCanonical

Now, all we have to do is to define these variables in all the pages where we have included the header.php file –

<?php
date_default_timezone_set(‘UTC’);
$pageTitle = “9Leafs ⇝Trusted Web Designing Company in India”; 
$pageDescription = “9Leafs is your trusted partner in web designing and development. We offer professional web designing services at affordable rates. Contact us to discuss.”; 
$pageCanonical = “http://www.9leafs.org/”;
include(‘header.php’); 
?>

In the above code, we have used the include function to invoke the header.php file and we have defined the all the three variables.

So, is not that super simple?

Now, whatever changes you will make on the header.php file will get reflected in those pages where you have fetched the header.php file.

You May Also Like…

Why WordPress is The Best Platform for Your Website

Why WordPress is The Best Platform for Your Website

Despite the immense popularity of Wordpress, there are some people who hesitate a lot while choosing a content management system for their business websites. They tend to look at Wordpress with suspicion. Some of them are even confronted with questions like these – Is...

How Much Does It Cost To Build A Website

How Much Does It Cost To Build A Website

It is great that you are planning to launch a website but you are also worried that you might have to fork out a huge amount of money. Launching a website does not have to be that costly affair at all. Here in this article, we are going to take into account several...

Download 10 Free Html and Web design Tips & Techniques Ebook

Download 10 Free Html and Web design Tips & Techniques Ebook

There is nothing cosier than cuddling up in bed with an interesting book on a rainy day. Nevertheless, more and more people are turning to e-books for the sake of practicality. Web designing requires a lot of creativity to generate the look and feel that will appeal...

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *