Can you advise where the code lives that controls the meta tags, so i can customize
Hi David,
I'm trying to change the keywords and descriptions for each product and I´m having some trouble understanding the code above. What does htmlentities($q,ENT_QUOTES,$config_charset); stand for?
What I want to do is changing the code to display name,brand,category and so on as the keywords, and also create a dynamic descriptions, for example "The product 'name' is of the brand 'brand' and belongs to the 'category'-category". Think you can point me in the right direction here David? :)
Best regards
Daniel
Hi Daniel,
The code:
htmlentities($q,ENT_QUOTES,$config_charset);
...is basically returning the value of $q - which is the product name; however, when you output a variable as part of the HTML, you have to make sure that the content of that variable does not break the HTML markup of the page; which is what the htmlentities() function does. The $config_charset parameter just makes sure that the htmlentities() function is working in the same character set as the page that is being generated.
As you're looking at the product page - products.php, the description meta tag is currently being generated based simply on $q (the product name) with the following code on line 52:
$header["meta"]["description"] = translate("Price search results for")." ".htmlentities($q,ENT_QUOTES,$config_charset);
However, at this point in the script, the product record has already been loaded, and is item zero in the $product["products"] array, so all the database fields can be referenced through $product["products"][0], for example:
$product["products"][0]["name"]
$product["products"][0]["category"]
$product["products"][0]["brand"]
Therefore, these variables can be used in your construction of the description meta tag, based on your sample format - the code would be (remembering to use htmlentities()!!):
$header["meta"]["description"] = "The product ".htmlentities($product["products"][0]["name"],ENT_QUOTES,$config_charset)." is of the brand ".htmlentities($product["products"][0]["brand"],ENT_QUOTES,$config_charset)." and belongs to the category ".htmlentities($product["products"][0]["category"],ENT_QUOTES,$config_charset);
However... this would only generate sensible descriptions if every product on your site has a brand and category value (remember that these are optional fields during feed registration). So a more comprehensive version would check for both brand and category being set before outputting that part of the sentence; for example:
$header["meta"]["description"] = "The product ".htmlentities($product["products"][0]["name"],ENT_QUOTES,$config_charset);
if ($product["products"][0]["brand"]) $header["meta"]["description"] .= " is of the brand ".htmlentities($product["products"][0]["brand"];
if (($product["products"][0]["brand"]) && ($product["products"][0]["category"])) $header["meta"]["description"] .= " and";
if ($product["products"][0]["category"]) $header["meta"]["description"] .= " belongs to the category ".htmlentities($product["products"][0]["category"];
"category"],ENT_QUOTES,$config_charset);
Hope this helps!
Cheers,
David.
Thanks for the fast answer!
Yeah it helped alot, now it´s exactly as I want it!
Thanks again,
Daniel
ps. I´ve emailed you the search.php and includes/admin.php like you requested in the Advanced Search thread. Dont know if it got stuck in the spam filter, you could take a look at it if you get some time to spare.
Sure;
Firstly, you will notice that meta tags are set in html/header.php with the following code:
<?php if (isset($header["meta"])): foreach($header["meta"] as $name => $content): ?>
<meta name='<?php print $name; ?>' content='<?php print $content; ?>' />
<?php endforeach; endif; ?>
What this does is read the array $header["meta"], and for each element in that array it outputs a meta tag, with the name attribute being the array key; and the content attribute being the value. For example, to set the "keywords" meta tag; you would load the array (prior to including html/header.php) as follows:
$header["meta"]["keywords"] = "These, Are, Some, Keywords";
Currently, the meta keywords and description tags are set within products.php and reviews.php. Look for the following lines:
<?php
$header["meta"]["description"] = translate("Price search results for")." ".htmlentities($q,ENT_QUOTES,$config_charset);
$header["meta"]["keywords"] = htmlentities($q,ENT_QUOTES,$config_charset);
?>
By changing the contents of the above you can control the meta tags on those pages; or by adding similar code to any other page; before including the header script.
Hope this helps!