Making Dynamic Meta Description Tags Using PHP and MySQL

December 8, 2006 – 1:36 pm

One of the issues I’ve been dealing with lately, is the issue of adding meta description tags to certain websites of mine that didn’t have them. It’s rumored that websites that have many pages with the exact same meta description tags have fallen into ill-repute in Google, and have landed themselves in the Supplemental Index. The solution, say many, is to make sure they’re all different.

My approach to the problem for static web pages that I had in a MySQL database

1) Use the on-page content to make the Meta tag description.
2) Retrieve up to 130 characters, and end with a … so it looks more interesting, and increases clickability

My thought was that the on-page content would make the best description, and each meta description would be different because the on-page content varies so much. Add the … after 130 characters and you have a nice, clean description that anyone would be proud to have.

Here’s the function I use to trim the Description at 130 characters:

< ?php
function nicetrim ($s) {

$MAX_LENGTH = 130;
$str_to_count = html_entity_decode($s);
if (strlen($str_to_count) < = $MAX_LENGTH) {
return $s;
}

$s2 = substr($str_to_count, 0, $MAX_LENGTH - 3);
$s2 .= "...";
return htmlentities($s2);
}
?>

Simply retrieve the data from your MySQL database, format it using Nicetrim, and echo it out in your header section in the form of a meta tag.

$newdesc = nicetrim($row['content']);
echo ““;
echo ““;

Something along those lines should work very nicely.

The result is a very nice, readable snippet, created dynamically, that displays a snipped that the person will actually want to click on, because it’s created from the on-page content.

  1. One Response to “Making Dynamic Meta Description Tags Using PHP and MySQL”

  2. That’s a great idea, specially for blogs and huge sites that have been already built without it (oh noes)…

    And for Wordpress users, there’s also one plugin that does exactly the same (even the … )

    http://guff.szub.net/head-meta-description/

    Try it, it works fine for me, but anyway.. it’s always better have an easy script that you could modify for your needs :D

    By Edd on Dec 24, 2006

Post a Comment