<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Ian Swenson .com &#187; Web</title>
	<atom:link href="http://ianswenson.com/category/web/feed" rel="self" type="application/rss+xml" />
	<link>http://ianswenson.com</link>
	<description>Professionally Amateurish</description>
	<lastBuildDate>Wed, 21 Oct 2009 21:14:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Add Tweets to Wordpress</title>
		<link>http://ianswenson.com/web/add-tweets-to-wordpress</link>
		<comments>http://ianswenson.com/web/add-tweets-to-wordpress#comments</comments>
		<pubDate>Sat, 04 Jul 2009 17:05:22 +0000</pubDate>
		<dc:creator>ianswens</dc:creator>
				<category><![CDATA[Web]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://ianswenson.com/?p=222</guid>
		<description><![CDATA[A month ago, Roxanne got her new Web site, which incorporated her latest Tweet in her footer. It&#8217;s a nifty idea I&#8217;ve seen on several other sites, and I knew Roxanne would like it. However, it was not near as easy to implement as it should have been.
Due to some unknown server configuration, Wordpress had [...]]]></description>
			<content:encoded><![CDATA[<p>A month ago, Roxanne got her new Web site, which incorporated her latest Tweet in her footer. It&#8217;s a nifty idea I&#8217;ve seen on several other sites, and I knew Roxanne would like it. However, it was not near as easy to implement as it should have been.</p>
<p>Due to some unknown server configuration, Wordpress had trouble grabbing the Twitter RSS feed with any consistency. No matter <a href="http://wordpress.org/extend/plugins/twitter-for-wordpress/" title="Twitter for Wordpress">which</a> <a href="http://wordpress.org/extend/plugins/twitter-tools/" title="Twitter Tools">plugin</a> I used, error messages occurred 90% of the time. I spent at least 10 hours researching the subject and participating in <a href="http://wordpress.org/support/topic/268589">a small discussion</a> on the matter.</p>
<p>I&#8217;ve finally devised a workaround by using Twitter&#8217;s Atom feed instead of RSS. For the last four days it has worked without a hitch. All the function does is grab the most recent tweet and prints it along with how long ago it was tweeted.</p>
<p>Follow the link below for the code, and feel free to see it working on <a href="http://www.roxannecooke.com">Roxanne&#8217;s page</a>.</p>
<p><span id="more-222"></span></p>
<h4>Twitterpate function.</h4>
<p>To use, insert the following into your theme or template:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> Twitterpate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>You need a <code>functions.php</code> file in your theme directory. If it does not exist for your theme, create it in your theme directory and wrap <code>&lt;?php</code> before the function and <code>?&gt;</code> after it.)</p>
<p>Feel free to customize the HTML on lines 6, 8, &amp; 26 to suit your needs.</p>
<p>Insert the following into <code>functions.php</code>, replacing the username:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> Twitterpate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Your twitter username.</span>
	<span style="color: #000088;">$username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;JoeSchmoe&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$prefix</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&lt;span&gt;Latest Tweet:&lt;/span&gt; &quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$suffix</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000088;">$feed</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://search.twitter.com/search.atom?q=from:&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$username</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;rpp=1&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> parse_feed<span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">/*** Getting Tweet text ***/</span>
		<span style="color: #000088;">$step_one</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;content type=<span style="color: #000099; font-weight: bold;">\&quot;</span>html<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$feed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$step_two</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&lt;/content&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$step_one</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$tweet</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$step_two</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$tweet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$tweet</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tweet</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">/*** Getting date in human time difference ***/</span>
		<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'~&lt;published&gt;(.*?)&lt;/published&gt;~'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">,</span> <span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$date_string</span> <span style="color: #339933;">=</span> iso8601_to_timestamp<span style="color: #009900;">&#40;</span><span style="color: #000088;">$match</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span> <span style="color: #000088;">$date_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$date_string</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$date_string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;abbr&gt;'</span> <span style="color: #339933;">.</span> human_time_diff<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date_string</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' ago&lt;/abbr&gt;'</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$tweet</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$date_string</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$tweet</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> iso8601_to_timestamp<span style="color: #009900;">&#40;</span><span style="color: #000088;">$datestr</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$eregStr</span> <span style="color: #339933;">=</span>
		<span style="color: #0000ff;">'([0-9]{4})-'</span><span style="color: #339933;">.</span>  <span style="color: #666666; font-style: italic;">// centuries &amp; years CCYY-</span>
		<span style="color: #0000ff;">'([0-9]{2})-'</span><span style="color: #339933;">.</span>  <span style="color: #666666; font-style: italic;">// months MM-</span>
		<span style="color: #0000ff;">'([0-9]{2})'</span><span style="color: #339933;">.</span>   <span style="color: #666666; font-style: italic;">// days DD</span>
		<span style="color: #0000ff;">'T'</span><span style="color: #339933;">.</span>                    <span style="color: #666666; font-style: italic;">// separator T</span>
		<span style="color: #0000ff;">'([0-9]{2}):'</span><span style="color: #339933;">.</span>  <span style="color: #666666; font-style: italic;">// hours hh:</span>
		<span style="color: #0000ff;">'([0-9]{2}):'</span><span style="color: #339933;">.</span>  <span style="color: #666666; font-style: italic;">// minutes mm:</span>
		<span style="color: #0000ff;">'([0-9]{2})(\.[0-9]+)?'</span><span style="color: #339933;">.</span> <span style="color: #666666; font-style: italic;">// seconds ss.ss...</span>
		<span style="color: #0000ff;">'(Z|[+\-][0-9]{2}:?[0-9]{2})?'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Z to indicate UTC, -/+HH:MM:SS.SS... for local tz's</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">ereg</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$eregStr</span><span style="color: #339933;">,</span><span style="color: #000088;">$datestr</span><span style="color: #339933;">,</span><span style="color: #000088;">$regs</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// not utc</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'Z'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$op</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$h</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$m</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$op</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'-'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$h</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$m</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
				<span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$op</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'+'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$h</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$regs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$m</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$regs[1]</span>-<span style="color: #006699; font-weight: bold;">$regs[2]</span>-<span style="color: #006699; font-weight: bold;">$regs[3]</span> <span style="color: #006699; font-weight: bold;">$regs[4]</span>:<span style="color: #006699; font-weight: bold;">$regs[5]</span>:<span style="color: #006699; font-weight: bold;">$regs[6]</span>Z&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000088;">$twitterFeed</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$prefix</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> parse_feed<span style="color: #009900;">&#40;</span><span style="color: #000088;">$twitterFeed</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$suffix</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Credit goes to: <a href="http://www.nabble.com/grabbing-rss-or-other-type-of-feeds-within-lift-td24266097.html">Mark Essel</a> for the foundation of the function and <a href="http://dev.xoofoo.org/modules/xsoap/db/df3/a00034_7458f4fe2e94892e63804807994fb5b9.html">XooFoo</a> for the <code>iso8601_to_timestamp</code> function.</p>
]]></content:encoded>
			<wfw:commentRss>http://ianswenson.com/web/add-tweets-to-wordpress/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
