• We just launched and are currently in beta. Join us as we build and grow the community.

How to Dynamically Change Meta Tags in every Pages using PHP

snoogy

Social Collaboration Coordinator
S Rep
0
0
0
Rep
0
S Vouches
0
0
0
Vouches
0
Posts
176
Likes
59
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Getting Started

For example, you have this simple meta data in your header.php template.

  1. <!DOCTYPE html>
  2. <html

    >
  3. <head

    >
  4. <meta

    charset

    =

    "utf-8"

    >
  5. <meta

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1.0"

    >
  6. <meta

    name

    =

    "url"

    content

    =

    "Your Site Url here"

    >
  7. <meta

    name

    =

    "description"

    content

    =

    "Your Site description here"

    >
  8. <meta

    name

    =

    "keywords"

    content

    =

    "Your set of keywords for the page "

    >
  9. <meta

    name

    =

    "author"

    content

    =

    "John Doe"

    >
  10. ................................
  11. <title

    >

    How to Dynamically Change Meta Tags in every Pages using PHP</

    title

    >
  12. <!-- css and scripts here -->
  13. </

    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 :)

 

452,292

323,526

323,535

Top