Tech Tip Station - Tips and Tutorials for Computer Users and Programmers Home | Projects | Pricing | Login | Contact
 

Advanced PHP Templates with Dynamic Meta Tags for SEO

The purpose of this tutorial is to show how to create a php website that uses an advanced template system which makes it easy for adding new pages to a website and easily adding new meta keyword and description values and page titles.

Dynamic Meta Tags

This takes into account that you might want different pages to have different meta tags for search engine optimization (SEO) reasons. The template stores the structure of the meta tags, while you simply specify the meta keywords and description values as PHP variables within the pages. Then the template dynamically outputs the values inside the meta tags.

Automatic Page Title Based on <h1> Tag

The template processor automatically picks out the page title based on the text inside of the <h1> tag on the individual pages. You can define in the template whether to append your website name to the page title as well.

There are 3 Different Types of Files

  1. Pages: Files such as: index.php, about.php, contact.php, etc.
  2. Template(s): File(s) that define the look-and-feel of your website.
  3. Template Processor: A file that will combine the pages with the template.

Examples of the 3 Different Types of Files Are Found Below

1. Example Page (Ex: index.php)

  1. Define variables for the template to use.
  2. Include the template processor
  3. Write the html for the page
<?php

	// Define meta tag values for SEO
	$meta_keywords="keywords,for,search,engine,optimization";
	$meta_description="Description of this page.";
	
	// Run the template processor
	require_once("template_processor.php");

  /*
   * The content from the <h1> tag is copied
   * into the <title> tag automatically by
   * the template processor.
   */ 
?>

<h1>Welcome To The Website</h1>

We hope you enjoy your visit here.

2. Example Template (Ex: /templates/default/template.php)

  1. Define html for the overall look-and-feel.
  2. Include the variables that are set in the page and template processor.
  3. Output the page content.
<html>
<head>
  <title><?=$PageTitle?> | XYZ Corporation</title>
  <meta name="keywords" content="<?=meta_keywords?>">
  <meta name="description" content="<?=meta_description?>">
  <link rel="stylesheet" href="/templates/default/style.css">
</head>
<body>

<a href="/index.php">Home</a> |
<a href="/contact.php">Contact Us</a> |
<a href="/about.php">About Us</a>

<hr />

<?php
  /*
   * Output the page content which
   * get stored in a variable when
   * template_processor.php is run   
   */         
  echo $PageContent;
?>

<hr />

<div style="text-align:center">
  Copyright &copy XYZ Corporation
</div>

</body>
</html>

Example Template Processor (/template_processor.php)

  1. Define variables for the template to use.
  2. Pre-load the page content.
  3. Find the <h1> tag and save it’s text for use in the <title> tag.
  4. Pre-load the template.
  5. Output the variable containing the template and page contents.
<?php

  /*
   * Specify the default template folder in case
   * one isn't specified in the page.
   */      
  $template = isset($template) ? $template : "default";
  
  /*
   * Specify default meta tags in case
   * they aren't specified in the page.
   */      
  $meta_keywords = isset($meta_keywords) ? $meta_keywords : "";
  $meta_description = isset($meta_keywords) ? $meta_description : "";

  /* 
   * Pre-load the page so that the meta tag variables
   * and page content will be available to the
   * template processor and the template file.      
   */
  ob_start();
    require(substr($_SERVER["SCRIPT_NAME"], 1, strlen($_SERVER["SCRIPT_NAME"])));
    $PageContent = ob_get_contents();
  ob_end_clean();
  
  /*
   * Now that the page content has
   * been loaded into memory, extract
   * the value in the <h1> tag and use it
   * for the <title> tag.
   */
  // Find where the <h1> tag starts and ends
  $H1TagStartPosition = strpos($PageContent, "<h1>");
  $H1TagEndPosition = strpos($PageContent, "</h1>");
  
  if($H1TagStartPosition > 0 && $H1TagEndPosition > $H1TagStartPosition){
    // Find where the <h1> text starts and ends
    $H1TextStartPosition = $H1TagStartPosition + 4;
    $H1TextEndPosition = $H1TagEndPosition - $H1TextStartPosition;
    $PageTitle = substr($PageContent, $H1TextStartPosition, $H1TextEndPosition);
  }
  else{
    $pageTitle = "";
  }
  
  /* 
   * Load the template. (The template will output
   * the content that is now stored in $PageContent.)
   */
  ob_start();
    require("templates/".$template."/template.php");
    $TemplateAndPageContent = ob_get_contents();
  ob_end_clean();
  
  /*
   * Display the template and page content.
   */
  echo $TemplateAndPageContent;
  
  
  /*
   * Stop here. Since the page content has already been
   * loaded and displayed by this point, you must
   * exit so that the page content doesn't show again.      
   */
  exit();
  
?>

7 Responses to “Advanced PHP Templates with Dynamic Meta Tags for SEO”

  1. Marcelo Says:

    Thank you very much for this, but the problem that i have is when get genereted the html, it apears with a lot of , and

    Please, how can i resolve that?
    Thank you very much

  2. Marcelo Says:

    I mean wuth a lot of : head, body and html

  3. Anthony Tietjen Says:

    Marcelo. Thanks for commenting. Can I ask what version of PHP, web server, and operating system you are running it on?

    Perhaps line 22 of template_processor.php needs to be modified for different servers.

  4. Anthony Says:

    Hey Anthony :-) weird, kinda like I’m writing to myself! I would like to add dynamic metatags on an existing php script for a gaming site I run. The database is in MYSQL and I assume I can add new columns for the tags, but don’t know how to call them so that they are dynamically updated with each new page.

    If you can help, let me know! I also have a few other projects in PHP/AJAX form population that I hired programmers to do but they never finished. If you know of anyone with those skills that won’t cost me any arm and a leg.. do tell :-)

    Cheers… Anthony

  5. Anthony Tietjen Says:

    Anthony. Thank you for commenting.

    I’m not sure exactly what you want to accomplish.

    Are you wanting to extract the main keywords out of the content from each page, store those keywords in the database, and then output the keywords in the corresponding page’s meta tag?

  6. Henry Says:

    Fatal error: require() [function.require]: Failed opening required ” (include_path=’.:/usr/lib/php:/usr/local/lib/php’) in /home/template_processor.php on line 22

    getting that error

    something to do with ob_start();
    require(substr($_SERVER["SCRIPT_NAME"], 1, strlen($_SERVER["SCRIPT_NAME"])));
    $PageContent = ob_get_contents();
    ob_end_clean();

    and

    ob_start();
    require(”templates/”.$template.”/template.php”);
    $TemplateAndPageContent = ob_get_contents();
    ob_end_clean();

  7. Anthony Tietjen Says:

    Henry,

    It might be that the web server you are using does not have the “SCRIPT_NAME” variable. Try using $_SERVER["PHP_SELF"] instead of $_SERVER["SCRIPT_NAME"] for both of those instances.


    Anthony

Leave a Reply

 
© 2006 - 2008 Anthony Tietjen