Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm getting a problem displaying tweets that include reply's (ie when you @username someone). It's showing the @ symbol but then cutting off the rest of the tweet. (Check out www.teamworksdesign.com in the bottom right hand corner)

Can anyone help with this or suggest a good plugin?

<?php

function parseTweet($text) {
$pattern_url = '~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)?@)?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i';
'@([A-Za-z0-9_]+)';
$tweet = preg_replace('/(^|\s)#(\w+)/', '\1#<a
href="http://search.twitter.com/search?q=%23\2″ rel="nofollow">\2</a>', $text);
$tweet = preg_replace('/(^|\s)@(\w+)/', '\1@<a
href="http://www.twitter.com/\2″ rel="nofollow">\2</a>', $tweet);
$tweet = preg_replace("#(^|[\n ])(([\w]+?://[\w\#$%&~.\-;:=,?@\[\]+]*)(/[\w\#$%&~/.\-;:=,?@\[\]+]*)?)#is", "\\1<a
href=\"\\2\" title=\"\\2\" rel=\"nofollow\">[link]</a>", $tweet);
return $tweet;
}

$username='teamworksdesign'; // set user name
$format='json'; // set format
$tweet=json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}")); // get tweets and decode them into a variable

$theTweet = parseTweet($tweet[0]->text);
$newTweet = substr($theTweet,0,65);
echo '<a class="tweet" rel="nofollow" href="http://www.twitter.com/teamworksdesign"> "' . $newTweet . '..."</a>';

?>
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Hi Rob@:try with this plugin Tweet This.you can set your custom image also.:)

or

Try this code...

Open the sidebar.php Locate the “twitter-feed” div

<div class=”twitter-feed”>
<h2>What I’m up to…</h2>
<ul id=”twitter_update_list”></ul>
<script type=”text/javascript” src=”http://twitter.com/javascripts/blogger.js”></script>
<script type=”text/javascript” src=”http://twitter.com/statuses/user_timeline/JunLoayza.json?callback=twitterCallback2&amp;count=1″></script>
<div class=”follow-on-twitter”>
<a rel=”nofollow” href=”">Follow me on Twitter</a>
</div>
</div>

Replace with Twitter username

<script type=”text/javascript” src=”http://twitter.com/statuses/user_timeline/AddYourUsernameHere.json?callback=twitterCallback2&amp;count=1″></script>

Now you will want to add your twitter URL to the “Follow me on Twitter” link. The following code is listed in blue above:

<a rel=”nofollow” href=”http://Twitter.com/AddYourUsernameHere“>Follow me on Twitter</a>

The final code

<div class=”twitter-feed”>
<h2>What I’m up to…</h2>
<ul id=”twitter_update_list”></ul>
<script type=”text/javascript” src=”http://twitter.com/javascripts/blogger.js”></script>
<script type=”text/javascript” src=”http://twitter.com/statuses/user_timeline/AddYourUsernameHere.json?callback=twitterCallback2&amp;count=1″></script>
<div class=”follow-on-twitter”>
<a rel=”nofollow” href=”http://Twitter.com/AddYourUsernameHere“>Follow me on Twitter</a>
</div>
</div>

save and run :)

share|improve this answer
    
Thanks for the reply. The reason I went with the code above was to strip all the unecessary stuff out, will that plugin allow me to just display the tweet and nothing else? –  Rob Jul 5 '11 at 14:21
add comment

That is one nasty regexp. I would suggest looking into Twitter entities instead, much easier than trying to parse it yourself.

And this really has little to do with WP and more suitable for other sites in network.

share|improve this answer
    
Cheers, shall try it out. p.s The reason it's on here is if there's a good working plugin then I'll use it instead. –  Rob Jul 5 '11 at 14:27
    
@Rob then update your question to mention that alternatively you are looking for a plugin and what that plugin should be able to do. –  Rarst Jul 5 '11 at 14:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.