Creating a Simple Twitter Plugin for WordPress

Creating a Simple Twitter Plugin for WordPress

Tutorial Details
  • Program: WordPress
  • Version: 3.2+
  • Difficulty: Easy
  • Estimated Completion Time: 10 minutes

Let’s see how can we make a very simple plugin showing some latest posts from a Twitter account.


Step 1 Download Scripts

Before we begin to write our plugin, we need some JavaScript code.


Step 2 Create File Structure and Copy Files

Create this directory: /wp-content/plugins/tweetfeed-light, and then copy these files.

/css
	style.css

/img
	buttons.png
	interface.png
	interface_dark.png
	twitter_bird.png

/js
	jquery.tweetable.min.js
	jquery-1.7.2.min.js

Step 3 Basic Plugin Data

Continue with creating tweetfeed-light.php (our main plugin file name) with the given content below.

/*
Plugin Name: Tweetfeed Light
Plugin URI: http://wp.tutsplus.com
Description: Show latest Tweets in sidebar for a given Twitter user 
Version: 1.0
Author: Adam Burucs
Author URI: http://wp.tutsplus.com
*/

Step 4 The Plugin Class

The basic declaration of our plugin class.

class AB_Tweetfeed_Light {
}

Step 5 Constructor Function

It is a good idea to put the initial settings and requirements into this function. In this section we set the following:

  • plugin path
  • shortcode
  • importing scripts
  • importing styles

The code for these tasks:

public function __construct() {
	// set plugin path
	$this->pluginUrl = WP_PLUGIN_URL . '/tweetfeed-light';

	// set shortcode
	add_shortcode('tweetfeed-light', array($this, 'shortcode'));

	// import scripts
	wp_enqueue_script('tweetable-script',   $this->pluginUrl . '/js/jquery.tweetable.min.js', array( 'jquery' ));

	// import style
	wp_enqueue_style('tweetable-style',     $this->pluginUrl . '/css/style.css');
}

Step 6 Retrieving Tweets

Get the latest tweets from a user. We can also set the limit variable controlling the number of tweets.

public function loadTweets($user, $limit) {

	// render tweets to div element
	echo '<div id="tweets"></div>';

	// render javascript code to do the magic
	echo
	'<script type="text/javascript">
	jQuery(function(){
	jQuery("#tweets").tweetable({
	username: "' . $user . '",
	limit: ' . $limit . ',
	replies: true,
	position: "append"});
	});
	</script>';

}

Step 7 Shortcode Function

This is the helper script for using the plugin with a shortcode.

// render tweets with shortcode
public function shortcode($data) {
	return $this->loadTweets($data['username']);
}

Step 8 Instantiate Class

Make an object from the plugin class.

// run plugin
$tweetfeed_light = new AB_Tweetfeed_Light();

Step 9 Final Code

Here is how the code looks when it is finished.

/*
Plugin Name: Tweetfeed Light
Plugin URI: http://wp.tutsplus.com
Description: Show latest Tweets in sidebar for a given Twitter user 
Version: 1.0
Author: Adam Burucs
Author URI: http://wp.tutsplus.com
*/

class AB_Tweetfeed_light {
public function __construct() {
	// set plugin path
	$this->pluginUrl = WP_PLUGIN_URL . '/tweetfeed-light';
	
	// set shortcode
	add_shortcode('tweetfeed-light', array($this, 'shortcode'));
	
	// import scripts
	wp_enqueue_script('tweetable-script',   $this->pluginUrl . '/js/jquery.tweetable.min.js', array( 'jquery' ));
	
	// import style
	wp_enqueue_style('tweetable-style',     $this->pluginUrl . '/css/style.css');
}

public function loadTweets($user, $limit) {

	// render tweets to div element
	echo '<div id="tweets"></div>';

	// render javascript code to do the magic
	echo
	'<script type="text/javascript">
	jQuery(function(){
	jQuery("#tweets").tweetable({
	username: "' . $user . '",
	limit: ' . $limit . ',
	replies: true,
	position: "append"});
	});
	</script>';

}

// render tweets with shortcode
public function shortcode($data) {
	return $this->loadTweets($data['user'], $data['limit']);
}
}

// run plugin
$tweetfeed_light = new AB_Tweetfeed_Light();

Step 10 Shortcode Usage

To use this plugin you can write the [tweetfeed-light user="johnb" limit="10"] shortcode into the page source you want. For example:

...
<div class="menu">...</div>
[tweetfeed-light user="johnb" limit="10"]
<div class="footer">...</div>
...

Step 11 The Look

Here is how the plugin looks in the default WordPress theme inserted into a page object.

The look


Summary

As you can see this is a simple, but great solution for our mini Twitter mission. For further (color) tweaking you should look into the included stylesheet. Thanks to Icontexto for the Twitter picture!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://lukespoor.com/ Luke S

    Any chance of an extension to this tutorial by explaining how to use this as a widget?

    • http://www.abditive.in/ Jayant Tyagi

      Try using a text widget and paste the short code in it and save. i think it would work.

      • http://lukespoor.com/ Luke S

        I’d like to know how to make it a widget so I can use it in commercial themes :)

  • http://kingofpunk.net King of Punk

    I think if you add this line to your function.php it will work in your widgets

    add_filter(‘widget_text’, ‘do_shortcode’);

  • http://profiles.wordpress.org/stephenh1988/ Stephen Harris

    Nice tutorial :). A couple of things though: you should use plugins_url instead of WP_PLUGIN_URL. Also you’ll want to sanitize the variables $user and $limit before echo-ing them inside the <script> tags, and the loadTweets function should return rather than echo.

    It would be better to call wp_enqueue_script and wp_enqueue_style inside your shortcode callback so as to only load when the script is needed.

    I like the structure of the plug-in (and wrapping it in a class): it makes it much easier to adapt the class to create a widget (without replicating code) as per Luke’s request :)

    • dj

      Woah… For some of us who are still a bit “WP Challanged” – can anyone help with code alterations that make these possible? It seems, from what you seem to be saying, that there are quite a few “errors” (or at least non-best-practices) in the original code.

  • Ankur

    For line 32 – 40 in the final code step 9 – is it not recommended to use esc_js? http://hitchhackerguide.com/2011/02/11/esc_js/

  • http://mifasm.com Mifas

    Thanks for the article. But why wp.tutsplus does not provide video tutorials?

  • Pingback: Creating a Simple Twitter Plugin for WordPress | Qtiva

  • Pingback: Creating a Simple Twitter Plugin for WordPress | Shadowtek Hosting and Design Solutions

  • Jeff

    When I activate the plugin, it turns all of my links links white in the admin screen. Also, how can we display the date/time of the tweet?

    Thanks.

    • Kyle

      @Jeff, same here.

      When the plugin is activated, it seemingly reverts back to an old UI of the Dashboard and all of the links within the Admin screen…weird.

  • http://www.edutechnology.net Rafi

    Thanks for sharing the tutorial. However i tried to make the plugin but i failed. When i tried to installed it says “plugin could not be installed”

  • Pingback: WordPress 3.3.2 is out | Open Knowledge

  • http://www.onestopgoa.com John D’sa

    Nice explaination. Will try building this plugin for sure

  • Pingback: Tweet-Parade (no.17 Apr 2012) | gonzoblog.nl

  • Pingback: WordPress Community Roundup for the Week Ending April 28 - Charleston WordPress User Group

  • Pingback: membuat twitter client sendiri untuk wordpress | Ysupr's Note

  • http://www.bloggerbelog.com Aditya Subawa

    nice toturial, would be more interesting if it does not use the shortcode function. But, Overall u have a big thumb from me :D

  • http://www.sufalamtech.com/ Web Development Company

    I cannot find any issues since its working fine on the demo site. Can u please send a screenshot of what you get ?
    If possible check this plugin on a fresh WordPress installation.