Flattr Button for WordPress without a Plugin

There is a lot talking about Flattr and the idea certainly is worth a try. That’s why I wanted to test and integrate Flattr in our blog. However, I don’t really like the Plugin and that’s why I created a small little function, which integrates the button as you can see right under the tweets of each post.

The function

The following small function goes in the functions.php of the themes or outsourced in a Plugin. It depends what is your opinion – for me it is theme-specific and thus a function of the theme and therefore in the functions.php of the theme.

 * Flattr Button in WordPress Templates
 * @author Frank Bültge
 * @link   Flattr API https://flattr.com/support/api
 * @param  integer  $uid   personal user ID
 * @param  string   $cat   Flattr category: text, images, video, audio, software or rest.
 * @param  string   $btn   Type of Flattr button: '' or 'compact'
 * @param  integer  $hide  Use this to hide the thing from listings on flattr.com. The value 1 will hide the thing.
function fb_flattr_link($uid = '', $cat = 'text', $btn = 'compact', $hide = 0) {
	$uid  = (int) $uid;
	$cat  = htmlspecialchars($cat);
	$btn  = htmlspecialchars($btn);
	$hide = (int) $hide;
	$noflattr = 0;
	$noflattr = (int) get_post_meta( get_the_ID(), 'noflattr', true );
	if ( !$uid )
		$uid = (int) get_post_meta( get_the_ID(), 'flattrid', true );
	if ( defined('WPLANG') )
		$locale = WPLANG;
	if ( empty($locale) )
		$locale = 'en_US';
	$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

	$tle = htmlspecialchars( strip_tags( get_the_title() ) );
	$tle = str_replace( "'", "", $tle );
	$flattr = '
	if ( !$noflattr && ( 0 != $uid ) )
		echo $flattr;


Within the template for your posts the above function will be called, the call would normally be appropriate in the single.php of the theme.

There is a query in this code, which checks a custom field with the name flattrid. There you can put an ID, so you can use the button in dependency of different authors. You can use the above function without the use of the parameter $uid , like this:

The button will only show if the custom field contains a flattrid and if the following field noflattr is not 1.

I also added a custom field – if you use this just type noflattr and as the value 1, then the button won’t display. I think there are posts that do not need a flattr button, for example because it is a guest post.

The button will be displayed within the span-element with the class flattr and can be addressed via CSS, here a small example:

.flattr {
margin: 5px 0 0 5px;

The function can be expanded in many ways, for example a profile field on authors profile page for the ID can help, better as an custom field.

You can also outsource everything in Metaboxes, thus making it more user-friendly and automatically showing it before the content or in your feed. Since Flattr provides its own Plugin, I didn’t wanted to build a Plugin and who may use the function can extend it accordingly, if the current functionality is not enough.





7 responses to “Flattr Button for WordPress without a Plugin”

  1. Theo Avatar

    I must admit that this is the first time i hear about Flattr, however, nice piece of code !

  2. Ozh Avatar

    Off topic: what plugin are you using to highlight code snippets?

  3. Frank Avatar

    @ozh: WP Syntax has problems with great traffic blogs, to much filter. I Have write a own solution with highlighting via javascript, maybe you will test the early beta? You can see live on my private blog http://bueltge.de

  4. Ozh Avatar

    @Frank: I don’t like client side highlighting. First, if you save the page, it’s gone if you don’t have the JS. Second, too many time I’ve seen Firefox halt because of highlighting JS.

    After a quick look at wp-syntax’s code it seems to be working the way I expected. 2 filters for post content is not a big deal I think.

  5. […] Flattr Button for WordPress without a Plugin […]

  6. Frank Avatar

    @Ozh: th etwo filters is a problem an big trafiix, the plugin fikter all synax with preg_replace and geshi set html-elements, this is slowly. I had many problems on great traffic-sites with this filters and i think you have also the problem, it is not possible to deactivate the plugin – then was interpreted the code in the content; i think it is better when you masked code save inside the content, inside the database.
    please, my comment is only my opinion and a hint.
    Best regards