Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

CHAPTER 7 • Making the Most of WordPress Plugins 203


$content .= $adaff_url[urlbefore];
$content .= $addaff_link_url;
$content .= $adaff_url[urlafter];
$content .= '">';
$content .= $addaff_link_title;
$content .= '';


// Return the content
return $content;


Remember that addaff_option is now in $adaff_url as an array. So pull the
urlbefore one, like this: $adaff_url[urlbefore]. After that, you get the product
URL from the post meta data, which you saved in $addaff_link_url. The affiliate URL is
done after you’ve added whatever was saved as $adaff_url[urlafter] on the settings
page. The rest of this part is pretty self-explanatory, so let’s skip it and just return the now
expanded $content, thus outputting the content through the filter.


It is tempting to stop there, but remember that you need to return $content, unaltered, if
either $addaff_link_title or $addaff_link_url (or both!) lacks content. That’s
what this else clause is for:


// Nope, both $addaff_link_title and $addaff_link_url didn't have content
else {
// Return the content
return $content;
}


That’s it, short of closing the PHP tag. Figure 7-8 shows the plugin in action, outputting a link
within an unordered list, which was built using the parameters saved on the settings page,
along with the title and product link that are saved on a post basis.


Figure 7-8: The AddAff plugin in action, displaying "Buy the book of Paazu" and using the Twenty Fourteen theme.

Free download pdf