Convert ‘&’ in your product link titles into HTML in your Magento web design

Posted on Thursday 1st Dec 2011 by Gavin 1 Comment

Happy Thursday readers!

This week I came across a really big issue that I just couldn’t iron out. I am currently working on a Magento web design and after completing the site, I did my usual thing of checking it was all W3C friendly. I had checked it a few weeks previous and all was good, but since then my customer had added some products and I’d done some tweaks so I thought i’d better check.

To my horror I was presented with at least 11 errors per page, I have never had so many fails in all my web dev life and I really was shocked to see that awful red bar saying ‘FAILED‘ in it. Turns out, my customer had been adding in the products and using an ‘&’ on quite a few occasions. By default Magento will take the name of your product and use it for the title of the link so ‘Mens & Womens bath towels’ product name will also be the title. This is good, at least Magento takes this into consideration, what it doesn’t take into consideration is that people are going to use the ampersand in their product titles and Magento won’t convert it into HTML for you.

Let’s fix this shall we….

After some blog searching I came across nothing, so decided to tackle it myself. After some testing and tweaking, it was a relatively simple fix so I want to share it with you.

In any place that shows a product like this:

<?php echo ($_product->getName(); ?>

Change it to this:

<?php echo htmlspecialchars ($_product->getName()); ?>

This will find any character that requires a html specific code and transform it for you, so a plain text ‘&’ will soon be turned into a &amp;!

The main page you want to do this on is template/catalog/products/view.phtml – but really, it’s anywhere you are showing your products.

This code is supported in PHP4 and 5 but if you want to do the vise versa on other titles, it would be htmlspecialchars_decode but this is only supported in PHP5

Thanks for reading guys, I hope this saves you hours of searching like I had to do!

About Gavin

Spends most of his life infront of his mac discovering new ways to make things awesome. But on the rare occasion when a computer isn't around Gav enjoys track racing, basketball and ju jitsu.

See all of posts.

Add on Gavin Google+

Gavin's Tweet's

This entry was posted in eCommerce & Magento and tagged . Bookmark the permalink.

One Response to Convert ‘&’ in your product link titles into HTML in your Magento web design

  1. Anit Mehta says:
    December 1, 2011 at 6:34 pm

    You’re not the only one to have this problem.
    Client’s want control but without maintainance it can all really go haywire
    I’ve found that its best to keep clients on a leash & have them report any changes made so that we can double check them.
    On many occasions we’ve found they have an enormous bandwidhth or memory problem – mainly because they really don’t know how things work in the wed world and have to be corrected.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>