“Vote for me…” how to embed in WordPress

[Clarification: I’m not actually asking you to vote for me :D, the title just follows a previous post]

Diego Medina has published a JavaScript code that can be embedded in your blog posts, and which allows for voting on Planet MySQL from within your blog.

Shared below is how to set this up for WordPress users. This is not a WordPress plugin, mind you. You’ll need to manually edit the WordPress template files (can be done from the Dashboard->Appearance->Editor->Single post).

The page you’re likely to edit is single.php, but depending on your template this can change. The explanation below assumes a single post page. This can also be worked out for your blog’s home page, which lists several entries.

Since there is no point in presenting the Planet MySQL voting widget for entries which do not relate to MySQL, the code verifies that the post is in the ‘MySQL’ category. You need to change this if your categorization differs. Mind that the category’s name is case sensitive.

<?php if ( in_category( 'MySQL' )) { ?>
        <script language="JavaScript"><!--
        var planet = "http://planet.mysql.com/entry/vote/?apivote=1&";
        var lk=encodeURIComponent(document.location);
        var thumb_up="<img src=\"http://planet.mysql.com/images/thumbs_up_blue.jpg\" border=\"0\" />";
        var thumb_down="<img src=\"http://planet.mysql.com/images/thumbs_down_blue.jpg\" border=\"0\" />";
        document.write('Vote on the <br /><a href=\"http://planet.mysql.com/\" >Planet MySQL</a><br />');
        document.write('<a title=\"Vote me Up on the Planet MySQL\" href=\"' + planet + 'vote=1&url=' + lk  +  '\">' + thumb_up + '</a>');
        document.write('&nbsp;');
        document.write('<a title=\"Vote me Down on the Planet MySQL\" href=\"' + planet + 'vote=-1&url=' + lk  +  '\">' + thumb_down + '</a>');
        // --></script>
<?php } ?>

The in_category( ‘MySQL’ ) function determines if the current post is relevant. You may also choose to show the widget based on tags, in which case you’ll need to use get_the_tags(), and verify your MySQL tag is in the resulting array.

I chose to embed this just before the comments area.

Good luck

2 thoughts on ““Vote for me…” how to embed in WordPress

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.