{"id":159,"date":"2011-11-15T11:58:46","date_gmt":"2011-11-15T17:58:46","guid":{"rendered":"http:\/\/www.marcblase.com\/blog\/?p=159"},"modified":"2012-03-07T10:23:31","modified_gmt":"2012-03-07T16:23:31","slug":"add-tweets-to-wordpress","status":"publish","type":"post","link":"https:\/\/ma.rcbla.se\/blog\/2011\/11\/add-tweets-to-wordpress\/","title":{"rendered":"Add tweets to WordPress"},"content":{"rendered":"<p>I&#8217;m posting this here for future reference as I&#8217;ve had use of it in the past.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/\/ Include to use built-in SimplePie functionality\r\n\/\/ see: http:\/\/simplepie.org\/wiki\/reference\/start for usage\r\n\r\ninclude_once(ABSPATH . WPINC . '\/feed.php');\r\nadd_filter( 'wp_feed_cache_transient_lifetime', create_function( '$a', 'return 3600;' ) );\r\n\r\n\/\/ Set feed URL\r\n$rss = fetch_feed('https:\/\/twitter.com\/statuses\/user_timeline\/YOUR_TWITTER_ID.rss');\r\n\r\n\/\/ Checks that the object is created correctly\r\nif (!is_wp_error( $rss ) ) : \r\n    $maxitems = $rss-&gt;get_item_quantity(3);\r\n    \/\/ Build an array of all the items, starting with element 0 (first element).\r\n    $rss_items = $rss-&gt;get_items(0, $maxitems);\r\nendif;\r\n?&gt;\r\n\t\t\t\t\t\r\n&lt;ul id=&quot;tweetbox&quot;&gt;\r\n\r\n&lt;?php \r\nif ($maxitems == 0) {\r\n    echo '&amp;lt;li&gt;Twitter unavailable at this time.';\r\n} else {\r\n    foreach ( $rss_items as $item ) : \r\n    \t\/\/Fetch the tweet itself\r\n\t$xtwit = $item-&gt;get_description();\r\n        \/\/Remove the preceding 'username: '\r\n\t$xtwit = substr(strstr($xtwit, ': '), 2, strlen($xtwit));\r\n\t\/\/ Convert URLs into hyperlinks\r\n\t$xtwit = preg_replace(&quot;\/(http:\\\/\\\/)(.*?)\\\/(&#x5B;\\w\\.\\\/\\&amp;\\=\\?\\-\\,\\:\\;\\#\\_\\~\\%\\+]*)\/&quot;, &quot;&lt;a href=\\&quot;\\&#92;&#48;\\&quot; rel=\\&quot;external\\&quot;&gt;\\&#92;&#48;&lt;\/a&gt;&quot;, $xtwit);\r\n\t\/\/ Convert usernames (@) into links \r\n\t$xtwit = preg_replace(&quot;(@(&#x5B;a-zA-Z0-9\\_]+))&quot;, &quot;&lt;a href=\\&quot;http:\/\/www.twitter.com\/\\\\1\\&quot;  rel=\\&quot;external\\&quot;&gt;\\&#92;&#48;&lt;\/a&gt;&quot;, $xtwit);\r\n\t\/\/ Convert hash tags (#) to links \r\n\t$xtwit = preg_replace('\/(^|\\s)#(\\w+)\/', '\\1&lt;a href=&quot;http:\/\/search.twitter.com\/search?q=%23\\2&quot;  rel=&quot;external&quot;&gt;#\\2&lt;\/a&gt;', $xtwit);\r\n\t\/\/Specifically for non-English tweets, converts UTF-8 into ISO-8859-1\r\n\t$xtwit = iconv(&quot;UTF-8&quot;, &quot;ISO-8859-1\/\/TRANSLIT&quot;, $xtwit);\r\n?&gt;\r\n    &lt;li&gt;\r\n\t&lt;span class=&quot;update&quot;&gt;&lt;?php echo $xtwit; ?&gt;&lt;\/span&gt;&lt;br \/&gt;\r\n\t&lt;span class=&quot;date&quot;&gt;&lt;abbr class=&quot;timeago&quot; title=&quot;&lt;?php echo $item-&gt;get_date('c'); ?&gt;&quot;&gt;&lt;?php echo $item-&gt;get_date('c'); ?&gt;&lt;\/abbr&gt;&lt;\/span&gt;\r\n    &lt;\/li&gt;\r\n&lt;?php \r\nendforeach;\r\nunset($rss); \r\n}\r\n?&gt;\r\n&lt;\/ul&gt;\r\n<\/pre>\n<p><small>Note: Use the <a href=\"https:\/\/github.com\/rmm5t\/jquery-timeago\" target=\"_blank\">jQuery timeago<\/a> plugin to parse PHP date stamps into twiiter like time stamps.<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m posting this here for future reference as I&#8217;ve had use of it in the past. &lt;?php \/\/ Include to use built-in SimplePie functionality \/\/ see: http:\/\/simplepie.org\/wiki\/reference\/start for usage include_once(ABSPATH . WPINC . &#8216;\/feed.php&#8217;); add_filter( &#8216;wp_feed_cache_transient_lifetime&#8217;, create_function( &#8216;$a&#8217;, &#8216;return 3600;&#8217; ) ); \/\/ Set feed URL $rss = fetch_feed(&#8216;https:\/\/twitter.com\/statuses\/user_timeline\/YOUR_TWITTER_ID.rss&#8217;); \/\/ Checks that the object is [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[16,23,28,20],"class_list":["post-159","post","type-post","status-publish","format-standard","hentry","category-coding","tag-php","tag-rss","tag-simplepie","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/159","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/comments?post=159"}],"version-history":[{"count":12,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/159\/revisions"}],"predecessor-version":[{"id":203,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/159\/revisions\/203"}],"wp:attachment":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/media?parent=159"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/categories?post=159"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/tags?post=159"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}