Drupal: how to place the Tweet Meme Widget EXACTLY where you want

As you can see in this very article, there's a TweetMeme widget to the right. It's pretty convenient, as it allows any user to post your content to Twitter. And you get this just by installing and configuring the TweetMeme module. Ain't it cool? You get the widget located at the beginning of your node's content. Always. No matter where you really want the widget to be. No matter if you want the widget to be in any other place.

Ummm... that sounds a bit less cool.

So what? What can we do?

The plan

The plan is to render the TweetMeme widget exactly where we want. We'll first prevent the module from rendering the widget where it wants, and then we'll edit our theme to make it appear in a location of our choice.

Step 1: Deactivating the widget's rendering

First, go to the TweetMeme admin panel (Administration > Site Configuration > TweetMeme). Among the options, you can find two of most interest: Content types and Location. If you want to relocate the widget, you first must prevent the module from rendering the widget automatically, so make sure the node types and locations you want to work on are deactivated. That is, the widget should not be rendered.

Step 2: Rendering the widget where we want

Good news! The TweetMeme module does its work using a theme function of its own that we can override, so if we fire this function at the appropiate place, we'll have our stuff where we want.

In tweetmeme.module, look for function theme_tweetmeme_button(). Override this function in your theme (a tutorial on theme funcion overriding here). You're now one step from your objective.

Now, insert the following code in your theme. A good place for me is, for example, the node template (node.tpl.php).

<?php
$url = url('node/' . $nid, array('absolute' => TRUE));
echo (theme('tweetmeme_button', $url, 'normal') );
?>

This code will render the widget where we put it. Read carefully the first line of code, it's very important. That first line generates an URL. This is how the widget works: associates URLs to tweets. It knows if a content has been tweeted or re-tweeted once, twice or 100 times by "remembering" the URLs.

So, if you're using the widget to promote nodes, this line of code is valid and can be used in the node.tpl.php file. If you have other plans, such as using the widget on a View, you should consider editing this line.

Please notice: url() is a core Drupal funcion. More info about url() on Drupal API site.

OK, now for step 3. Oh, crap, there's no step 3! You're already done! Good job!

 

Think this will be very convenient,I try to figure the steps you have posted. With step 2 you just presented codes, Where do I find the other codes for other websites?

Stanley Lee from Trotinette électrique 

For the social media stuff, I abandoned all this custom made tricks, now I use "www.addthis.com" widget. It's easy, updates automatically, and it gets statistics for you.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <i> <b> <strong> <cite> <pre> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <img> <h2> <h3> <blockquote>
  • Lines and paragraphs break automatically.

More information about formatting options