Apr 9th, 2014
I just read a great article on this that breaks things down very well. You can find the original article here: http://fredparke.com/blog/create-your-own-tokens-drupal-7
Basically, you need to use two hooks. The first one tells the system about your token. The second one contains the specific logic for your token.
<?php /** * Implements hook_token_info(). */ function module_name_token_info() { $info = array(); // Define a new token type. $info['types']['my_custom_tokens'] = array( 'name' => t('My custom tokens'), 'description' => t('A token type for my custom tokens.'), ); // Define any new tokens. $info['tokens']['my_custom_tokens']['my_cool_token'] = array( 'name' => t('My cool token'), 'description' => t('A token I use to show that I am cool.'), ); return $info; } /** * Implements hook_tokens(). */ function module_name_tokens($type, $tokens, array $data = array(), array $options = array()) { $replacements = array(); if ($type == 'my_custom_tokens') { // Loop through the available tokens. foreach ($tokens as $name => $original) { // Find our custom tokens by name. switch ($name) { case 'my_cool_token': // Work out the value of our token. $value = 'Whatever we want the token to be'; // Give our token it's value! $replacements[$original] = $value; break; } } } return $replacements; }