Getting BuyMeABeer To Show Only On Selected Posts

I have now adopted the Buy Me A Beer plugin, but there was one thing I didn’t like: it is not possible to make it appear on the sidebar for selected posts only. I wanted it on my plugin pages, but not my other posts (I don’t believe in donations for posts). Here I explain how to do this.

Firstly, the concept behind the plugin is fantastic. Full credit to the guys at Blogclout. For me, it is a great way to ask for donations for the plugins I write – as long as it doesn’t appear on other posts / pages.

Note: To acheive this, we need to make a small change in the plugin file. Although many people don’t like to edit plugins, this seems to be the only way to do it at present (Buy Me A Beer v1.4). Hopefully the developers can add this to future versions, so there is no need for this hack.

How Buy Me A Beer Is Displayed

Buy Me A Beer can be displayed in two places:

  • In the post body, just before the comments (with a small icon)
  • In the sidebar, via Sidebar Widgets (with a large icon)

For the post body, the following parameters control whether it is displayed:

  • Auto Insert on the Option page: should it appear in all posts or none.
  • Display Buy Me A Beer in right column of the Write / Edit post screen: Each post can be set to either Default, Yes, or No. Default means use the Auto Insert option, Yes & No override the Auto Insert option.

So for the post body, it is possible to control on which posts it appears. Set Auto Insert to off, then set Display Buy Me A Beer to Yes for those posts you do want it to appear for.

By default, Buy Me A Beer is not displayed on the Sidebar. To turn it on, go to Presentation -> Widgets (or Sidebar Widgets) in your Admin menu and add the Buy Me A Beer widget. This will cause it to appear on all posts.

Unfortunately, the sidebar widget does not seem to be affected by the Display Buy Me A Beer option set in the Write / Edit page area, so there is no way to control on which posts it appears.

Getting Sidebar Widget To Appear Only For Selected Posts

To make the sidebar widget appear only for certain posts, we are simply going to tell it to use the Display Buy Me A Beer option mentioned above. To do this we need to make a small change in the plugin file.

Edit the buy-me-beer.php file which comes with the plugin. Look for the following code (on about line 34):

  function display_widget($args) {
     echo buymebeer::display($args);
  }

and replace it with:

  function display_widget($args) {
    global $post;
    if (get_post_meta($post->ID, 'buymebeer-post', true)=="yes"){echo buymebeer::display($args);}
  }

Upload the edited file to your server, overwriting the original file.

Problem solved! Make sure you have the sidebar widget enabled (it will no longer show on all posts) and set the Display Buy Me A Beer option to Yes for any posts which should display the sidebar widget.

Using my site as an example, if you look at the sidebar for this post there is no Buy Me A Beer widget. If you go to the DualFeeds plugin page, you’ll notice it does appear there.

If You Are Not Using Sidebar Widgets

If you are not using Sidebar Widgets, you can still get Buy Me A Beer to display in the sidebar.

To get it to display for all posts, edit sidebar.php in the wp-content/themes/yourtheme folder (where yourtheme is the theme you are using) and add the following where you want it to appear:

<?php buymebeer_sidebar(); ?>

To get it to display only for those posts that have the Display Buy Me A Beer option to Yes, edit sidebar.php and use the following instead:

      <?php global $post;
        if (get_post_meta($post->ID, 'buymebeer-post', true)=="yes"){buymebeer_sidebar();}
      ?>

Final Word

Apart from this one issue, the Buy Me A Beer plugin seems great. Now I just have to wait and see if anyone actually wants to donate!

One response on “Getting BuyMeABeer To Show Only On Selected Posts

  1. tony@Cheap web hosting.

    Great tip. I am sure you could use this for other plugins if you wanted. Buy me a beer i think i would just use that in certain post and save the code for another plugin.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Quiz: