« Securing the web with SSL Client Certificates Converting a simple XML snippet to an array »

Getting Flattr to play nice with your WordPress 3 setup


If you are experiencing problems using the Flattr plugin with WordPress then it's very likely that you are using a plugin or a theme that is incompatible with the Flattr plugin. More often than not your issue will be related to excerpts. Because of how WordPress and most other blog engines handles plugins it's difficult to write a plugin that is compatible with all other plugins.

When trying to resolve a conflict between two plugins the answer is usually one of two solutions.
1) Disable one of the plugins. This is the easiest fix but not really the answer since you want the functionality of both.

2) Patch one or both of the plugins with a case specific fix. Example: plugin1 could check for the presence of plugin2 and if found disable or modify the behavior of the conflicting function in itself. This option is highly impractical since there is an endless amount of WordPress plugins out there and maintaining compatibility with them all is futile at best.

So what is the solution then? Well unfortunately there is no easy fix for this, if there is I simply don't know about it. But you don't have to give up. Often you can get the functionality you are after with a little manual labor (read: coding).

The Flattr plugin

In the case of the Flattr plugin the function that people have problems with is the auto injection of the button. If we disable all auto injection we can still render the button by adding a piece of code to the template without the risk of getting conflicts with other plugins.

I know some people will think that it's to difficult or impractical to edit the theme, but it really isn't, in fact It's easy as pie. Most of you will change your theme maybe once a year or perhaps never so you can't say that it's impractical either. And since virtually all WordPress plugins follow the same schematic, learning how to change it in one means you know how to do it in all others as well.

Modifying the theme

WordPress themes consist of a handful of template files. Each template has a specific name and purpose regardless of the theme. This schematic is decided by the makers of WordPress so it's unlikely to change between themes. For our purpose we need to focus on the template single.php.

single.php (or sometimes single-post.php) is used to display a single post and this is, in my opinion, the best place to put your Flattr button. Open the file in your favorite text editor and search for the row containing "the_content();". This function call is what outputs the contents of your post to the browser. So this is also a good place to put the Flattr button. Depending on wether you would like the flattr button to render above or below the content place "the_flattr_permalink()" function call before or after the "the_content();". It should now look something like this . That is all there is to it, you should now have a Flattr button in your post.

Sometimes you might want to put the Flattr button on pages where multiple posts are displayed. There is some inconsistency when it comes to which template file that is used for those pages. But the idea is the same as above. Look for a call to the_content() or the_excerpt() and if you find it then you know where to place the Flattr button code. Depending on your theme, WordPress version the template files to look in could be: index.php, loop.php, loop-index.php or search.php. If you can't seem to find it just search in each file, it's in there somewhere.

For the handy ones theres also the "get_the_flattr_permalink()" call that will return the code of the button instead of outputting it directly. This might be usefull if you want to make changes to the button html before it gets sent to the browser.

Flattr plugin at WordPress.org
Flattr plugin at GitHub (Not always up to date)