Send out custom Tweets from WordPress using Twitter Tools

In this post I will explain how you can send out custom tweets from WordPress. For this purpose, we will configure and use the functionality present in the WordPress Twitter Tools plug-in.

Get Twitter Tools

Head over to the WordPress plugin repository and grab the Twitter Tools zip file. Upload it to your /wp-content/plugins directory and unzip. Next, go to the Plugins section of your WP admin and activate the plugin.

Twitter Tools bundles a set of complementary plugins. Make sure you activate Twitter Tools – Bit.ly URLs for the URL shortening ability. We'll be using this plugin to send out custom tweets (triggered by any type of action), so we don't need the other components (Exclude Category and Hashtags).

Connect to Twitter

Follow the instructions on the Twitter Tools settings page to connect your website to Twitter. First, you need to register your site as a Twitter application. A couple of things to keep in mind:

A screenshot is worth more than 1000 words, so here's an example:

Register your Twitter Application

A couple of special strings used for authentication and API access need to be copied over to Twitter Tools:

The latter can be found by going to 'My Access Token' in the right menu:

My Access Token

Configure Twitter Tools

Remember that we only need this plugin as a support to send out our custom Tweets, so we're gonna disable all the auto-tweeting functionality. Set the following options to 'No':

That's it! Now let's get down to some coding.

Example code for a custom tweet

Suppose your website uses a Custom Post Type called 'product' and you want to send out a custom notification on Twitter each time a new 'product' is added.

First let's create a custom function that uses (or hijacks) the Twitter Tools objects to tweet out a text portion, a link and some hashtags:

<?php
function my_tweet( $args = array() ) {
  // Check reqs
  if( ! class_exists( 'aktt_tweet' ) )
    return;

  global $aktt;

  // Set up defaults
  $defaults = array(
    'text' => '',
    'url' => '',
    'tags' => array()
  );

  // Extract the arguments
  extract( wp_parse_args( $args, $defaults ) );

  // Sanity check
  if( empty( $text ) )
    return;

  // Add the URL, after shortening
  if( ! empty( $url ) )
    $text .= ' ' . aktt_bitly_shorten_url( $url );

  // Add the hashtags
  if( count( $tags ) )
    $separator = ' #';
    $text .= $separator . implode( $separator, $tags );

  // Initialize the aktt_tweet object
  $tweet = new aktt_tweet;
  $tweet->tw_text = @html_entity_decode( $text , ENT_COMPAT, 'UTF-8' );

  // And Tweet!
  return $aktt->do_tweet( $tweet );
}

Armed with this function, we'll use the wp_insert_post action hook to check when a new 'product' is added:

<?php
add_action( 'wp_insert_post' , 'my_twitter_notifications' , 99 );
function my_twitter_notifications( $post_id ) {
  $post = get_post( $post_id );
  if( $post->post_status != 'publish' ) return;

  switch ( $post->post_type ) {
    case 'product':
      // Tweet it
      $args = array(
        'text' => 'New Product: ' . $post->post_title,
        'url' => get_permalink( $post_id ),
        'tags' => array( '#awesome', '#merchandise', '#fb' )
      );

      my_tweet( $args );
    break;
  }
}

This function checks the post type and calls the my_tweet function with the appropriate arguments.