We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36822
    • 8 Posts
    I know this topic has been posted before, but a co-worker and I pulled together a way to add a Twitter feed to a site using getFeed, then use output modifiers to remove the original account name from the tweets, and restore the clickable links, account names, and hashtags.

    First, make a chunk called get_twitter_feed. Note that it uses property placeholders for the account and number of tweets, and has a ":replace" output modifier (to remove the original account name from the tweets).

    NOTE: the forums keep formatting the &url= property as a link and removing formatted placeholders... It should only read
    &url=`http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=accountplaceholder`

    <ul>
      [[getFeed:replace=`[[+account]]: ==`?
    	&url=`http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=[[+account]]` 
    	&limit=`[[+tweetcount]]`
    	&tpl=`general_twitter_tpl`
      ]]
    </ul>



    Call the chunk as follows. The output modifier requires the name to be case-sensitive as it appears on Twitter. For example, &account=`cnn` will pull the @cnn feed, but not remove the account name since it appears in the feed as "CNN".

    [[$get_twitter_feed? &account=`TwitterHandle` &tweetcount=`3`]]



    Make a tpl chunk to format the tweets pulled from the RSS feed. I've called it general_twitter_tpl. On the description, there is an output modifier called "twitter-parser" on the description placeholder.

    <li>
      <div class="twitter_item">
    	  <p>[[+description:twitter-parser]]</p>
    		<p class="small"><a href="[[+link]]">[[+date_timestamp:date:ago]]</a></p>
      </div><!-- end twitter_item -->
    </li>


    Finally, create a new snippet to be used as an output modifier. Name the snippet twitter-parser.

    <?php
    //Parses links: http://stackoverflow.com/questions/3329859/add-links-to-php-that-pulls-latest-tweet-from-twitter-account
    	// properly formatted URLs
        $urls = "/(((http[s]?:\/\/)|(www\.))?(([a-z][-a-z0-9]+\.)?[a-z][-a-z0-9]+\.[a-z]+(\.[a-z]{2,2})?)\/?[a-z0-9._\/~#&=;%+?-]+[a-z0-9\/#=?]{1,1})/is";
        $input = preg_replace($urls, " <a href='$1'>$1</a>", $input);
    
        // URLs without protocols
        $input = preg_replace("/href=\"www/", "href=\"http://www", $input);
    
    //Parses accounts
    	$input =  preg_replace('/ (@([-a-zA-Z0-9_]+)(:)?)/',' <a href="http://twitter.com/$2" target="_blank" rel="nofollow">@$2</a>',$input);
    
    //Parses hashtag
    	$input =  preg_replace('/(#([-a-zA-Z0-9_]+)(:)?)/','<a href="http://twitter.com/search/$2" target="_blank" rel="nofollow">#$2</a>',$input);
    
    return $input;
    ?>


    This should result in a clean unordered list with the specified number of tweets, and clickable links for links, account names, and hashtags. [ed. note: [email protected] last edited this post 14 years, 5 months ago.]
      • 14162
      • 67 Posts
      Good work Kevin. The twitter-parser didn't work for me though. But the Stackoverflow link you gave led me to this:
      // properly formatted URLs
          $input = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" target=\"_blank\">\\2</a>", $input);
          $input = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" target=\"_blank\">\\2</a>", $input);
          $input = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $input);
          $input = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $input);
      
      return $input;

      This works perfectly.
        • 28000 ☆ A M B ☆
        • 397 Posts
        Anyone still able to get this to work? Regardless if I use these snippets above or the add-on montwitter, getFeed is not able to process the twitter feed. I am getting an error in the getFeed snippet according to the modx error log.
          Benjamin Davis: American web designer living in Munich, Germany and a MODX Ambassador. I am also co-founder of SEDA.digital, a MODX Agency.
          • 36822
          • 8 Posts
          No, this method stopped working when Twitter changed its API. Try using the TwitterX add-on. You will need to create a Twitter app first, but there is easy to follow documentation included from the developer.
            • 28000 ☆ A M B ☆
            • 397 Posts
            Thanks, will give it a try!
              Benjamin Davis: American web designer living in Munich, Germany and a MODX Ambassador. I am also co-founder of SEDA.digital, a MODX Agency.