Dynamic Meta Tags with PHP
119   1  

Dynamic Meta Tags with PHP

  06 Nov 2017
  Coding Category
Dynamic Meta Tags with PHP

Meta Tags provide metadata about the HTML document and can be used by browsers, search engines or other web services. You can set Full Meta Tags with PHP and use them everywhere with dynamic changes in different pages. Most of PHP developers use reusable files like config.php (start session, define constants, connect to db, define functions and ...), footer.php (add HTML for page footer and ...) and also header.php to add head of HTML that contains link to JS and CSS and Meta Tags. Finally in the page like index.php use include_once("config.php"); in top, include_once("header.php"); in start of new HTMLs and include_once("footer.php"); in the end.

We add all of our default meta tags in config.php and use them in header.php and finally, change them in index.php, before including header.php.

1. In the config.php for example (you can add all of default meta tags like this):

if(empty($_SESSION['page_description'])) {

    $_SESSION['page_description'] = "Giliapps ⦿ Designing • Programming • Developing • Supporting ⥤ We ❤️  Websites, Apps and Games Development";

}


2. In the header.php for example (you can use all of them like this):

<meta name="description" content="<?php echo htmlspecialchars($_SESSION['page_description'])?>">

3. And finally in your page like index.php change them (only needed sessions and leave others) before including header.php:

include_once("config.php");

$_SESSION['page_description'] = "Giliapps ⦿ Designing • Programming • Developing • Supporting ⥤ We ❤️  Websites, Apps and Games Development";

$_SESSION['app_name'] = "Giliapps";

$_SESSION['page_lang'] = "en";

$_SESSION['og_url'] = "https://www.giliapps.com";

$_SESSION['og_title'] = "Giliapps ⦿ Website, Apps and Games Developer";

$_SESSION['og_description'] = "Giliapps ⦿ Designing • Programming • Developing • Supporting ⥤ We ❤️  Websites, Apps and Games Development";

$_SESSION['og_image'] = "https://www.giliapps.com/image/giliapps-logo-1500.png";

$_SESSION['og_image_type'] = "image/png";

$_SESSION['og_image_width'] = "1500";

$_SESSION['og_image_height'] = "1500";

$_SESSION['og_site_name'] = "Giliapps";

$_SESSION['og_type'] = "article";

$_SESSION['og_locale'] = "en_US";

$_SESSION['fb_admins'] = "541805102605555";

$_SESSION['fb_app_id'] = "166625253895555";

$_SESSION['article_published_time'] = date("c");

$_SESSION['article_tag'] = "giliapps";

$_SESSION['article_author'] = "Giliapps";

$_SESSION['itemprop_name'] = "Giliapps ⦿ Website, Apps and Games Developer";

$_SESSION['itemprop_description'] = "Giliapps ⦿ Designing • Programming • Developing • Supporting ⥤ We ❤️  Websites, Apps and Games Development";

$_SESSION['itemprop_image'] = "https://www.giliapps.com/image/giliapps-logo-1500.png";

$_SESSION['twitter_url'] = "https://www.giliapps.com";

$_SESSION['twitter_title'] = "Giliapps ⦿ Website, Apps and Games Developer";

$_SESSION['twitter_description'] = "Giliapps ⦿ Designing • Programming • Developing • Supporting ⥤ We ❤️  Websites, Apps and Games Development";

$_SESSION['twitter_image'] = "https://www.giliapps.com/image/giliapps-logo-1500.png";

$_SESSION['twitter_site'] = "@giliapps";

$_SESSION['twitter_site_id'] = "@giliapps";

$_SESSION['twitter_creator'] = "@giliapps";

$_SESSION['twitter_card'] = "summary_large_image";

$_SESSION['page_title'] = "Giliapps ⦿ Website, Apps and Games Developer";

$_SESSION['page_url'] = "https://www.giliapps.com";

include_once("header.php");


4. Improve this code by your comments. Thanks!

Author: Ismael Azaran

Do you like it? Share it with your friends.

Categories

Tags