Use Flattr with WordPress 3.5.1

Flattr is a “social micro-payment system”, that allows you to make online payments, when you like an article, a blog, a project…

I’ve decided to add it to my personnal websites, both the Jekyll site and my WordPress blog.

Looking at the Flattr documentation, the basic solution for WordPress.org blogs (hosted blogs) is to use the Flattr plugin. But the current version of the plugin (1.2.0) doesn’t work with WordPress 3.5.1. The Flattr button simply isn’t displayed.

So I’ve had to find an alternate solution, which was to simply create a WordPress function in my theme, and to call this function each time I want to add a Flattr button.

I’ve based my code on this post, but I’ve simplified it, as I didn’t need customization like multi-user handling.

So the final function, as used on my blog, is:

function fb_flattr_link($uid = 'ramy', $cat = 'text', $btn = 'compact') {

	$cat = htmlspecialchars($cat);
	$btn = htmlspecialchars($btn);

	$ftag = '';
	$tags = get_the_tags( get_the_ID() );
	if ( $tags ) {
		foreach( $tags as $tag ) {
			$ftag .= $tag->name . ', ';
		}
		$ftag = substr( $ftag, 0, -2 );
	} else {
		$tag = '';
	}

	$dsc = htmlspecialchars( strip_tags( trim( get_the_excerpt() ) ) );
	$dsc = str_replace( "'", "", $dsc );
	$dsc = str_replace( "\n", " ", $dsc ); // maybe \r\n
	$dsc = substr($dsc, 0, 150);

	$tle = htmlspecialchars( strip_tags( get_the_title() ) );
	$tle = str_replace( "'", "", $tle );

	$flattr = '
		<span class="flattr">
			<script type="text/javascript">
				var flattr_uid = "'.$uid.'";
				var flattr_url = "' . get_permalink() . '";
				var flattr_tle = "' . $tle . '";
				var flattr_dsc = "' . $dsc . '";
				var flattr_cat = "' . $cat . '";
				var flattr_tag = "' . $ftag . '";
				var flattr_btn = "' . $btn . '";
			</script>
			<script src="http://api.flattr.com/button/load.js" type="text/javascript"></script>
		</span>
	';

	echo $flattr;

}

To use it, I simply use this code each time I want to add a button:

<?php if ( function_exists('fb_flattr_link') ) fb_flattr_link(); ?>
No tips yet.
Be the first to tip!

Like this post? Tip me with bitcoin!

14zZyZq4rNVP2rMUmY6Qovb2H8YGt7DKot

If you enjoyed reading this post, please consider tipping me using Bitcoin. Each post gets its own unique Bitcoin address so by tipping you're not only making my continued efforts possible but telling me what you liked.

Leave a Reply

Your email address will not be published. Required fields are marked *