Use PHP to Generate Dynamic Meta Tags

Give Each Page Specific, Relevant Information

Rebecca Mastey

While meta tags, titles and descriptions may not be the Holy Grail of search engine optimization, these HTML header sections can provide additional value to a correctly optimized page. Many websites, especially those with dynamic content, will benefit from assigning unique tags, titles and descriptions to individual pages. PHP simplifies this process and removes manual editing of individual pages.

Before approaching the PHP, familiarize yourself with the three items this tutorial discusses. At the top of your browser, you will see the Associated Content page title. For any given article, this is Article Title - Associated Content. This not only appears as the page title in search results, but lets users who use multi-tabbed browsing identify your page.

The description is a short block text, typically less than 160 characters, which is displayed with your title in search results. If this section is not completed, the search engine will select an appropriate section of text from the page to display.

Meta tags are, for all intents and purposes, useless for most modern search engines such as Google, Yahoo and Bling. Some search engines still use it while crawling pages and social bookmarking sites may rely on the tags to categorize your content or associate it with other websites.

To properly implement these items dynamically, ensure your pages use the .php extension rather than .html or .htm. Your pages must be in at least two parts. First, header.php, which will contain all the static information contained at the top of your page. These static items include logos and navigation buttons.

The rest of the content should be contained in an appropriately named .php file, such as index.php for the homepage or about.php for information on yourself, your company or your organization.

The following sections outline the necessary code. If you're comfortable with PHP and want to get right to it, use this and you'll be on your way. If you're not comfortable with it, continue reading after these sections for explanations.

In the header.php file, place the following lines

- Your Webpage Name

In the index.php or pageTitle.php files, insert the following lines at the very top

$pageTitle = "My page title";

$metaDescription = "My page description, less than 160 characters.";

$metaTags = "a list, of appropriate, tags, separated with, commas";

include("header.php");

?>

If you are unfamiliar with PHP, here is a quick primer. The $ + word combination denotes a variable. This is simply a short place holder for a longer section of text. There are exceptions to this rule for numbers, equations and other variables, but, for the purposes of this tutorial, remember to enclose the text in quotation marks.

By typing include("header.php"), you are using the include function. This takes all the information from header.php (or any other included file) and places it exactly where you have called the function. It effectively merges the two documents when the page is loaded.

If you think of the two documents as merged, the variables will be declared at the very top of the page. The header file will then render, and echo, or print, the information inside the previously declared variables. Because the PHP itself is handled by the server, not your browser, your website will display the header information as plain text.

My page title - Your Webpage Name

Define $pageTitle, $metaDescription and $metaTags and include the header.php file at the top of every content page. Each page will display unique, dynamic meta information when it is loaded.

Published by Rebecca Mastey

Rebecca has been writing for fun and profit for the past 5 years and specializes in politics, technology, parenting and cuisine. Presently, she is researching and writing about sustainable technologies.  View profile

4 Comments

Post a Comment
  • J. Van Horne4/11/2011

    Thank You soo Much. Such a simple piece of script and I was just about banging my head against the wall. Thanks A Million!!!

  • Rebecca Mastey9/16/2010

    @maximo
    You need to specify specific meta tags on a page by page basis. $metaTags, for example, might be "cat, dog, mammal" on one page, but "chicken, pheasant, poultry" on another. Since each will be declared on an individual page, you can change them as you go while keeping the header static across the site.

  • maximo7/9/2010

    I'm sorry, I may sound dumb but it seems
    to me that the above script would echo
    the same exact meta on every page.
    How can you have different content displayed if you don't define it and tell php what to use where?

  • Ryan Ray2/3/2010

    Thank you thank you thank you thank you!! I've been trying to make this happen for years and your post made it much less complicated than I've seen on other Blogs! Soooo appreciate it!! Cheers!

Displaying Comments

To comment, please sign in to your Yahoo! account, or sign up for a new account.