Parsing Amazon affiliate links in WordPress

Featured Image

Recently my team had problem with Amazon affiliate links on our web site.
On the web, we have wordpress installation and inside main content of web page we have some Amazon affiliate links that entered in the content few years ago and not working any more because Amazon changed API somehow. I searched the web for some new version of wordpress plug-in for new API version, because our old plug-in for displaying that links properly was turned off.

FYI that links inside content look like this:

<a type="amzn"> “Jerusalem the Golden” performed by Rupert Gough–Malcolm Archer–Wells Cathedral Choir</a>

When links was formed like that, our site could never pass HTML validation, and also we had some click-able non-functional links in our content. It would be very hard work to parse every article inside database and remove that links from our content, because we have much content that would need checking and fixing.

I found the good plug-in that parses that links into proper formed links and making our links work, but there was one more problem to solve with that:

With regular Amazon API, like this new plug-in does, links are parsed with some included Javascript files that are linked directly from and small chunk of calling Javascript are included inside page to make it work.

But, what is happening when my colleagues develop iPhone application that reads content from web site’s rss feed are trying to make this links functional? They have to manually include all of this Javascript inside iPhone application and run that script locally from device.

I don’t think that is the best solution for us. So decided to make some own wordpress plug-in that will build that links with php and show them already parsed on web page and also inside feed to avoid Javascript parsing.

Here is the code:

<!--?php /*   Plugin Name: Amazon ASIN links parser   Plugin URI:   Description: Plugin that parses Amazon links with ASIN code and converts them to full Amazon affiliate url without javascript   Version: 1.0   Author: Darko Goleš   Author URI:   License: Surgeworks - all rights reserved.  */ define('DVO_LOCATIONS_PLUGIN_PATH', dirname(__FILE__)); define('DVO_LOCATIONS_PLUGIN_URL', get_option('siteurl') . '/wp-content/plugins/' . basename(DVO_LOCATIONS_PLUGIN_PATH)); include(dirname(__FILE__) . '/simple_html_dom.php'); //Adding filter into content add_filter('the_content', 'parseLinks'); function parseLinks($content) { 	/*if(is_feed()) 	{*/ 		$html = new simple_html_dom(); 		$html--->load($content);
		$ret = $html->find('a[type="amzn"]');
		foreach($ret as $val)
				$asin = $val->asin;
				$linktext = $val->plaintext;
				$newLinkStr = buildURL($asin,$linktext,'divine-office-20');
				$val->outertext = $newLinkStr;
		$content = $html->save();
    return $content;
function buildURL($asin, $linktext, $affiliate)
	return '<a href="' . $asin . '/?tag=' . $affiliate . '">' . $linktext . '</a>';

For parsing I used the SimpleHtmlDOM library that can be found on web for free download.

Now when displayed, Amazon links looks like this:

Feel free to use this code to make own plugin for AMZN links with ASIN code.

Cheers 🙂

1 comment

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=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.