0

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>';

?>
2

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 :)

  • 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
2

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.

  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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