Customizing Twitter Sharing with AddThis and Drupal

Dev

Customizing Twitter Sharing with AddThis and Drupal

by Chris Armstrong
//

If you missed my intro into getting AddThis installed and configured, you can view it here.

Requirements

Optional

After getting AddThis up and running (see previous post), you can customize AddThis a bit more to give you / your end-users even more control. You can create a custom field that will be the text of the tweet instead of just the title. This is helpful when you want to your visitors to use specific hashtags or perhaps when your title is ill-fitted for Twitter.

Custom Tweet field

So, to do this, first, create a custom field in your content type. I’ve labeled mine Twitter Headline with a machine name of field_news_t (because it is going in my “news” content type).

If you’ve installed and enabled Maxlength, make sure you input 126 into the Maximum length field. You’ll use 126 (instead of 140) because the bit.ly link will use 14 characters, including the space between the title and the link.

Custom Twitter field input

Now, the code to let AddThis know that there is a custom Twitter template, you must add the following code to page.tpl.php (Drupal 6) or html.tpl.php (Drupal 7):


  var addthis_share =  {
    //…
    templates: {
      twitter = " {{url}}"
    }
  }

So, in order to get our custom field into the module, we must do a little bit before that code.

First, we have to get the field into a variable:

  $tweet = $node->field_news_t[0]['value'];

But what if you/your clients don't want to enter custom data for a particular node? Let's add a conditional:

  if($node->field_news_t[0]['value'] != NULL) { 
     $tweet = $node->field_news_t[0]['value']; 
   } else { 
     $tweet  = $title; 
   }

So now our final code looks like this:

  if($node->field_news_t[0]['value'] != NULL) { 
     $tweet = $node->field_news_t[0]['value']; 
   } else { 
     $tweet  = $title; 
    } 
 
   
    var addthis_share =  {
      //…
      templates: {
        twitter = " {{url}}"
      }
    }
 

NOTE: for certain page templates, you may need to add $node = node_load(arg(1)); before the previous code.

Upload your template, clear your cache and you should be good to go. Now, when your visitor clicks on your AddThis Twitter button, they'll see your custom tweet instead of just the title.

Custom Twitter field with AddThis

More About the Author

Chris Armstrong

Multimedia Manager
Drupal Quick Tip: Auto “node/nid” Paths from Alias in Menus “In learning you will teach, and in teaching you will learn.” Words spoken by (or, at least, attributed to) the great Phil ...
Quick Tutorial on Overriding Drupal Views using Templates Scott and I were recently invited to Oklahoma State University to provide Drupal training for their web admins. Drupal has been on the ...

See more from this author →

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072

×

Love our blog? You should see our emails. Sign up for our newsletter!