How to Dynamically Change Meta Tags in every Pages using PHP

Getting Started

For example, you have this simple meta data in your header.php template.
  1. <!DOCTYPE html>
  2.         <meta charset="utf-8">
  3.         <meta name="viewport" content="width=device-width, initial-scale=1.0">
  4.         <meta name="url" content="Your Site Url here">
  5.         <meta name="description" content="Your Site description here">
  6.         <meta name="keywords" content="Your set of keywords for the page ">
  7.         <meta name="author" content="John Doe">
  8.         ................................
  9.         <title>How to Dynamically Change Meta Tags in every Pages using PHP</title>
  10.         <!-- css and scripts here -->
  11. </head>

Code:

In order to create a unique meta data for another page and reuse your header.php template, do the ff. 1. Assign variables for your meta tag in your header.php. Example:
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.         <meta charset="utf-8">
  5.         <meta name="description" content="<?php echo $description; ?>">
  6.         ............................
  7.         <title>How to Dynamically Change Meta Tags in every Pages using PHP</title>
  8.         <!-- css and scripts here -->
  9. </head>
2. Edit the value of the variable in each landing page by declaring the variable first before declaring your header.php. Example:
  1. <?php
  2.           $description = "Your site description here";
  3.           include 'header.php';
  4. ?>
You should now be able to create unique meta tags for your pages. That ends this tutorial. Happy Coding :)

Add new comment