Hello!
I’m of the opinion that it is better to code your own functions, shortcode and templates to accomplish even simple things within content management systems, as opposed to downloading a free plugin to accomplish the same task.
You might be asking “Why not just download a plugin? Its much easier!”. Well that may be true in the short term, but there are many reasons why investing a little bit of extra time to develop a solution in wordpress yourself is better in the long run. I’ll go into the reasoning first and then I’ll get into how we created our own shortcode to pull tweets from a twitter user.
Security
Security is more important than anything else. When you use someone else’s WordPress plugin to accomplish a task, you are putting all your faith and trust in that developer’s ability to develop a solution that adheres to security best practices. Not only that, any time a new security advisory comes out you are trusting that that developer will quickly adapt and push out respective fixes without waiting too long. Many developers in the WordPress community are great and adhere to these standards, however there are some that for whatever reason may have abandoned the project or are busy with other things that can’t accommodate as easily.
If you develop the solution yourself then you only have yourself to answer to.
Customization
Developing something from scratch gives you the ability to customize it yourself 100%. Third party plugins sometimes include their own jQuery libraries, CSS stylesheets and other similar files that are very difficult to customize unless the plugin author gives you the ability to customize certain elements. Even when they give you the ability to customize things, it might not be everything. This leaves you having to juggle overwriting CSS declarations and jQuery statements in order to get something to look and perform exactly how you want.
Now, to the actual code! We wanted to create a custom shortcode that takes in 3 options : 1) Number of tweets to pull, 2) Username of tweets to pull and 3) The heading title of the twitter feed that you want to display.
To actually interact with twitter, we first had to make a developer account and create a twitter “app” for our code to be able to interact with twitter’s API. Head on over to Twitter’s developer site to register.
Next, we found a twitter PHP library in order to interact with Twitter and pull the information we need. There’s no need to re-invent the wheel as many very smart people have already done that work for us. We Chose TweetPHP.
What you’ll want to do is open the functions.php in your WordPress theme and load the TweetPHP library file :
/**. * Load Twitter Feed library */ require_once get_template_directory() . '/twitter/TweetPHP.php';
Once the library is loaded in your functions php, you can go ahead and start using it! We’ll want to create shortcode that takes the option arguments outlined above, and pulls the respective feed!
function shift8_twitter_shortcode($atts){ extract(shortcode_atts(array( 'number' => '3', 'username' => 'username', 'headingtitle' => 'Latest Tweets' ), $atts)); $TweetPHP = new TweetPHP(array( 'consumer_key' => '', 'consumer_secret' => '', 'access_token' => '', 'access_token_secret' => '', 'twitter_screen_name' => $username, 'enable_cache' => false, 'tweets_to_display' => $number, 'twitter_template' => ''.$headingtitle.'
- {tweets}
So in the above function (still in functions.php), we are declaring the options for the shortcode and the default variables that will be set if they are never defined. The next block of code initiates a new TweetPHP array with the variables that you can customize further if needed. There are many more options you can change, but these are whats important to us. For consumer_key, secret and the access tokens, this is what you would generate when registering your app on Twitter’s developer site.
For the twitter & tweet templates, you can customize how the tweets will look 100%. This is the best part, because this allows you to manipulate everything and integrate it into your website however you want. You could even take shortcode options and pass them to style the tweets to give the shortcode even more flexibility. For this demonstration, we simply define the template HTML / CSS in the code options for the TweetPHP array.
Next we simply need to execute WordPress’ add_shortcode function :
add_shortcode('shift8_twitter', 'shift8_twitter_shortcode');
Once the above code is added to functions.php you can then start actually using the shortcode!
[shift8_twitter number="5" username="shift8web" headingtitle="Latest Tweets from Shift8"]