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 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 –

$pageTitle = “9Leafs ⇝Trusted Web Designing Company in Malta”; 
$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 = “https://www.9leafs.org/”;

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…

Tips for Landing Page Design

Tips for Landing Page Design

According to a survey, about 50% of the online purchase is through searches. Google being the top in search engines, accounts for about 83% of the total searches worldwide. Companies give great importance to conversions, so designing a perfect landing page is very...

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...


Submit a Comment

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