Question

For example say a user is selling a computer. The title of the product and the description of the product will go in the meta tags like this.

<title><?php echo $product_title; ?></title>
<meta name="description" content="<?php echo $product_desc; ?>" />

Is that correct way of doing things on a user based website? If so, what if the product description is like 500 characters? Is that too long for meta tags? Should it be cut?

Was it helpful?

Solution

Yes that approach is fine but you should also use htmlspecialchars. For example:

<title><?php echo htmlspecialchars($product_title); ?></title>
<meta name="description" content="<?php echo htmlspecialchars($product_desc); ?>" />

Regarding the length of the meta description. Google has the answer:

Meta descriptions can be any length, but search engines generally truncate snippets longer than 160 characters. It is best to keep meta descriptions between 150 and 160 characters.

So, if you would like to do the work for the search engines you can truncate the meta description by using substr.

<?php
    echo '<title>'.htmlspecialchars($product_title).'</title>'.
         '<meta name="description" content="'.htmlspecialchars(substr($product_desc,0,160)).'" />';
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top