Among all, WordPress Dashboard shows most recent updates and activity at your website. It’s nice to have useful things at hand, so let’s expand this functionality a little and create a custom widget for the WordPress Dashboard.

Today we’ll create a custom widget which displays the latest tweets from a custom user.
To do this, we use the wp_add_dashboard_widget function to register a new dashboard widget for your blog.

wp_add_dashboard_widget(
    $widget_id,
    $widget_name,
    $callback,
    $control_callback = null
)

Getting Started

Let’s add a simple new widget to WordPress dashboard. First, open your plugin or functions.php file and write something like this:

add_action( 'wp_dashboard_setup', 'twitter_dashboard_setup' );
function twitter_dashboard_setup() {
    wp_add_dashboard_widget(
        'twitter-dashboard-widget',
        'Recent Tweets',
        'twitter_dashboard_content',
        $control_callback = null
    );
}

We set the function twitter_dashboard_setup that adds a widget with twitter-dashboard-widget id, and name it “Recent Tweets”. Then we hook it into the dashboard setup process, where twitter_dashboard_content is the callback function with actual contents of our widget. Here’s what the function looks like:

function twitter_dashboard_content() {
    echo 'Hello, World!';
}

There – we just told twitter_dashboard_content() function to say “Hello, World!”. And that’s it! Go to you admin main page – you can see our new widget at the bottom of the page. Just drag it around and place wherever you want!

Output latest tweets

We just created a dashboard widget – now let’s make it show something useful! Specifically, the latest tweets from custom user. Do do this, we will use GET statuses/user_timeline API from Twitter, to load tweets, and twitter-provided script and render them on the widget.

To put these scripts in WordPress, we will use admin_enqueue_scripts action hook to target our scripts to main WordPress admin page. Let’s write a function with hooks, that checks current page and loads scripts only when needed.

add_action( 'admin_enqueue_scripts', array( &$this, 'dashboard_engine' ) );
function twitter_dashboard_engine( $hook ) {
    if( 'index.php' != $hook )
        return; 

    wp_enqueue_script(
        'twitter_blogger',
        'http://twitter.com/javascripts/blogger.js',
        null, null, true
    );
    wp_enqueue_script(
        'twitter_user_timeline',
        'http://twitter.com/statuses/user_timeline/themefm.json?callback=twitterCallback2&count=3',
        null, null, true
    );
 }

Of course, you can change themefm to whatever you want ;) Also, you can set the number of output tweets by changing count number. Now we need to rewrite twitter_dashboard_content() function with specified in twitter-provided script structure. Exactly, we need to specify ul list with twitter_update_list id. Below, you can find the final version of the code listing.

add_action( 'wp_dashboard_setup', 'twitter_dashboard_setup' );
add_action( 'admin_enqueue_scripts', 'twitter_dashboard_engine' );
function twitter_dashboard_setup() {
    wp_add_dashboard_widget(
        'twitter-dashboard-widget',
        'Recent Tweets' ,
        'twitter_dashboard_content',
        $control_callback = null
    );
} 

function twitter_dashboard_content() {
    echo '<ul id="twitter_update_list"><li>&nbsp;</li></ul>';
} 

function twitter_dashboard_engine( $hook ) {
    if( 'index.php' != $hook )
        return; 

    wp_enqueue_script(
        'twitter_blogger',
        'http://twitter.com/javascripts/blogger.js',
        null, null, true
    ); 

    wp_enqueue_script(
        'twitter_user_timeline',
        'http://twitter.com/statuses/user_timeline/themefm.json?callback=twitterCallback2&count=3',
        null, null, true
    );
}

Conclusion

Congratulations – you’ve just created your own WordPress dashboard widget! Thank you for reading, I hope you like this tutorial and everything was clear enough and easy to understand. Feel free to ask questions in comments, and don’t forget to fan our page on Facebook!