Parsing Amazon affiliate links in WordPress

Parsing Amazon affiliate links in WordPress

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 Amazon.com 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: http://www.surgeworks.com   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: https://inchoo.net/author/darko.goles/   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)
		{
			if(isset($val->asin))
			{
				$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="http://www.amazon.com/dp/' . $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:

http://www.amazon.com/dp/B0000542GQ/?tag=divine-office-20

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

Cheers 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

Magento Coding Standards Damir Korpar
Damir Korpar, | 5

Magento Coding Standards

TWIG with wordpress part2 Darko Goles
Darko Goles, | 9

TWIG with wordpress part2

TWIG with WordPress part1 Darko Goles
Darko Goles, | 1

TWIG with WordPress part1

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>.

Tell us about your project

Drop us a line. We'd love to know more about your project.