Fixing a Twitter WordPress Plugin with jQuery
I use the fantastic Twitter for WordPress plugin on my website. Sometimes though, Twitter decides that it doesn’t want to make my tweets available. The plugin has accounted for this, and it displays No public Twitter messages when this happens. That kinda sucks though.
It’s definitely nice that the plugin doesn’t throw an error, but I really don’t want that message displayed on my site. So what’s the solution? jQuery of course.
When the Tweet Comes Through
Here is the markup that is displayed on my site when a tweet comes through successfully:
<div id="twitterContainer">
<a class="twitter-link" href="http://twitter.com/davist11/status/1200405534">
I feel like I'm in an episode of Seinfeld right now.
</a>
</div>
When the Tweet Doesn’t Come Through
This is what you end up with:
<div id="twitterContainer">
No public Twitter messages.
</div>
The jQuery
My thinking is that I will check the contents of div#twitterContainer to see if it is No public Twitter messages. If it is, I think it would be useful to replace that text with a link to my Twitter account with a message telling people to follow me.
So first, let’s execute the jQuery code when the document is loaded:
$(document).ready(function() {
});
Next, we are going to check and see if the contents of div#twitterContainer is No public Twitter messages:
$(document).ready(function() {
if(jQuery.trim($('div#twitterContainer').text()) == 'No public Twitter messages.') {
}
});
Finally, let’s just replace this text with a link to my Twitter feed:
$(document).ready(function() {
if(jQuery.trim($('div#twitterContainer').text()) == 'No public Twitter messages.') {
$('div#twitterContainer').html('<a href="http://twitter.com/davist11" class="twitter-link">Follow Me on Twitter</a>');
}
});
That’s it. This is just another example of how I have used jQuery to improve a WordPress plugin on my site.
You can see this in action on my site, or take a look at the demo.








Great post. I started using twitter and I can’t seem to stop, its a great tool for communication with your blog users. Feel free to follow me http://www.twitter.com/mattwaterman
Nice idea :P
Well, it’s easier if you just edit the plugin and search for “No public Twitter messages” and change it inot whatever you want so you just don’t need jquery post-processing and you acn do it even if you have no idea of php.
@Astroboy-
Yes, it’s definitely easier to just edit the plugin, but then you would have to edit the plugin every time there was an update.
Thx. All the jQuery stuff is way beyond me, but because of your post I managed to edit the plugin so a more friendly message is displayed.
V. happy.
SC
Why not just edit the plugin source?
@Scarf*oo-
Because anytime to plugin is updated, I would have to make my change again.
@Trevor – Agree, but still relying on jQuery to modify html seems a crude – albeit a valid way to solve the problem.
@Scarf*oo-
To each his own.
If you prefer editing the plugin source, then that sounds like the best solution for you.
What page are you guys looking at?
I’m looking at the twitter.php but it doesn’t have this code you guys are posting
it has
if ( empty($messages->items) ) {
if ($list) echo ”;
echo ‘No public Twitter messages.’;
adding this code worked for me
//define(‘MAGPIE_CACHE_AGE’, 120);
define(‘MAGPIE_CACHE_ON’, 0); //2.7 Cache Bug
define(‘MAGPIE_INPUT_ENCODING’, ‘UTF-8′);
sorry
that was the code that was already there
i modified it to this
define(‘MAGPIE_CACHE_AGE’, 240);
// define(‘MAGPIE_CACHE_ON’, false); //2.7 Cache Bug
define(‘MAGPIE_INPUT_ENCODING’, ‘UTF-8′);
Just wondering, I like the idea to display a link to the twitter account, but how about when twitter feed is read in and isn’t empty, 1) the messages are shown as per normal and 2) the formatted messages are written to a text file, so when the function is called and the feeds returns empty, the file is included instead and shows the messages up to the point where it then returned blank.
I’m still new to this Wordpress lark, but for me it makes sense to try and ensure some kind of continuity remains even though to an extent the feed and the text file might be briefly out of sync with each other.
@int3rc3ptor-
I’m not sure what you are asking. Can you explain in more detail?
I’m having this problem of no messages, but instead of jQuery I just put a link to follow me on twitter just above where the messages would display so that it’s always there…..
But I like int3rc3ptor’s idea…..If I understand correctly, he/she is referring to capturing the incoming stream each time a refresh is done to a text file, then if the “no public messages” is encountered, displaying the text file (with the last known messages) instead…….
Now if you can write some jQuery to do THAT, it would be an elegant solution indeed!