How to Create Custom Tokens for Twitter

Last updated on
17 August 2018

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

How I created custom tokens of the Twitter API user information that is stored in the twitter_account table.

I am launching a site with a heavy Twitter focus using the Twitter module for user registration. The Twitter module does an avid job of authenticating but lacks a lot of function in integrating the data that is provided by the Twitter API that the module stores in the 'twitter_account' table. I would hope that the maintainers would mature the module to have a field mapping/merging feature similar to the Janrain/RPX module. This should include pulling in the user photo (I'll try to post how I cobbled together a solution for photos later) and tokenizing all user fields like the full name, description/bio, location, etc.

Views pulls in this information which works well for various page and block views but I wanted to inject the user details in the meta tags and page titles for SEO and search purposes. This is the recipe that I am using but please let me know if there's a better way.

Modules Used:

Install, enable, yada yada...

The heavy lifting is done by the sweet Custom Token module. It has a great UI. Simply go to admin/structure/token-custom and select +Add token. Enter a name and description for your new token. Select a Token type (Tip: The module will let you define new token types which are just new titles at the top of the collapsable groups.). Paste in some PHP, HTML, or whatever. (I've included the PHP I'm using below). BE SURE to select the appropriate Text Format, in our case the 'PHP Code' selection. Hit save and enter in to token bliss.

In the Metatag, or any other area that uses tokens, expand the token type you specified and say hello to you new token friends. These are now just ordinary tokens that can be used and abused.

I've included some screen shots for you. Let me know how this works out for you. Enjoy!

Code Snips:

Token for pulling the full Twitter user name from the 'twitter_account' table:

   $uid = arg(1);
   $nameSQL = "SELECT name FROM twitter_account WHERE uid=:uid";
   $result = db_query($nameSQL, array(':uid' => $uid));
    foreach ($result as $row){
   $name = $row->name;
 }
   return $name;

Token for pulling the description from the 'twitter_account' table:

   $uid = arg(1);
   $descSQL = "SELECT description FROM twitter_account WHERE uid=:uid";
   $result = db_query($descSQL, array(':uid' => $uid));
    foreach ($result as $row){
   $desc = $row->description;
 }
   return $desc;

Pulling the location...

   $uid = arg(1);
   $locSQL = "SELECT location FROM twitter_account WHERE uid=:uid";
   $result = db_query($locSQL, array(':uid' => $uid));
    foreach ($result as $row){
   $location = $row->location;
 }
   return $location;

You get the idea. Remember these are only available on pages that pass the uid in the path.

(This documentation was originally provided by InTheLyonsDen #1416570: How to: Create Custom Tokens for Twitter)

Help improve this page

Page status: No known problems

You can: