@monicadavisf ohh, he was Lucas? I was trying to remember what I had actually seen him in, but didn't care enough to go to imdb.

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>');
	}
});
Share This:
  • del.icio.us
  • Digg
  • Twitter
  • Facebook
  • StumbleUpon
  • Google Bookmarks
  • NewsVine
  • Technorati
  • Reddit
  • LinkedIn

RSS feed of comments for this entry

  1. 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

  2. Nice idea :P

  3. 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.

  4. @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.

  5. 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

  6. Why not just edit the plugin source?

  7. @Scarf*oo-
    Because anytime to plugin is updated, I would have to make my change again.

  8. @Trevor – Agree, but still relying on jQuery to modify html seems a crude – albeit a valid way to solve the problem.

  9. @Scarf*oo-
    To each his own.

    If you prefer editing the plugin source, then that sounds like the best solution for you.

  10. 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.’;

  11. 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′);

  12. 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′);

    • int3rc3ptor
    • 8.26.2009 at 12:10 pm
    • #
    • Reply »

    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.

  13. @int3rc3ptor-
    I’m not sure what you are asking. Can you explain in more detail?

  14. 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!

    • 2.12.2009 at 10:35 am
    • #
    • 2.12.2009 at 11:06 am
    • #
    • 2.12.2009 at 12:28 pm
    • #

Speak Your Mind

* Denotes Required Field

  1. To post code snippets, use <pre><code>YOUR CODE HERE</code></pre>
Me, Trevor Davis. My blue steel face…

Hi, I’m Trevor Davis

I’m a 25 year old Front-End Developer living in Arlington, VA. I work full-time at Viget Labs and freelance on the side.

I specialize in CSS, HTML, jQuery, WordPress & ExpressionEngine. See more of my work, and then hire me.

Recent Work

  • 4th District IBEW Health Fund
  • Employers Council on Flexible Compensation
  • Monica Davis
  • The National Christmas Tree
  • Matrix Group International
  • The MatriX Files
  • Wireless Career
  • George Washington Wired
  • Direct Selling 411
  • Makeup Bizz
  • InstallNET
  • National Park Foundation
See More of My Work

Asides