How to Add Twitter to Your Drupal Site
One of the best communication tools on the Internet today for both individuals and businesses is Twitter.com. Twitter is a short form communication tool that allows you to post a message of 140 characters or less. As a member of Twitter, I can follow other users and view their updates. How does this benefit individuals or businesses? A lot of very interesting individuals are on Twitter. One way for an individual to benefit from Twitter is to use the service as a way to keep their followers updated on the latest news about themselves. You can point followers to recent blog posts or even to your latest project. Another example is Dell. Dell uses the service to keep their customers updated on the latest discounts for their products. Twitter is a great way for both individuals and businesses to leverage their brand equity.
This article will explain how to easily add your Twitter updates to your Drupal site. We will add the code provided by Twitter to a Drupal block, and then use CSS to style the block to fit our site. Our finished product will look like this:

To get started, go to http://twitter.com/badges/which_badge. We are given three choices: 1) Flash, Just Me 2) Flash, With Friends, and 3) HTML/JavaScript. Choose HTML/JavaScript:

After we select HTML/JavaScript, we are taken to the http://twitter.com/badges/html page:

On this page, the default number of updates is five. For my site, five updates are perfect. You may choose to add or subtract the number of updates depending on your style. The next thing to do is to check the checkbox that says “No Title.” We will add in our own title later, so go ahead and get rid of it here. Copy the code that is provided and move on to the next step.
The code will look similar to this:
<div id="twitter_div"> <ul id="twitter_update_list"></ul>,</div>, <script type="text/javascript" src="http://twitter.com/javascripts /blogger.js"></script>,<script type="text/javascript" src="http://twitter.com /statuses/user_timeline /Anarchtica.json?callback=twitterCallback2& amp;count=5"></script>
In the code above, Anarchtica is my Twitter username and count=5 sets the number of updates that will display on your site. You can always change this count later if you want to add or subtract the number of updates displayed on your site.
Now go to your administration page on your Drupal site. Under “Site building,” choose “Blocks”:

Click the “Add block” link or the “add block page link”:

On the “Add block” page, type in Twitter for the block’s description. Leave the “Block title” blank, we will give it a title shortly. Under “Block body,” paste in the code from the Twitter badges page.

Expand the “Input format” field, and choose Full HTML.

Under “User specific visibility settings,” choose “Users cannot control whether or not they see this block.”

Under “Role specific visibility settings,” you can leave both options unchecked.

Under “Page specific visibility settings,” choose “Show on every page except the listed pages.” Leave the “Pages” field blank.

Of course, you can modify these settings to fit your specific site.
If we were to click “Save block,” the block would look similar to this (Do not click “Save block” just yet, the following screenshot just shows what the block would look like if we did click “Save block”):

Ouch! What happened? Why are there only commas or something similar? (The background color is the background of the page, we have not edited the CSS to style the block yet).
Remember, the code we copy and pasted from the Twitter badge page looked similar to this:
<div id="twitter_div"> <ul id="twitter_update_list"></ul>,</div>, <script type="text/javascript" src="http://twitter.com/javascripts /blogger.js"></script>,<script type="text/javascript" src="http://twitter.com /statuses/user_timeline /Anarchtica.json?callback=twitterCallback2& amp;count=5"></script>
Notice the commas after the </ul> </div> and </script> tags? They do not belong in the code and we need to fix this before we click “Save block.” Scroll back up to “Block body.” Copy just this portion of the HTML/JavaScript code:
<script type="text/javascript" src="http://twitter.com/javascripts /blogger.js"></script>,<script type="text/javascript" src="http://twitter.com /statuses/user_timeline /Anarchtica.json?callback=twitterCallback2& amp;count=5"></script>
Paste that portion of the code into a text editor such as Microsoft Word so we can paste it somewhere else shortly. Now that we have done that, delete that portion of the code from the “Block body” so the “Block body” now looks like this:
<div id="twitter_div"> <ul id="twitter_update_list"></ul>,</div>,
Delete the two commas from the above code and make it look like this:
<div id="twitter_div"><ul id="twitter_update_list"></ul></div>
We originally clicked the “No Title” checkbox on the Twitter badge page so that we could add in our own title later. We will add in the title now. Do this by adding in the code between the <h2> and </h2> tags to look like this:
<div id="twitter_div"><h2 class="twitter-title">twitter</h2><ul id="twitter_update_list"></ul></div>
The twitter text between the <h2> and </h2> tags is the actual heading for our Twitter block. You can change this text fit your style.
Now you can click “Save block.”
After you click “Save block,” you will be taken to the build block page. Hover your mouse over the cross to the left of the block that says Twitter and move it into the region you would you would like the block to display in. I have placed my block in the right sidebar region:

Click “Save blocks” at the bottom of the page to save your changes.
We now need to copy and paste the JavaScript code we deleted from the Twitter block and paste it into page.tpl.php. The Twitter badges page says:

The page.tpl.php page is where the <body> tag resides, so we will paste the code there.
You can find the page.tpl.php page in the same folder as your theme. So open up your favorite FTP client and navigate to your theme’s folder. For example, if you are using the Garland theme, navigate to /themes/garland and you will see page.tpl.php. I am using the Zen theme that I have modified to be a sub-theme. Therefore, my page.tpl.php page was located in the main Zen theme directory. Since I wanted to modify the page.tpl.php page, I had to copy that file and place it into my sub-theme directory. I could then modify the file located in the sub-theme directory, and it would take precedence over the page.tpl.php page located in the Zen theme directory. Edit the page.tpl.php page by copying the code you saved earlier and pasting it right above the </body> tag so that it looks similar to this (remember to delete the comma that is located between the two <script>,</script> tags):
<? php print $closure ?> <script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script> <script type="text/javascript" src="http://twitter.com/statuses/user_timeline/Anarchtica.json?callback=twitterCa llback2&count=5"></script> </body> </html>
The text between the <script> and </script> tags is what we pasted into the page.tpl.php page. Remember to paste in your specific code and to not copy and paste my code since this has my specific Twitter information. Save this change and now go back to your site. Your Twitter block should now look similar to this:

At this point, we know that it at least works. We now need to give the block some style.
If you are using the Garland theme, use your FTP client again to go back to /themes/garland and edit the style.css file. My CSS file is aaron.css and is located in /sites/all/themes/aaron.
The four CSS pieces we need to add to our CSS file are:
div#twitter_div
{
font-size: 12px; /* use this to change the font size of the Twitter updates */
font-weight: bold; /* I made the Twitter updates font bold to make it easier to read */
border-style: solid; /* makes the border around the Twitter block solid */
border-width: 2px; /* sets the width of the border around the Twitter block */
border-color: #C5C295; /* sets the color of the border around the Twitter block */
text-align: justify; /* makes the Twitter updates text justified */
background-color: #D7D39A; /* sets the background color of the Twitter block */
}
#twitter_div ul
{
margin: 0px 10px 0px 10px; /* positions the Twitter update text 10 pixels from the right and 10 pixels from the bottom. This makes the text line up properly in the Twitter block. */
padding: 0px 0px 10px 0px; /* sets the padding of the Twitter update text to 10 pixels from the bottom */
}
#twitter_update_list li
{
margin: 10px 0px 0px 0px; /* separates the Twitter updates with a 10 pixel margin at the top between each of the updates */
padding: 0px 0px 10px 0px; /* gives a 10 pixel padding between each of the Twitter updates on the bottom */
list-style-type: none; /* gets rid of the bullet point next to each of the Twitter updates */
border-bottom: 2px solid #C5C295; /* separates each of the Twitter updates with a 2 pixel border at the bottom and gives it a color */
color: #555555; /* sets the font color for the Twitter updates */
}
.twitter-title
{
font-size: 20px; /* sets the Twitter title font to 20 pixels */
font-weight: normal; /* sets the title font weight to normal */
color: #444444; /* sets the title font color */
text-align: center; /* centers the title within the Twitter block */
background-color: #7D9C1F; /* sets the green background color for the title */
}Once you have added the above CSS code and modified any of it, go ahead and save the file and view your new Twitter block! It should look similar to this:

Congratulations! That wasn’t so hard was it? Have a question or comment? Go ahead and leave your feedback in the comments below.
Aaron
Comments
Twitter on Drupal
This was really helpful! I'm putting my site together with Drupal and I spend a lot of time googling for tutorials. There is a lot of information out there, but it's not often put together as well as your tutorial was. Much appreciated!
Colin
Thanks
Thanks Colin. I know exactly what you're talking about.
Aaron
Title
Thank you for your tutorial. Is there any possibility to add an link to the Title in exchange for the "follow me on Twitter link"?
Best regards,
Mike
Reply
Hello Mike,
It looks like Twitter has added the "follow me on Twitter" part since I wrote this tutorial.
Here are the changes to remove the "follow me on Twitter," and then make the title a link to your Twitter page:
To remove the "follow me on Twitter," just delete this code from the Twitter badge code:
To have the title be a link to your Twitter page, change your Twitter block's body to this:
Thanks,
Aaron
Title
Happy new year Aaron,
and thanks a lot for the fast reply. I tryed to add an a-tag to the blocktitle but the lenght is limited. Got no clue, even after i searched a dozen of threads, to manipulate this limit. Do you got an idea, because if i add the title in the blockbody i got to laborious edit the even laborious coded css. I'm not that professional in coding as i whish to be …
Cheers,
Mike
Reply
Hi Mike,
You have to put the code in the block body. I understand it's a pain to code even more CSS, but in order to fully customize the Twitter block, it is absolutely necessary. I have done most of the work for you by providing all of the CSS elements. All you have to do is copy and paste the CSS code and then edit the variables to fit the style of your website.
Go back to the tutorial and find the CSS code. These are the main CSS elements:
div#twitter_div
#twitter_div ul
#twitter_update_list li
.twitter-title
Then follow the comments in the CSS code to match your site's style.
Thanks,
Aaron
Twitter as a Drupal module?
I wonder if I can turn this into a custom module. That takes care of most of the things and leave the rest to the theme.
I would especially want this since that would avoid having to add the "script" part of the code in every theme for the site. Perhaps we have to load it into the $closure variable.
Reply
Hello Vyoma,
I tried a couple of the Twitter modules available at first. The problem I had with them was that I could not stylize the module to fit my theme. That is why I dropped the custom module approach and did it as a block instead.
But if you know how to write a custom module to do this, that would be awesome!
It's been a while since I tried those modules though, so they may have been updated by now or a better one has come along.
Thanks,
Aaron
Twitter Block - Custome Drupal 6 Module created
I just finished creating the custom module.
Download it from: http://kmaheshbhat.com/blog/twitter-block-drupal-module
Great job
That's the reason why i love open source. You're great ...
Thanks a lot,
Mike
Twitter Module for drupal 6
The Twitter module for Drupal 6 uses Views2 to display sidebar blocks, pages of tweets, and other nice bits. If you aren't already using Views 2 it may be a hassle to install, but it does give you quite a bit of flexibility. It also means that if you use multiple twitter accounts, the tweets from them can be combined into a single stream easily.
http://img.skitch.com/20090116-xp6mbf4y77djjh6ch4rm8bj3u.png shows a screenshot of it in action. I wanted something that was very streamlined...
Great Tutorial!
Thanks Aaron! This is just what I was looking for!
Thanks
Thanks Matt, glad I could be helpful.
Aaron
Twitter on Website
I've added twitter onto my homepage but am having trouble customising it - each link and timestamp is on a separate line (see www.takeonafrica.com). Is there any way to have them inline after the post, to save space.
It must be to do with the styling but I can't figure it out - any suggestions?
Thank you.
(I know I don't use Drupal, but the theory's the same I believe).
Reply
Hello Helen,
What an amazing bike expedition through Africa you're about to do!
The problem is coming from your style.css file on line 427:
#sidebar .menu ul li a, .tabbed div ul li a { display: block; padding: 2px 5px; }The "display: block" is what is causing your problems.
If you add this line below line 427, it will fix the links showing on separate lines:
#sidebar .menu ul li a { display: inline; }I also gave you some extra styling, you can add this below too:
#twitter_update_list li { margin: 0px 0px 10px 0px; /* separates the Twitter updates with a 10 pixel margin at the bottom of each of the updates */ padding: 0px 0px 10px 0px; /* gives a 10 pixel padding between each of the Twitter updates on the bottom */ border-bottom: 2px solid #D7CEC2; /* separates each of the Twitter updates with a 2 pixel border at the bottom and gives it a color */ }This styling separates each "tweet" and gives it a border.
Here is a screenshot I took of the changes:
If you need any additional help, feel free to ask me.
Aaron
Thanks
Thanks Aaron,
I figured it had something to do with the display:block property but couldn't get it right.
Changes made as you suggested and it looks perfect now!
Much appreciate your help.
Helen
Thanks, that was just the
Thanks, that was just the 'easy solution' I was looking for after fighting around with the Twitter module and Twitter feeds that need cron...
Update
hi Aaron,
All the detailed explanation was really help but the new twitter badges do not come with misplaced comma's.
I've just added a twitter block on my site {http://ashwinz.com} and didn't find any of those misplaced comma's from twitter on their badge. So maybe you should consider updating your post.
Just a copy-paste of badge followed by a cut-paste of the script lines did the work for me.
Thanks
Ashwin
twitter module
Hi Aaron,
Twitter tutorial is helpful for the starters like me.
I am new to PHP & drupal.
I wanna use twitter to get the total number of tweets for a particular blog user in my site.it should show the number of tweets in a small block.
can you Please tell me how to do that?
I don't have a twitter account. Are status messages and tweets same?
Please post a tutorial to have an idea of what it is?That helps people like me alot!
Thank you.
Where are my white asterisks coming from?
I can't figure out where the asterisks are coming from in my twitter box on http://www.PalestinePosterProject.org
1. They're not in the source code (or page.tpl.php)
2. The CSS includes list-style-type: none;
for #twitter_update_list li
3. There are no asterisks in the original twitter posts
Still, little white asterisks are showing up for each post in the twitter block. Any ideas where they might be coming from... and how to take them out?
thanks
thank you very much for the tutorial .... very helpful specially for a new drupalist :)
thx bro... very helpfull..
thx bro... very helpfull..