Adding a Social Media Button to Your Posts

All of the modifications we've made so far have relied on action hooks. Let's try one with a filter hook, shall we? Unlike an action, a filter will receive some content from WordPress that it can modify and must then pass along, so that lower priority filters can act on it, and so that it can be output. This means that your filter function should accept an argument (the initial content), and needs to return the modified content (the output) when it's done.

For this example, we'll be adding a social media share button to the end of every post. We'll grab a nifty combined share button from AddThis,13 though you could use whatever share buttons or combination of them you prefer. Head on over to AddThis.com and grab yourself a button (choose Website from Select your service, as the WordPress option provides you with a plugin rather than a code snippet). At the end of the process, you'll be given some HTML to copy and paste into your template. We'll use that in our function:

chapter_05/v4/wicked/functions.php (excerpt)

function wicked_linklove($content) { if(is_single()) {

$content .= '<div class="linklove"> Did you love this post? Tell everyone you know, right now! • Paste the markup you received from AddThis here. </div>';

return $content;

Notice that, unlike the action hook functions we wrote previously, this one accepts an argument: $content. That parameter will contain whatever content WordPress is applying a filter to. We take that variable and append our new .linklove div to it, then return it for subsequent filters or for display (if our filter was the last one).

If you forget to return the content at the end of your filter, your filter will act like a black hole: WordPress will pass the content in, but nothing will come out. That would result in the entire contents of all our posts disappearing!

We're also using a quick if statement to check if the post is being displayed on its own page: we only want to display the share link on the full view, not in excerpts. This is accomplished with WordPress's is_page function. WordPress has a great selection of these conditional tags14 that allow you to display content based on the conditions met by the current page; it's worth becoming familiar with them.

Now to tell WordPress to apply this filter to each post's content. Thematic passes the full content of every post through a filter handily named thematic_post, so let's try using that:

chapter_05/v4/wicked/functions.php (excerpt) add_filter('thematic_postl3'wicked_linklovel3 90);

Pass It Along

13 http://www.addthis.com/

14 http://codex.wordpress.org/Conditional_Tags

We have used a priority of 90 to try to ensure that our filter will execute after any other filters modifying the same content; the priority argument is entirely relative to the other filters, so if it appears in the wrong place just try adjusting the number up or down. Load it up in your browser: instant social media rockstardom is yours!

Make Money Messing Around On Facebook

Make Money Messing Around On Facebook

In this short guide you are going to learn exactly how you can start earning money in as little as 24 hours, doing the things that you already do on Facebook! Did you know that social media has become one of the biggest drivers of sales and new customers for businesses? Companies that used to spend millions of dollars on radio advertising, TV commercials and billboard signs, are now shifting huge chunks of their marketing budgets to social media sites like Facebook.

Get My Free Ebook


Post a comment