Quick and dirty wordpress to twitter feed with images

I wanted a really quick and dirty implementation of a wordpress blog post to twitter feed and came up with the following. i didn’t even bother to implement this as a plugin (although you could quite easily and this is really where it belongs). But just as a proof of concept i added the code to the functions.php file in my theme

The basic principle is to hook the new post event which is
add_action( 'publish_post', 'post_published_notification', 10, 2 );

This is fired every time a post is published so you can then feed the data on to twitter. I also wanted to send embedded images to twitter, so i used the posts thumbnail as the image source. For the engine to actually speak to twitter i used codebird.

I’ve broken it down into 3 functions the first one is substrwords() which is a helper function that takes a string and ensures it is no longer than $maxchars but terminates on a word boundry.
Next we have twitterpost() this does the actual post to twitter. Ensure you set your twitter API keys and secrets for this to work. The final function is the post_published_notification() function. This is what gets fired from the action callback. It gets the post and finds the attached image. Really it should check that an image is attached and not blindly assume that it is. But i will leave this as an exercise for the reader to implement.

The actual implementation looks something like :-

require_once('codebird.php'); < /code>

function substrwords($text, $maxchar) {
    $end = '...';
    if (strlen($text) > $maxchar || $text == '') {
        $words = preg_split('/\s/', $text);
        $output = '';
        $i = 0;
        while (1) {
            $length = strlen($output) + strlen($words[$i]);
            if ($length > $maxchar) {
                break;
            } else {
                $output. = " ".$words[$i];
                ++$i;
            }
        }
        $output. = $end;
    } else {
        $output = $text;
    }
    return $output;
}

function twitterpost($url, $posttxt, $imglink) {

    $consumerKey = "";
    $consumerSecret = "";
    $accessToken = "";
    $accessTokenSecret = "";

    $posttxt = $url." ".substrwords($posttxt, 114);

    \Codebird\ Codebird::setConsumerKey($consumerKey, $consumerSecret);
    $cb = \Codebird\ Codebird::getInstance();
    $cb->setToken($accessToken, $accessTokenSecret);

    $params = array(
        'status' => $posttxt,
        'media[]' => $imglink
    );
    $reply = $cb->statuses_updateWithMedia($params);
}

function post_published_notification($ID, $post) {
    $type = get_post_type($post);

    if ($type != "post") {
        return;
    }

    $title = $post->post_title;
    $permalink = get_permalink($ID);
    $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
    $imgurl = $image[0];

    twitterpost($permalink, $title, $imgurl);
}
add_action('publish_post', 'post_published_notification', 10, 2);

Only 1 comment left Go To Comment

  1. RobinC / Post Author

    I’ve now changed the action hook to

    add_action( ‘wp_insert_post’, ‘post_published_notification’, 10, 2 );

    As the hook was not firing from posts I was adding programaticly using wp_insert_post() API function. It did work fine for posts from the admin interface however. The change passes a new parameter to the callback so that needs to look like :-

    function post_published_notification( $ID, $post, $update ) {

    as $update is also set to indicate if this is a new post or an edit.

Leave a Reply