eantics logo
 
Follow us on Twitter  Subscribe to our Blog  Linked in

Category filter for Sociable Wordpress Plugin

Author: Ollie Phillips, Monday, March 30, 2009, Posted in Web Design
Tags: , , ,

I installed the Sociable plugin by Joost de Valk to my Wordpress website at the weekend. Sociable is a simple plugin that applies social bookmarking to the bottom of your posts, which allows your visitors to bookmark or share your post assuming they like what you have to say!

The plugin installation went fine, but because my entire website is running under Wordpress and not just the blog area, it served up social booking icons in some (well lots of) places I didn't want them. Not the plugins fault, more down to the fact that I'm using Wordpress beyond what it was designed for or more likely probably just in an unorthdox way, using posts to build sections on pages.

Fortunately, I'd taken my own advice as in "Beyond blogging with Wordpress" and had anticipated this, so it got installed to a development website first.

So how to use Sociable in the way that I needed to.  Sociable can be disabled at post level, but my site was built and I didn't fancy having to identify all the posts where I didn't want it displayed and switch it off.

No, what I needed was a way to filter by category so the social bookmarking links were only output on what I considered to be true blog posts rather than individual sections of pages, and I wouldn't have to disable it for each new post that wasn't a blog post.  A quick look in the settings revealed that there were some configuration options but none that would let me exclude some categories.

So off to hack the plugin I went. The code can be found in sociable.php, and the function sociable_html() which starts on line 588.

The line i replaced is found right at the bottom of the function:-

function sociable_html($display=Array()) {

.....

return $html; //<---This line
}

And I added this filtering code and commented out the orginal:-

function sociable_html($display=Array()) {

.....

// Category Filter. Only show Sociable if posts are NOT in the categories defined in $mycatfilter
// Ollie Phillips
// www.eantics.co.uk

global $wpdb;
$mycategory = $wpdb->get_var("SELECT name AS category_name FROM $wpdb->terms WHERE term_id = (SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id=$post->ID limit 1)");

$mycatfilter = array("website content","work","sandpit"); //<-- My category names where I don't want it displayed

if(!in_array($mycategory,$mycatfilter)){
return $html;
}
// return $html; //<--- Just a comment now
}

Word of warning: As this was a quick and dirty fix, the SQL is not that efficient, and could be hit and miss for posts with multiple categories, I'm not that familiar with the tables in use, so if anyone can make this better please post it up here.

Hope others find this useful too. I would bet the future versions of Sociable will do this, and in a much nicer scripted way, but for the moment this works.

Share or Bookmark this post:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google
  • E-mail this story to a friend!
  • Furl
  • LinkedIn
  • MySpace
  • Netvibes
  • Print this article!
  • StumbleUpon
  • Technorati
  • TwitThis
 

Leave a Reply


eantics just tweeted:
FOLLOW US