WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Twitter for WordPress] Error in hyperlink discovery (9 posts)

  1. erick_paper
    Member
    Posted 4 years ago #

    Hi.

    Your plugin has issues with discovering hyperlinks from twitter.

    This update:
    http://twitter.com/karla_porter/statuses/2652060959

    Is translated into HTML code as follows:

    <p class="twitter-message">
    The recruitersphere's Renegade of Funk <a href="http://twitter.com/sgordon70" class="twitter-user">@sgordon70</a> 12:00pm EST on
    <a href="http://twitter.com/animal" class="twitter-user">@animal</a> Recruiting Animal Show 
    
    <a href="http://<a href="http://www.recruitingshow.com" class="twitter-link">www.recruitingshow.com</a>" class="twitter-link">http://<a href="http://www.recruitingshow.com" class="twitter-link">www.recruitingshow.com</a></a>
    
    / <a href="http://twitter.com/karla_porter/statuses/2652060959" class="twitter-link">#</a> <span class="twitter-timestamp"><abbr title="2009/07/15 09:58:02">15 mins ago</abbr></span></p>

    As you can see the hyperlink for "recruitingshow.com" has become broken.

    http://wordpress.org/extend/plugins/twitter-for-wordpress/

  2. ramir
    Member
    Posted 4 years ago #

    The regexps that are applied to the text to generate a-elements are not mutually exclusive.

    When the text contains an url of the form http://www.some.where, the first regexp (which matches a full url starting with a protocol) generates an a-element, and the second regexp (which matches a url without a protocol but starting with www.) generates a-elements inside both the href-attribute and the contained text of the first a-element.

    The second regexp should not match anything that has already been matched by the first, or in general, anything that is inside an a-element.

  3. ramir
    Member
    Posted 4 years ago #

    Rather than generating a-elements with two different regexps, which causes the problems mentioned above, it would probably be better to use only one regexp to generate a-elements from complete urls. Before this any incomplete urls can be first turned into complete urls e.g. by adding http://

    I'm also not very happy with the url syntax in the regexps, which among other problems does not seem to allow any hash signs, or periods in the path part.

    My suggestion:

    // turn an incomplete url beginning with www. into a complete url beginning with http://
    $text = preg_replace("/(?<!:\/\/)(www\.([a-z][a-z0-9\-]*[a-z0-9]\.)*[a-z]+(\/[a-zA-Z0-9\#\%\_\=\*\-\?\&\.]*[a-zA-Z0-9\#\%\_\=\*\-\?\&])*)/i","http://$1", $text);
    
    // turn complete url beginning with protocol:// into a-elements
    $text = preg_replace("/([a-zA-Z]+:\/\/([a-z][a-z0-9\-]*[a-z0-9]\.)*[a-z]+(\/[a-zA-Z0-9\#\%\_\=\*\-\?\&\.]*[a-zA-Z0-9\#\%\_\=\*\-\?\&])*)/i","<a href=\"$1\" class=\"twitter-link\">$1</a>", $text);
  4. striker
    Member
    Posted 4 years ago #

    just noting that ramir's suggested change above worked great.

  5. dehlz
    Member
    Posted 4 years ago #

    thanks ramir, your suggestion works much better for me also. The only (slight) problem is if the url ends with a '/', it doesn't get included in the link. Not a huge issue for me though, I'll try to figure it out.

  6. ptvguy
    Member
    Posted 4 years ago #

    Thanks for the fix. It works great.

    For any beginners finding this thread and wondering how to use ramir's fix for version 1.9.6 of Twitter for WordPress, open the twitter.php file and replace the following lines (115-119):

    // Props to Allen Shaw
    // match protocol://address/path/file.extension?some=variable&another=asf%
    $text = preg_replace("/\b([a-zA-Z]+:\/\/[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","<a href=\"$1\" class=\"twitter-link\">$1</a>", $text);
    // match www.something.domain/path/file.extension?some=variable&another=asf%
    $text = preg_replace("/\b(www\.[a-z][a-z0-9\_\.\-]*[a-z]{2,6}[a-zA-Z0-9\/\*\-\?\&\%]*)\b/i","<a href=\"http://$1\" class=\"twitter-link\">$1</a>", $text);

    with ramir's code:

    // Props to ramir
    // turn an incomplete url beginning with www. into a complete url beginning with http://
    $text = preg_replace("/(?<!:\/\/)(www\.([a-z][a-z0-9\-]*[a-z0-9]\.)*[a-z]+(\/[a-zA-Z0-9\#\%\_\=\*\-\?\&\.]*[a-zA-Z0-9\#\%\_\=\*\-\?\&])*)/i","http://$1", $text);
    // turn complete url beginning with protocol:// into a-elements
    $text = preg_replace("/([a-zA-Z]+:\/\/([a-z][a-z0-9\-]*[a-z0-9]\.)*[a-z]+(\/[a-zA-Z0-9\#\%\_\=\*\-\?\&\.]*[a-zA-Z0-9\#\%\_\=\*\-\?\&])*)/i","<a href=\"$1\" class=\"twitter-link\">$1</a>", $text);
    // Props to Allen Shaw
  7. dehlz
    Member
    Posted 4 years ago #

    just an FYI for people using Ramir's code above, the new "j.mp" shortened links no longer work with it. Expect to see these links a lot more since the bit.ly team is behind it and are beginning to roll out this week.

    I modified the regexp to work with the plugin:

    // turn complete url beginning with protocol:// into a-elements
    	$text = preg_replace("/([a-zA-Z]+:\/\/([a-z0-9\-]*[a-z0-9]\.)*[a-z]+(\/[a-zA-Z0-9\#\%\_\=\*\-\?\&\.]*[a-zA-Z0-9\#\%\_\=\*\-\?\&])*)/i","<a href=\"$1\" class=\"twitter-link\">$1</a>", $text);
  8. hifa
    Member
    Posted 4 years ago #

    do you know how we can display more than 1 tweet ?
    I searched all codes but could not find or make sure..
    I want to display last 5 tweets on my site

  9. hifa
    Member
    Posted 4 years ago #

    i do not mean widget. because I have bilingual site, i want to set two different twitter account in different languages. I'm able to do twitter account part, meaning in english different account, in secondary language different twitter account.
    just i could not set the number of links in code.
    how can i make it default it in code ?

Topic Closed

This topic has been closed to new replies.

About this Topic