Plugin-less Integration of Twitter Anywhere with WordPress

Continuing my attempt to avoid wordpress plugins when possible and hand code the theme to include different functionalities, I looked around for php code snippets so that I could include the twitter anywhere code in my theme – the hovercards, the tweetbox and all that jing-bang. I found it here. I also added the tweetbox in addition to that. Here are the code snippets that you need to include in your theme’s functions.php to get twitter anywhere on your theme. Enjoy!

Before we go any further, register for a twitter api key here => http://dev.twitter.com/anywhere/apps/new.
The example below shows how to fill out the form.

Application Name: My blog name
Application Website: http://www.myblog.com
Callback URL: http://www.myblog.com
Default Access type: Read & Write
Description: Some Description

Once you have submitted the form successfully, you will be redirected to a page where you will find your twitter api key. Keep that handy, we will need it in the next step.

Now it’s time to mod the functions.php in your theme folder. Open it in a text editor and add the following code snippet. This will add the twitter javascript code into the section of your theme. This will now allow us to add the hovercards, tweetbox etc.

$twitter_api_key = "Replace with your Twitter API key";
wp_enqueue_script("twitter-anywhere", 
  "http://platform.twitter.com/anywhere.js?id={$twitter_api_key}&v=1");

Let’s activate the hovercards now:

add_action("wp_head", "twitter_anywhere");
function twitter_anywhere()
{
  echo '<script type="text/javascript">';
  echo 'twttr.anywhere(onAnywhereLoad);';
  echo 'function onAnywhereLoad(twitter) { twitter.hovercards(); }';
  echo '</script>';
}

This is the code that I used to add tweetbox at the bottom of my single posts. On line 4, I’m fetching yourls customized shorturl for my post. If you don’t use yourls and the YOURLS: WordPress to Twitter plugin, I have mentioned a few other ways of fetching the shorturl following the code snippet below.

function add_tweetbox() {
        global $post;
        $id = $post->ID;
        $shorturl = wp_ozh_yourls_geturl( $id );
        $title = $post->post_title;
        $twitterid = "@free_thinker";
        $content = $title . " " . $shorturl . " " . $twitterid;
?>
        <div id="tbox"></div>
        <script type="text/javascript">

                          twttr.anywhere(function (T) {

                            T("#tbox").tweetBox({
                              height: 40,
                              width: 616,
                              defaultContent: "<?php echo $content ?>",
                                        label: "Shout!"
                                });

                  });

        </script>
<?php
}

Here are the alternatives for fetching the shorturl.

If you have the WordPress Stats Plugin, then you can fetch native wordpress shorturls using the following code snippet.

global $post;
$shorturl = get_shortlink($post);

Alternatively, you can use the wordpress shortlinks that have the format – http://example.com/?p=1234

global $post;
$id = $post->ID;
$shorturl = wp_get_shortlink($id, 'post');

3 comments

  1. But why exactly do you want to avoid plug-ins? Putting custom code in the theme is a maintenance nightmare. Avoid plug-ins might give you a slight performance boost but that’s worth it only for high traffic sites.

    This is one of the things that Drupal does better than WordPress. In Drupal all blocks in the sidebar are simply blobs of PHP code or html. Plug-ins (modules in the Drupal world) can make special sidebar blocks available but that doesn’t restrict the admin from maintaining simple blocks without fiddling with theme code.

    1. Hi Varun,

      All the code that I have put use very standard wordpress tags like add_action and add_filter which are not going to be changed anytime in near future. Plus ofcourse the twitter api, which shouldn’t change either. Plus its also an excuse to learn, instead of blindly using plugins. And my themes have broken because of plugins too and plugin maintainers who refuse to upgrade in time.

Leave a Reply

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