<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0"><channel><title>phoenix.heart - portfolio &amp; more</title> <link>http://www.phoenixheart.net</link> <description>phoenix.heart - portfolio &amp; more</description> <lastBuildDate>Tue, 10 Nov 2009 04:44:12 +0000</lastBuildDate> <generator>http://wordpress.org/?v=2.8.5</generator> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/phoenixheart" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item><title>WordPress: Thank that first time commentator!</title><link>http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/</link> <comments>http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/#comments</comments> <pubDate>Sat, 07 Nov 2009 05:12:08 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Blahblahblah]]></category> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[user interaction]]></category> <category><![CDATA[wordpress]]></category> <category><![CDATA[wordpress hack]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=392</guid> <description><![CDATA[Thumbnail credit: Premshree Pillai
To a website, comments are important &#8211; this you must agree. But not all visitors leave comments &#8211; in fact, very, very few. Most of them care about the content only, and tend to leave (bounce) the site right after getting the information they need (so sad a life, huh?)
Many tips have [...]]]></description> <content:encoded><![CDATA[<p><small>Thumbnail credit: <a
href="http://www.flickr.com/photos/premshree/">Premshree Pillai</a></small></p><p><span
class="drop-cap">T</span>o a website, comments are important &#8211; this you must agree. But not all visitors leave comments &#8211; in fact, very, very few. Most of them care about the content only, and tend to leave (bounce) the site right after getting the information they need (so sad a life, huh?)</p><p>Many tips have been introduced and used to encourage visitors to contribute to your site via comments. To my knowledge, and to name a few:</p><ul><li>Use dofollow links in comments. By default, WordPress and other blogging systems mark links in comments with <code>rel="nofollow"</code> attribute. This attribute tells search engines to not follow the links, which means the commenter&#8217;s site will not be able to share any Google juice with you. While effective in fighting spammers, this technique may a bit disappoint the real visitors. Plugins like <a
href="http://wordpress.org/extend/plugins/sem-dofollow/">Dofollow</a> address this problem and remove &#8220;nofollow&#8221; attribute from comment links.</li><li>Further promote the commenter&#8217;s blog (if any). <a
href="http://wordpress.org/extend/plugins/commentluv/">CommentLuv</a> is a plugin that &#8220;will visit the site of the comment author while they type their comment and retrieve a selection of their last blog posts, tweets or digg submissions which they can choose one from to include at the bottom of their comment when they click submit&#8221;.</li><li>Choose a (random) comment to give small prizes such as free ebooks, preminum themes etc.</li><li>Explicitly ask the readers to give comments at the end of the article &#8211; &#8220;Please share your thoughts&#8221;, &#8220;What do you think?&#8221;, &#8220;What say you?&#8221; etc. etc.</li></ul><p>Today I would like to mention another method to encourage commenting. Though this won&#8217;t likely attract more commenters, it may encourage existing ones to leave more comments and become more effective contributors.</p><p>The method is called &#8220;Thank first time commenters&#8221; and it works like this: <span
id="more-392"></span></p><p>Normally when a visitor leaves a comment for the first time on your blog, the comment is displayed under &#8220;awaiting moderation&#8221; status and visible to him only. When there&#8217;s nothing wrong with this approach, it&#8217;s not really interesting for the commenter himself. Now how about showing him a &#8220;thank you&#8221; or &#8220;welcome&#8221; message, or better, a whole page, for the first time contributing his [great] ideas to your site? This time it&#8217;s much cooler!</p><p>Let&#8217;s turn the idea into real action for a WordPress site.</p><h3>Step 1. Detect first time commenter</h3><p><img
class="shot" src="/wp-content/uploads/2009/11/comments.gif" alt="Comments" width="470" height="136" /><br
/> <small>Original image from <a
href="http://www.flickr.com/photos/chrismar/">Chrismar</a></small></p><p>To detect whether the comment author is leaving his first comment on you site, we need to <em>hook</em> into one of the comment-related functions (you can read about hooks <a
href="http://codex.wordpress.org/Plugin_API">here</a>). For this purpose I use <strike><code>comment_post()</code>, but there may be some other alternatives which I don&#8217;t know about</strike> <code>comment_post_redirect</code> filter. This filter is applied just before WordPress redirects the commentator after him posting a comment &#8211; exactly what we need.</p><p>In the theme&#8217;s <code>functions.php</code> page, add these lines:</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
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'comment_post_redirect'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'check_first_time_comment'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * This functions accepts one parameter being the location to redirect commentators to
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> check_first_time_comment<span style="color: #009900;">&#40;</span><span style="color: #000088;">$location</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">global</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the comment from global variables</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// if it's not &quot;unapproved&quot;, don't modify the redirect location</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">!=</span> <span style="color: #000088;">$comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment_approved</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$location</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// now check if it's really the first time the commenter comments</span>
    <span style="color: #666666; font-style: italic;">// to do this, we check the number of comments this author have left on our site</span>
    <span style="color: #990000;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT COUNT(comment_ID) from <span style="color: #006699; font-weight: bold;">{$wpdb-&gt;prefix}</span>comments WHERE comment_author_email='<span style="color: #006699; font-weight: bold;">{$comment-&gt;comment_author_email}</span>'&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_var</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$location</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// he's left more than 1 comment - do nothing with him</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//--- if we reach here, this is the first comment he leaves on our site. Do our stuffs now! ---//</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// first save the comment data into session. We'll use it later</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_time_comment'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">serialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// now modify the $location so that WordPress redirects this commentator to our special page</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'/thank-you-for-contribution/'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>What the code does is pretty self-descriptive. The function <code>check_first_time_comment($location)</code> is triggered just before WordPress redirects the commentator to a location (being the post, the page, or the error page). It takes one parameter, being the location url. Upon triggered, it checks for the status of the comment and only does its deeds if the comment is &#8220;unapproved&#8221; (status code 0). Then, it does a further check to see if this is really the first time the comment author leaves a comment on the site with the email address. If this is true, it saves the comment data into session for later retrieval, and modifies the location string to redirect the commenter to the thank you page.</p><h3>Step 2. Create a &#8220;Thank you&#8221; landing page</h3><p><img
class="shot" src="/wp-content/uploads/2009/11/thanks-narrow.jpg" alt="Thank you!" width="470" height="130" /><br
/> <small>Original image by <a
href="http://www.flickr.com/photos/premshree/">Premshree Pillai</a></small></p><p>This is the page where our valuable commenter is redirected to. To create such a page, first copy page.php in the theme folder into thank-you-page.php. Open it and locate these lines on the top:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
</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: #666666; font-style: italic;">/*
Template Name: Page
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div><p>Change the template name to something descriptive like &#8220;Thank You Page&#8221;, and add some logic, like this:</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
</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: #666666; font-style: italic;">/*
Template Name: Thank You Page
*/</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">session_id</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">session_start</span><span style="color: #009900;">&#40;</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;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_time_comment'</span><span style="color: #009900;">&#93;</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;">// the page should not be directly accessed</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: /'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now get the comment data from session and utilize it</span>
<span style="color: #000088;">$first_time_comment</span> <span style="color: #339933;">=</span> <span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_time_comment'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// clear the session</span>
<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">unserialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SESSION</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'first_time_comment'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$the_post</span> <span style="color: #339933;">=</span> get_post<span style="color: #009900;">&#40;</span><span style="color: #000088;">$first_time_comment</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comment_post_ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// get the post of the comment</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$the_post</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;">// for some reason, we cannot find the post.</span>
    <span style="color: #666666; font-style: italic;">// The action here depends on you. For me I redirect to home.</span>
    <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: /'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// tell the browser not to cache this page in anyway</span>
<span style="color: #666666; font-style: italic;">// !IMPORTANT: You may also want to exclude this page from cache plugins too!</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cache-Control: no-cache, must-revalidate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
&quot;Thank you&quot; content goes here
With the post on hand, you can enrich this thank you page with:
- similar posts
- posts in the same categories
- random posts
- most read posts
- most commented posts etc.
*/</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div><p>Save the template. Now go to WordPress control panel, create a new page with &#8220;Thank You Page&#8221; as the template, and <code>thank-you-for-contribution</code> as the slug. You may want to disable ping and comments for the page too.</p><p>There! Your blog is now ready to welcome the first time commentators!</p><p>But wait&#8230; there&#8217;s still a catch.</p><h3>Step 3. Post-production</h3><p>Now, if you list down your pages somewhere on your blog, &#8220;Thank You&#8221; page will stupidly appear. The fix is easy: <a
href="http://codex.wordpress.org/Template_Tags/wp_list_pages">exclude the page ID</a>. Also, if you have a cache plugin installed, it&#8217;s best to exclude Thank You page from caching.</p><p>Want to see it in action? Just leave a comment here as a new commentator!</p><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/&amp;title=WordPress%3A+Thank+that+first+time+commentator%21" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=WordPress%3A+Thank+that+first+time+commentator%21+-+http://tr.im/Ep3L+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/&amp;title=WordPress%3A+Thank+that+first+time+commentator%21" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/&amp;title=WordPress%3A+Thank+that+first+time+commentator%21" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/&amp;title=WordPress%3A+Thank+that+first+time+commentator%21" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/&amp;t=WordPress%3A+Thank+that+first+time+commentator%21" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/M3p9b93_9Jw" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/11/thank-that-first-time-commentator/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>How I sped up my Thica.net</title><link>http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/</link> <comments>http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/#comments</comments> <pubDate>Tue, 27 Oct 2009 09:44:08 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Blahblahblah]]></category> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[optimization]]></category> <category><![CDATA[speed]]></category> <category><![CDATA[wordpress]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=377</guid> <description><![CDATA[Thumbnail credit: Amnemona
If you didn&#8217;t notice, I have another site called Thica.net &#8211; Vietnam poetry network, a WordPress (what else) powered blog dedicated to poems in Vietnamese. The site is receiving about 60K of views per month, which is 12x to that of the moment when it was started back on March 2008, and I&#8217;m [...]]]></description> <content:encoded><![CDATA[<p><small>Thumbnail credit: <a
href="http://www.flickr.com/photos/marinacvinhal/">Amnemona</a></small></p><p><span
class="drop-cap">I</span>f you didn&#8217;t notice, I have another site called <a
href="http://www.thica.net">Thica.net &#8211; Vietnam poetry network</a>, a WordPress (what else) powered blog dedicated to poems in Vietnamese. The site is receiving about 60K of views per month, which is 12x to that of the moment when it was started back on March 2008, and I&#8217;m rather happy about it.</p><p>About one month ago, Thica.net started to become very slow and tent to produce strange problems. More than often it threw 503 Internal Server Error just when I attempt to add a new post, or 404 Page Not Found for a page that I <em>knew</em> it was there, such as admin panel, plugin section etc. After some deep look inside, I decided that my site was too bloated and then it was time to optimize things to start it up. To admit, the result is nowhere near perfection, but it satisfies my need. So I think I&#8217;ll share with you here.</p><h4>1. Eliminate unused plugins</h4><p><img
class="shot" src="/wp-content/uploads/2009/10/jigsaw.jpg" alt="Plugins" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/smackfu/">smackfu</a></p><p>Being a developer, I&#8217;m a big fan of plugins and addons. My Firefox has about 30 addons, ranging from <a
href="https://addons.mozilla.org/firefox/addon/1865">Adblock Plus</a> to <a
href="http://www.ultrareach.com/">UltraSurf</a> (I&#8217;m living in a communist country FYI) and <a
href="https://addons.mozilla.org/en-US/firefox/addon/5369">YSlow</a>. Similarly, Thica.net had like 50 plugins, active and inactive alike. So you know, plugins power up WordPress in many ways, but on the downside slow it down because of all the added functions, hooks, data and so on. Some plugins are even terribly written (like one random post plugin which gets <strong>ALL</strong> posts from the database and uses PHP loop to get 5 random posts &#8211; WTH) and may cause serious problems: slowness, security holes, or even crashes your site. <span
id="more-377"></span></p><p>So I dedicated my time filtering the plugins &#8211; which were <em>really</em> needed, which may be replaced by hacks and/or other inexpensive methods, which may be replaced with another plugin not as bloated, and which were totally useless. For instance, <a
href="http://wordpress.org/extend/plugins/akismet/">Askimet</a>, <a
href="http://wordpress.org/extend/plugins/ozh-admin-drop-down-menu/">Ozh&#8217; Admin Drop Down Menu</a>, <a
href="http://wordpress.org/extend/plugins/google-sitemap-generator/">Google XML Sitemaps</a>, and <a
href="http://wordpress.org/extend/plugins/login-lockdown/">Login LockDown</a> were definitely kept. I&#8217;m now using my own functions to get the random and most read posts, searching for a SEO plugin in place of <a
href="http://wordpress.org/extend/plugins/all-in-one-seo-pack/">All In One SEO Pack</a> as for some reasons <a
href="http://wordpress.org/extend/plugins/platinum-seo-pack/">Platinum SEO Pack</a> didn&#8217;t fit my needs, and have trashed away tens of unused plugins. By &#8220;trashing away&#8221;, I mean deleting from the hard drive, as inactive plugins still waste WP resource &#8211; it still has to look them up and check for their status anyway.</p><h4>2. Use a total cache solution</h4><p><img
class="shot" src="/wp-content/uploads/2009/10/db.jpg" alt="Database" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/adesigna/">adesigna</a></p><p>To a busy application &#8211; web or non-web alike, caching is vital. For as far as I know WordPress is not as bloated as other CMS (Joomla, Drupal, Magento to name a few), but its performance has rooms to be improved. I had been using <a
href="http://wordpress.org/extend/plugins/wp-super-cache/">WP SuperCache</a> for quite a while and the performance was good enough, until the problems occurred and I realized that it had some extra features that I didn&#8217;t need at all. Then I sought out for some alternatives, like <a
href="http://wordpress.org/extend/plugins/w3-total-cache/">W3 Total Cache</a>, <a
href="http://wordpress.org/extend/plugins/askapache-crazy-cache/">Crazy Cache</a> etc. <a
href="http://www.satollo.net/plugins/hyper-cache">HyperCache</a> is where I stopped at &#8211; just a matter of personal opinion, and I&#8217;m happy with it so far.</p><p>But HyperCache alone was not enough in my case. Don&#8217;t you know that for a complete page to be served, WordPress has to make like a bunch of continuous calls to the database? My Thica.net for instance, makes an average of 50 MySQL queries for the home page and 80 to 90 queries for each single page. That&#8217;s rather expensive isn&#8217;t it?</p><p>Lucky me, there&#8217;s another kind of caching solution called database caching. As most of the query results don&#8217;t change from time to time &#8211; the post content, the categories, the tags&#8230; often remain the same &#8211; they can be cached for later use. For this purpose I installed <a
href="http://wordpress.org/extend/plugins/db-cache-reloaded/">DB Cache Reloaded</a> and &#8211; just like magic &#8211; almost all of the queries (85 over 90 for example) are now served from the cache. You must admit, that&#8217;s a huge improvement. My host should thank me for not bombing their server!</p><h4>3. Staticalize WordPress variables</h4><p><img
class="shot" src="/wp-content/uploads/2009/10/phpcode.jpg" alt="Plugins" width="470" height="136" /></p><p>WordPress is so flexible &#8211; almost anything can be customized. That&#8217;s definitely great. But sometimes the greatness comes a bit too far from necessity. If you open a normal header.php file from a normal theme, 99% chance is you&#8217;ll see these lines:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
</pre></td><td
class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> &lt;?php language_attributes<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; ?&gt;</span>&gt;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php bloginfo('html_type'); ?&gt;</span></span>; charset=<span style="color: #009900;">&lt;?php bloginfo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'charset'</span><span style="color: #66cc66;">&#41;</span>; ?&gt;</span>&quot; /&gt;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;&lt;?php wp_title<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'&amp;laquo;'</span>, true, <span style="color: #ff0000;">'right'</span><span style="color: #66cc66;">&#41;</span>; ?&gt;</span> <span style="color: #009900;">&lt;?php bloginfo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'name'</span><span style="color: #66cc66;">&#41;</span>; ?&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;?php bloginfo<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'stylesheet_url'</span><span style="color: #66cc66;">&#41;</span>; ?&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pingback&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php bloginfo('pingback_url'); ?&gt;</span></span>&quot; /&gt;</pre></td></tr></table></div><p>You guessed it, these scripts get the settings from the database. Now let&#8217;s face it: how often would you change your blog name, or the language, or the style sheet URL, or the HTML charset? For me, it&#8217;s once and for all. vi-VN is my site lang, charset is of course UTF-8, and I don&#8217;t have any intention to change them ever. That&#8217;s why in the theme, I replaced the dynamic scripts with static content whenever applicable:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
</pre></td><td
class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span style="color: #000066;">dir</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ltr&quot;</span> <span style="color: #000066;">lang</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;vi-VN&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;stylesheet&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://static.thica.net/css/notepad-chaos.css?ver=1.2.21&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span> <span style="color: #000066;">media</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;screen&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">link</span> <span style="color: #000066;">rel</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;pingback&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.thica.net/xmlrpc.php&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;!-- and so forth --&gt;</span></pre></td></tr></table></div><p>This way I saved quite a few of unnecessary queries and made my site run a bit faster.</p><h4>4. Follow Yahoo&#8217;s optimization tips</h4><p>Have you taken a look at Yahoo!&#8217;s <a
href="http://developer.yahoo.com/performance/rules.html">Best Practices for Speeding Up Your Web Site</a>? It is an awesome resource for those who want to optimize their blogs for performance. Among the rules, these are particularly useful for my case and have been implemented:</p><h5>4.1. Add an Expires or a Cache-Control Header</h5><p><img
class="shot" src="/wp-content/uploads/2009/10/clock.jpg" alt="Clock" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/laffy4k/">laffy4k</a></p><p>An Expires header lets the browser have an idea of when the requested content is expired and needs to be re-downloaded. By setting a content&#8217;s expires header to a far future, you can tell the browser to use the local cached copy instead of retrieving a fresh version from the server, thus save both bandwidth and loading time. Perfect candidates for this are the images, css, javascript etc.</p><p>In the .htaccess file of Thica.net&#8217;s root directory, I added these lines:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td
class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;IfModule mod_expires.c&gt;</span>
ExpiresActive on
ExpiresByType image/gif &quot;access plus 10 years&quot;
ExpiresByType image/png &quot;access plus 10 years&quot;
ExpiresByType image/jpg &quot;access plus 10 years&quot;
ExpiresByType application/x-javascript &quot;access plus 10 years&quot;
ExpiresByType text/css &quot;access plus 10 years&quot;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>IfModule&gt;</span></pre></td></tr></table></div><p>These lines mean &#8220;These images, javascript, and css includes will not be changed for 10 years more, so dear Firefox/Chrome/Opera/Safari/IE, please use their copies from your cache whenever applicable and don&#8217;t put the heavy load on me, thanks&#8221;. The so understanding browser will be ok with such a polite request, and tada, all the specified contents are loaded from the cache in a blink of an eye.</p><h5>4.2 Gzip Components</h5><p><img
class="shot" src="/wp-content/uploads/2009/10/zip.jpg" alt="Zip" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/xploded/">Isobel T</a></p><p>Now-a-days, all modern browsers support gzipped components. Imagine it like this: first the browser requests for a page. Instead of returning the page as is, the server compresses it and sends the archive back. The browser receives the archive, decompresses it into normal state, and renders the decompressed content normally. Like WinZIP or WinRAR does, but in a web context. Simple?</p><p>To take advantage of this technique, once again I opened the .htaccess file and added these lines:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
</pre></td><td
class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;ifmodule mod_deflate.c&gt;</span>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>ifmodule&gt;</span></pre></td></tr></table></div><p>This configuration forces the server to gzip those MIME contents before returning them to the browser. Made a quick test at the <a
href="http://www.gidnetwork.com/tools/gzip-test.php">GIDZipTest</a> fot the homepage and I received this result:</p><table
style="height: 250px; width: 300px;" border="0" cellspacing="0" cellpadding="0"><tbody><tr><td
style="border-bottom: 1px dotted #dadada;">Web page compressed?</td><td
style="border-bottom: 1px dotted #dadada; text-align: right; font-weight: bold;"><span
style="color: green;">Yes</span></td></tr><tr><td
style="border-bottom: 1px dotted #dadada;">Compression type?</td><td
style="border-bottom: 1px dotted #dadada; text-align: right; font-weight: bold;">gzip</td></tr><tr><td
style="border-bottom: 1px dotted #dadada;">Size, Markup (bytes)</td><td
style="border-bottom: 1px dotted #dadada; text-align: right; font-weight: bold;">31,179</td></tr><tr><td
style="border-bottom: 1px dotted #dadada;">Size, Compressed (bytes)</td><td
style="border-bottom: 1px dotted #dadada; text-align: right; font-weight: bold;">7,476</td></tr><tr><td>Compression %</td><td
style="text-align: right; font-weight: bold;">76.0</td></tr></tbody></table><p>So, 3 lines added into the .htaccess file and I reduced 76% of bandwidth (means loading time cut down to 24%). Marvelous.</p><h5>4.3 Split Components Across Domains</h5><p><img
class="shot" src="/wp-content/uploads/2009/10/server.jpg" alt="Servers" width="470" height="136" /><br
/> Original image by <a
href="http://www.flickr.com/photos/jamisonjudd/">Jamison_Judd</a></p><p>Browsers have a limit on how many parallel requests can be sent &#8211; it&#8217;s 2 for Internet Explorer and 4 for Firefox (configurable, but can&#8217;t exceed 8 if I&#8217;m not wrong). This is a per domain value. It is advised that a site&#8217;s components should be distributed on more than one domain to maximize the parallel downloads. So if your HTML is served from site.com, your images are located on img.site.com, and your css files are put on static.site.com, the number of maximum parallel downloads a browser can perform on your site are tripled, and the loading time may be cut by two third or so.</p><p>That&#8217;s why I created two subdomains: img.thica.net for the theme images, and static.thica.net for other static contents like style sheets and javascripts. The performance is significantly improved &#8211; my site often finishes loading before I knew it, period.</p><h4>The result so far</h4><p>Well, my site ran much faster. Unfortunately, the errors kept occurring from time to time still. From the error logs, it showed that somehow my php5 CGI processes were terminated now and then, hence the 503 and 404 errors.</p><p>I contacted my host. They said my site was using too much of resource. I told them that a WP-powered site with 2K to 3K of views per day and 2GB of bandwidth a month could hardly use too much resource. So they were like: bandwidth and resource are not the same. Then they blamed my PHP scripts, saying that All in One SEO Pack sucked. &#8220;Then how come it&#8217;s so widely used around this earth?&#8221; I asked. They kept silence, the errors kept happening.</p><p>I was forced to use my final weapon.</p><h4>5. Switch to another host</h4><p><a
href="http://wpwebhost.com/affiliate/idevaffiliate.php?id=728_0_1_17"><img
class="shot" src="/wp-content/uploads/2009/10/wpwebhost.jpg" alt="WPWebHost" width="470" height="136" /></a></p><p>Yes, the final step I took was switching to another host. I asked for reference from many people, and later got convinced by Jean Baptiste Jung, the famous guy behind <a
href="http://www.wprecipes.com">WPRecipes</a>, <a
href="http://www.catswhocode.com">CatsWhoCode</a>, and most recently <a
href="http://www.codeswhoblog.com">CatsWhoBlog</a>, to go with <a
href="http://www.wpwebhost.com">WPWebHost</a> (many thanks, Jean!). It was not a smooth migration to be honest, as I had 4 or 5 sites to be relocated, when my old host doesn&#8217;t use cPanel for site controlling. But what I do really, really appreciate is, no matter what the problem was, no matter small or big, they were always there, supportive and helpful.</p><p>Well I don&#8217;t want to sound like a salesman, but WPWebHost really rocks. So if you are planning for a move, I highly recommend them. The banner above is in fact an affiliate link, so I would appreciate a lot if you purchase their hosting package via clicking on it <img
src='http://www.phoenixheart.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><h4>The <em>final</em> result</h4><p>That&#8217;s it &#8211; my journey to optimize my Thica.net to make it speedy, and I&#8217;m really pleased with the result: faster load, light footprints, no stupid errors, minimum bandwidth. You may want to check the result yourself at <a
href="http://www.thica.net" title="Thica.net - Mạng thi ca Việt Nam">the site itself</a>. What do you think about it / this article / my pidgin English / my bad writing? I&#8217;m happy to see your comments.</p><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/&amp;title=How+I+sped+up+my+Thica.net" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=How+I+sped+up+my+Thica.net+-+http://tr.im/DczI+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/&amp;title=How+I+sped+up+my+Thica.net" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/&amp;title=How+I+sped+up+my+Thica.net" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/&amp;title=How+I+sped+up+my+Thica.net" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/&amp;t=How+I+sped+up+my+Thica.net" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/-s2da1RbYOg" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/10/how-i-sped-up-thica-net/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Code Snippet 3 – Create post slugs</title><link>http://www.phoenixheart.net/2009/10/code-snippet-3-create-post-slugs/</link> <comments>http://www.phoenixheart.net/2009/10/code-snippet-3-create-post-slugs/#comments</comments> <pubDate>Sat, 17 Oct 2009 12:20:40 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=328</guid> <description><![CDATA[If you&#8217;re used to WordPress, you must have noticed that usually a blog doesn&#8217;t use the default permalink structure (like http://site.com/?p=43, where 43 is the post ID store in the database). Instead, almost all blog owners tend to use the built-in option form to set the permalinks to something similar to http://site.com/a-great-post and leave the [...]]]></description> <content:encoded><![CDATA[<p>If you&#8217;re used to WordPress, you must have noticed that usually a blog doesn&#8217;t use the default permalink structure (like <code>http://site.com/?p=43</code>, where 43 is the post ID store in the database). Instead, almost all blog owners tend to use the built-in option form to set the permalinks to something similar to <code>http://site.com/a-great-post</code> and leave the rest to Apache&#8217;s mod_rewrite to handle. In this case, <em>a-great-post</em> is called a <em>post slug</em>, or to be short, a slug. According to WordPress Codex:</p><blockquote><p>A slug is a few words that describe a post or a page. Slugs are usually a URL friendly version of the post title (which has been automatically generated by WordPress), but a slug can be anything you like. Slugs are meant to be used with permalinks as they help describe what the content at the URL is.</p></blockquote><p>In case you are wondering, slugs play a really, really important part in <abbr
title="Search Engine Optimization">SEO</abbr>. This is due to the fact that search engines like Google analyze an URL, and if it is relevant to the page&#8217;s content, the page&#8217;s rank point may be increased. Just like to us human, <em>?p=43</em> doesn&#8217;t tell anything, but <em>how-to-create-post-slugs</em> surely does.</p><p>So how is a slug generated?<span
id="more-328"></span> Most of the time, the post/page title is involved.</p><ol><li>First, notice that all slugs are in lowercase format</li><li>Second, all non-alphanumeric characters &#8211; those ugly &amp;?#$()&#8230; &#8211; are removed.</li><li>Third, spaces get replaced by dashes</li></ol><p>Let&#8217;s <em>codify</em> them into PHP:</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
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> create_slug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_title</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// 1. convert into lower case</span>
    <span style="color: #000088;">$post_title</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// 2. only accepts alphanumerical characters (a-z, 0-9), spaces, and dashes</span>
    <span style="color: #666666; font-style: italic;">// to do that, we use some RegEx magic</span>
    <span style="color: #000088;">$post_title</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[^a-z0-9 -]/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">// 3. replace the spaces with dashes</span>
    <span style="color: #000088;">$post_title</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$post_title</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>Now test the function &#8211; we&#8217;ll be using some real world examples from Digg:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">echo</span> create_slug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;High-Speed 'Other' Internet Goes Global &quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> create_slug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Proposed Anti-Piracy Legislation is Flawed, ISP   Says'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> create_slug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cheetah, Gecko and Spiders Inspire Robotic Designs (PICS)'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> create_slug<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'In-App Sales &amp; iTablet: The Killer Combo to Save Publishing?'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div><p>The above code produces:</p><div
class="wp_syntax"><div
class="code"><pre class="html4strict" style="font-family:monospace;">high-speed-other-internet-goes-global-
proposed-anti-piracy-legislation-is-flawed-isp---says
cheetah-gecko-and-spiders-inspire-robotic-designs-pics
in-app-sales--itablet-the-killer-combo-to-save-publishing</pre></div></div><p>Not bad huh? There are some problems however. First, if the title has continuous spaces, the slug will contains continuous dashes, which is not quite right. Second, much more important, we didn&#8217;t take into account a concept called <a
title="Wikipedia article on stop words" href="http://en.wikipedia.org/wiki/Stopwords">stop words</a> &#8211; long story short, stop words are words that don&#8217;t contain important information and are often filtered out from search queries by search engines. A list of English stop words can be found <a
title="List of English stop words" href="http://armandbrahaj.blog.al/2009/04/14/list-of-english-stop-words/">here</a>.</p><p>With this information on hand, we improve our code a bit:</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
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> create_slug<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_title</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// 1. convert into lower case</span>
    <span style="color: #000088;">$post_title</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// 2. only accepts alphanumerical characters (a-z, 0-9), spaces, and dashes</span>
    <span style="color: #666666; font-style: italic;">// to do that, we use some RegEx magic</span>
    <span style="color: #000088;">$post_title</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/[^a-z0-9 -]/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
    <span style="color: #666666; font-style: italic;">// 3. replace the spaces with dashes</span>
    <span style="color: #000088;">$post_title</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// 4. deal with stop words. I added '' (empty string) into the stop words array too.</span>
    <span style="color: #000088;">$stop_words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'a'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'about'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'above'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'above'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'across'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'after'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'afterwards'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'again'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'against'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'all'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'almost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'alone'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'along'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'already'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'also'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'although'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'always'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'am'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'among'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'amongst'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'amoungst'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'amount'</span><span style="color: #339933;">,</span>  <span style="color: #0000ff;">'an'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'and'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'another'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'any'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'anyhow'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'anyone'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'anything'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'anyway'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'anywhere'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'are'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'around'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'as'</span><span style="color: #339933;">,</span>  <span style="color: #0000ff;">'at'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'back'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'be'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'became'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'because'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'become'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'becomes'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'becoming'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'been'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'before'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'beforehand'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'behind'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'being'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'below'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'beside'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'besides'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'between'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'beyond'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bill'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'both'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'bottom'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'but'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'by'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'call'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'can'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cannot'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cant'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'co'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'con'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'could'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'couldnt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'cry'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'de'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'describe'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'detail'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'do'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'done'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'down'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'due'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'during'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'each'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'eg'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'eight'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'either'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'eleven'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'else'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'elsewhere'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'empty'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'enough'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'etc'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'even'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ever'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'every'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'everyone'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'everything'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'everywhere'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'except'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'few'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fifteen'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fify'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fill'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'find'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fire'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'first'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'five'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'for'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'former'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'formerly'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'forty'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'found'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'four'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'from'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'front'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'full'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'further'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'get'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'give'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'go'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'had'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'has'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hasnt'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'have'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'he'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hence'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'her'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'here'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hereafter'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hereby'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'herein'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hereupon'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hers'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'herself'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'him'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'himself'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'his'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'how'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'however'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hundred'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ie'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'if'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'in'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'inc'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'indeed'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'interest'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'into'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'is'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'it'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'its'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'itself'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'keep'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'last'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'latter'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'latterly'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'least'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'less'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ltd'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'made'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'many'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'may'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'me'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'meanwhile'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'might'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mill'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mine'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'more'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'moreover'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'most'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'mostly'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'move'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'much'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'must'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'my'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'myself'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'namely'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'neither'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'never'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nevertheless'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'next'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nine'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'no'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nobody'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'none'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'noone'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nor'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'not'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nothing'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'now'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'nowhere'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'of'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'off'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'often'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'on'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'once'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'one'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'only'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'onto'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'or'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'other'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'others'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'otherwise'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'our'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ours'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ourselves'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'out'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'over'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'own'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'part'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'per'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'perhaps'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'please'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'put'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rather'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'re'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'same'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'see'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'seem'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'seemed'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'seeming'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'seems'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'serious'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'several'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'she'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'should'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'show'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'side'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'since'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sincere'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'six'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sixty'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'so'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'some'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'somehow'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'someone'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'something'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sometime'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sometimes'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'somewhere'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'still'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'such'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'system'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'take'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'ten'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'than'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'that'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'the'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'their'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'them'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'themselves'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'then'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thence'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'there'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thereafter'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thereby'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'therefore'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'therein'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thereupon'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'these'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'they'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thickv'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thin'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'third'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'this'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'those'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'though'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'three'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'through'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'throughout'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thru'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'thus'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'to'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'together'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'too'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'top'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'toward'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'towards'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'twelve'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'twenty'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'two'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'un'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'under'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'until'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'up'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'upon'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'us'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'very'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'via'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'was'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'we'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'well'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'were'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'what'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whatever'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'when'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whence'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whenever'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'where'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whereafter'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whereas'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whereby'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wherein'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whereupon'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'wherever'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whether'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'which'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'while'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whither'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'who'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whoever'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whole'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whom'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'whose'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'why'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'will'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'with'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'within'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'without'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'would'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yet'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'you'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'your'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yours'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yourself'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'yourselves'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$slug</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// explode() the post title into single words</span>
    <span style="color: #000088;">$segments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$post_title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$segments</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$segment</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// if the segment is not a stop words, add it into $slug array</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$segment</span><span style="color: #339933;">,</span> <span style="color: #000088;">$stop_words</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$slug</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$segment</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// now convert the $slug array into a string with dashes being the connector</span>
    <span style="color: #000088;">$slug</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$slug</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$slug</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div><p>Run the previous example again, we have:</p><div
class="wp_syntax"><div
class="code"><pre class="html4strict" style="font-family:monospace;">high-speed-internet-goes-global
proposed-anti-piracy-legislation-flawed-isp-says
cheetah-gecko-spiders-inspire-robotic-designs-pics
app-sales-itablet-killer-combo-save-publishing</pre></div></div><p>That&#8217;s better, and this time more much usable, isn&#8217;t it? Now the next step should be the database part &#8211; create a `slug` field as a unique key, and start querying on it instead of the ID. You handle it!</p><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/10/code-snippet-3-create-post-slugs/&amp;title=Code+Snippet+3+-+Create+post+slugs" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=Code+Snippet+3+-+Create+post+slugs+-+http://tr.im/C7Pg+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/10/code-snippet-3-create-post-slugs/&amp;title=Code+Snippet+3+-+Create+post+slugs" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/10/code-snippet-3-create-post-slugs/&amp;title=Code+Snippet+3+-+Create+post+slugs" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/10/code-snippet-3-create-post-slugs/&amp;title=Code+Snippet+3+-+Create+post+slugs" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/10/code-snippet-3-create-post-slugs/&amp;t=Code+Snippet+3+-+Create+post+slugs" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/10/code-snippet-3-create-post-slugs/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/-hMIk5TIkgA" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/10/code-snippet-3-create-post-slugs/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Here we go – CDN Rewrites</title><link>http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/</link> <comments>http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/#comments</comments> <pubDate>Thu, 24 Sep 2009 10:15:34 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Freebies]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[cdn-rewrites]]></category> <category><![CDATA[plugin]]></category> <category><![CDATA[wordpress]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=325</guid> <description><![CDATA[Right after Free CDN was released, I got a request to enhance the plugin to support commercial CDNs &#8211; you know, those big guys like Akamai, Limelight, EdgeCast, Velocix etc. The implementation is not too complicated: specify an origin host, and rewrite it into a destination host. That origin is of course usually http://www.a-busy-site.com, and [...]]]></description> <content:encoded><![CDATA[<p>Right after Free CDN was released, I got a request to enhance the plugin to support commercial CDNs &#8211; you know, those big guys like Akamai, Limelight, EdgeCast, Velocix etc. The implementation is not too complicated: specify an origin host, and rewrite it into a destination host. That origin is of course usually http://www.a-busy-site.com, and the destination is something a CDN would provide you with: http://static.a-busy-site.com, or http://images.a-busy-site.com, or http://a-static-host.com etc. This way, all static contents will be served from that CDN host.</p><p>So, instead of developing the enhancement as a new feature for Free CDN, I decided to create a new plugin called CDN Rewrites. <span
id="more-325"></span>The implementation was not really a breeze, but in the end it worked rather smoothly.</p><p>I would like to thank <a
href="http://www.ezsite.us/">Mike Colburn</a> for the idea and being so kind enough to help me with all the documents and testing. Without him this plugin would never see the light.</p><p>Head <a
href="/wp-plugins/cdn-rewrites/">here for the plugin&#8217;s official page</a>.</p><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/&amp;title=Here+we+go+-+CDN+Rewrites" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=Here+we+go+-+CDN+Rewrites+-+http://tr.im/C9h5+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/&amp;title=Here+we+go+-+CDN+Rewrites" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/&amp;title=Here+we+go+-+CDN+Rewrites" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/&amp;title=Here+we+go+-+CDN+Rewrites" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/&amp;t=Here+we+go+-+CDN+Rewrites" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/23-XSTsKiHA" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/09/here-we-go-cdn-rewrites/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>First version of Free CDN WP plugin released!</title><link>http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/</link> <comments>http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/#comments</comments> <pubDate>Sun, 13 Sep 2009 17:03:10 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Freebies]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[free CDN]]></category> <category><![CDATA[plugin]]></category> <category><![CDATA[wordpress]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=314</guid> <description><![CDATA[Office life has its advantages and disadvantages. On one hand, it keeps me so exhausted and leaves me so little free time for other hobbies &#8211; I&#8217;m talking about my books, my December Flower guitar self-training, my photographic stuffs etc. On the other hand, it does improve my knowledge and skills with all those working [...]]]></description> <content:encoded><![CDATA[<p>Office life has its advantages and disadvantages. On one hand, it keeps me so exhausted and leaves me so little free time for other hobbies &#8211; I&#8217;m talking about my books, my <a
href="http://www.youtube.com/watch?v=sLPXAcAsLgo">December Flower</a> guitar self-training, my photographic stuffs etc. On the other hand, it does improve my knowledge and skills with all those working requirements.</p><p>I&#8217;m rather lucky to be working as an R&amp;D guy in my current company, thus got a (legal) chance to (legally) spare a lot of time for (sometimes illegal) new and cool stuffs. Among them is <abbr
title="Content Delivery Network">CDN</abbr>, a solution to distribute (mostly static) contents across a network and let end-user access their copies from the cloud instead of the central server itself, thus reduces bottle neck problems during peak hours. To enterprise websites like those of Microsoft, Yahoo, Amazon, eBay etc., this is vital, as the number of concurrent visitors and downloads very frequently exceeds millions. Some of them build their own CDN, when the others rather hire third party services to handle the load to save time and money. Most well known among these 3rd services are properly <a
href="http://www.akamai.com">Akamai</a> and <a
href="http://www.limelightnetworks.com/">Limelight</a>, though there is a vast of them, naturally. For instance, Windows 7 downloads (~2GB each!) were served through Akamai network, when the live internet broadcast of Barrack Obama&#8217;s inaugural speech was done with help from Limelight.<span
id="more-314"></span></p><p>CDN&#8217;s are great as they save websites from heavy loads thus reduces bandwidth and shorten that number on the webmasters&#8217; monthly bills. So why aren&#8217;t they used so popularly? Well, because in general they are expensive, as you can tell. Depending on each CDN&#8217;s price table and the data size, the cost of using a CDN may range from thousands a month, which is unaffordable for like 98% of our blog owners despite of being just a peanut to Yahoo and Microsoft. So unless he is earning millions from selling Photoshop brushes or Google ads on his personal blog, a blog owner would close his eyes and wait for that Digg wave to calm down instead of (dare) using a CDN and survive the typhoon. So the rich keep getting richer and the poor keep remaining poor, life is bitter huh?</p><p>Not really (whoo hoo!). Akamai and Limelight are big, but they are not the only. Aside of those commerical CDN&#8217;s, there are some free ones, like <a
href="http://www.coralcdn.org/">Coral</a>, which provide us with free CDN &#8211; means free bandwidth. Coral CDN uses peer-to-peer technology in its CDN architecture, thus eliminates the need of maintaining a system of expensive server clusters while still offering an CDN solution with acceptable speed and stability. Long story shot, we small blog owners can also have CDN (on pair with Microsoft, yay!).</p><p>It&#8217;s dead simple to get a content (being any of types, but most commonly static contents like images, video clips, music tracks, css, java scripts) served thought Coral. All you have to do is appending <em
style="color:#f00; font-weight:bold">.nyud.net</em> to the host name of that content&#8217;s URL and call it done. Try it <a
href="http://www.phoenixheart.net.nyud.net">here</a>, <a
href="http://www.thica.net.nyud.net">here</a>, and <a
href="http://www.digg.com.nyud.net">here</a> to see it yourself. The first time requested, the content is retrieved from the original server and cached on Coral&#8217;s distributed servers. From the second request on, visitors will get a copy of that content from Coral. If the content weights 100KB in size, you&#8217;ve just saved 100KB bandwidth. If you have 1000 requests, it&#8217;s 100MB of bandwidth saved. Piece of cake.</p><p>So I was very excited getting to know about Coral and free P2P CDN&#8217;s. Then I told myself: ok, it&#8217;s time for another WordPress plugin &#8211; a &#8220;Free CDN&#8221; one which will rewrite all static URLs (being images, css, js etc.) to take advantage of Coral. Easier said than done, but nonetheless, I managed to get it done (thank you very much <a
href="http://omninoggin.com/">Thaya</a>, for the great help on how to capture the whole WordPress HTML stream). Today is the day <a
href="/wp-plugins/free-cdn/">Free CDN WordPress plugin</a> is born, cheers! The concept behind this plugin is simple: it looks for static contents inside a WordPress page and rewrites them into Coral-ready format. For example, <em>http://www.yourblog.com/wp-contents/themes/one-room/images/sprites.png</em> will be rewritten into <em>http://www.yourblog.com<strong>.nyud.net</strong>/wp-contents/themes/one-room/images/sprites.png</em> and gets handled by Coral network.</p><p>If your blog is rather small and you don&#8217;t have to worry about bandwidth fee / peak times, then it may be not worth your time to lay an eye on this plugin. But if you have a big enough blog with tens of thousands of views a day, and the bandwidth fee is giving you some good nightmare, and the bottle necks are causing you some headaches, then why not <a
href="/wp-plugins/free-cdn/">give Free CDN a try</a>?</p><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/&amp;title=First+version+of+Free+CDN+WP+plugin+released%21" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=First+version+of+Free+CDN+WP+plugin+released%21+-+http://tr.im/C8Qx+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/&amp;title=First+version+of+Free+CDN+WP+plugin+released%21" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/&amp;title=First+version+of+Free+CDN+WP+plugin+released%21" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/&amp;title=First+version+of+Free+CDN+WP+plugin+released%21" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/&amp;t=First+version+of+Free+CDN+WP+plugin+released%21" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/_rt92ZN896c" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/09/free-cdn-first-version-released/feed/</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>Write your own URL shortener</title><link>http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/</link> <comments>http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/#comments</comments> <pubDate>Thu, 04 Jun 2009 11:53:37 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[twitter]]></category> <category><![CDATA[url shorten]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=305</guid> <description><![CDATA[With Twitter and its 140 characters limit leading all the trends, URL shortening services are in extremely high demand. We have seen TinyURL in the past, now Bit.ly, Is.gd, Tr.im and dozens of others are joining the party to share the cake. Honestly saying, to me they&#8217;re all the same (except for ➡.ws for its [...]]]></description> <content:encoded><![CDATA[<p>With Twitter and its 140 characters limit leading all the trends, URL shortening services are in extremely high demand. We have seen <a
href="http://tinyurl.com">TinyURL</a> in the past, now <a
href="http://bit.ly">Bit.ly</a>, <a
href="http://is.gd">Is.gd</a>, <a
href="http://tr.im">Tr.im</a> and dozens of others are joining the party to share the cake. Honestly saying, to me they&#8217;re all the same (except for <a
href="http://➡.ws">➡.ws</a> for its cool name). With that being said however, it is interesting how they are doing it out there &#8211; what mechanism / algorithm / buzzword-here?</p><p>I curiously did some &#8220;research&#8221;es (well, another buzzword), and it seems in order to create the shorten URLs they are following the same steps:</p><ol><li>Insert the original (long) URL into the database</li><li>Get the insert ID. If the URL already exists, take its row ID. Let&#8217;s say we got 123456.</li><li>Convert the ID 123456 into something even shorter, let&#8217;s say &#8220;am4k&#8221;</li><li>Make use of Apache&#8217;s mod_rewrite so that any request to <em>http://host.com/am4k</em> will reach <em>http://host.com/redir_script.php?code=am4k</em> instead.</li><li>In redir_script.php the value &#8220;am4k&#8221; is converted back into base10&#8217;s 123456 and the corresponding URL is queried back from database</li><li>If a URL is found, redirect the request to it.</li></ol><p>It is simple enough&#8230; except for step 3 and 4. Which conversion is there, and how is the .htaccess written? <span
id="more-305"></span></p><p>So I did some other <em>buzzword-here</em>, and it turned out <a
href="http://en.wikipedia.org/wiki/Base36">base36 encoding</a> is taking place. According to Wikipedia, &#8220;the choice of 36 is convenient in that the digits can be represented using the <a
class="mw-redirect" title="Hindu-Arabic numerals" href="http://en.wikipedia.org/wiki/Hindu-Arabic_numerals">Arabic numerals</a> 0-9 and the <a
title="Latin alphabet" href="http://en.wikipedia.org/wiki/Latin_alphabet">Latin letters</a> A-Z&#8221;. There is a handy conversion table too:</p><table
style="border:1px solid #ccc"><tbody><tr
align="right"><th>Decimal</th><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td><td>11</td><td>12</td><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td></tr><tr
align="right"><th>Base 36</th><td>0</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td><td>F</td><td>G</td><td>H</td></tr><tr><td
colspan="19"></td></tr><tr
align="right"><th>Decimal</th><td>18</td><td>19</td><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td><td>27</td><td>28</td><td>29</td><td>30</td><td>31</td><td>32</td><td>33</td><td>34</td><td>35</td></tr><tr><th>Base 36</th><td>I</td><td>J</td><td>K</td><td>L</td><td>M</td><td>N</td><td>O</td><td>P</td><td>Q</td><td>R</td><td>S</td><td>T</td><td>U</td><td>V</td><td>W</td><td>X</td><td>Y</td><td>Z</td></tr></tbody></table><p>Even better, PHP takes only one line of code to convert between decimal and base36, as shown on the same Wiki page:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$base_36</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ZAQFG&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Sample Base 36 Number</span>
<span style="color: #000088;">$decimal</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;7654321&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Sample Decimal Number</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">base_convert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$base_36</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">36</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs $base_36 converted to decimal</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">base_convert</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$decimal</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">36</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs $decimal converted to base 36</span></pre></td></tr></table></div><p>In our example, <em>123456</em> will be converted into its tidy equivalent base36 <em>2n9c</em> (not <em>am4k</em>, my very bad).</p><p>As for the mod_rewrite thing, as I am not a .htaccess master, I copied this thing that has been working perfectly so far from <a
href="http://www.codeigniter.com">CodeIgniter</a> forum:</p><div
class="wp_syntax"><table><tr><td
class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;">RewriteEngine On
RewriteBase <span style="color: #339933;">/</span>
&nbsp;
<span style="color: #666666; font-style: italic;">#Checks to see if the user is attempting to access a valid file,</span>
<span style="color: #666666; font-style: italic;">#such as an image or css document, if this isn't true it sends the</span>
<span style="color: #666666; font-style: italic;">#request to redir.php.</span>
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>f
RewriteCond <span style="color: #339933;">%</span><span style="color: #009900;">&#123;</span>REQUEST_FILENAME<span style="color: #009900;">&#125;</span> <span style="color: #339933;">!-</span>d
RewriteRule ^<span style="color: #009900;">&#40;</span><span style="color: #339933;">.*</span><span style="color: #009900;">&#41;</span>$ redir<span style="color: #339933;">.</span>php?code<span style="color: #339933;">=</span>$<span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#91;</span>L<span style="color: #009900;">&#93;</span>
&nbsp;</pre></td></tr></table></div><p>With this, all of the hardest parts are done, leave the remains being simple MySQL database, queries and HTML form handling. To save you from getting boring, I&#8217;ll omit it.</p><p><a
href="http://u.phoenixheart.net/" class="demo">Instead, let&#8217;s see a demo here</a><a
href="http://u.phoenixheart.net/source.zip" class="download" rel="nofollow">The source code of the demo are can be downloaded from the same server</a><br
/> <br
class="clear" /></p><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/&amp;title=Write+your+own+URL+shortener" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=Write+your+own+URL+shortener+-+http://tr.im/C9h7+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/&amp;title=Write+your+own+URL+shortener" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/&amp;title=Write+your+own+URL+shortener" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/&amp;title=Write+your+own+URL+shortener" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/&amp;t=Write+your+own+URL+shortener" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/CK1gt1X6ttI" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Referrer Detector 4.2.0.1 (and the IE pain)</title><link>http://www.phoenixheart.net/2009/05/referrer-detector-4-2-0-1-ie-pain/</link> <comments>http://www.phoenixheart.net/2009/05/referrer-detector-4-2-0-1-ie-pain/#comments</comments> <pubDate>Sat, 23 May 2009 04:28:14 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Freebies]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[ie]]></category> <category><![CDATA[Referrer Detector]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=302</guid> <description><![CDATA[On May 12 I managed to release the latest major version of Referrer Detector. As it was a complete rewritten, bugs are not a doubt expected. Since then, there has been 5 newer versions:4.0.1 &#8211; A hot fix to for allow_call_time_pass_reference problem. As for version 5, PHP passes function parameters by reference by default, thus [...]]]></description> <content:encoded><![CDATA[<p>On May 12 I managed to release <a
href="http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/">the latest major version</a> of <a
href="http://www.phoenixheart.net/wp-plugins/referrer-detector/">Referrer Detector</a>. As it was a complete rewritten, bugs are not a doubt expected. Since then, there has been 5 newer versions:</p><ul><li>4.0.1 &#8211; A hot fix to for allow_call_time_pass_reference problem. As for version 5, PHP passes function parameters by reference by default, thus eliminate the need of the prefix <em>&amp;</em>. This shouldn&#8217;t be anything big, if it didn&#8217;t generate a warning with any attempt to use <em>&amp;</em> and totally break my JavaScript&#8217;s eval().</li><li>4.0.2 &#8211; Some other hot fixes.</li><li>4.1.0 &#8211; &#8220;Related posts&#8221; feature implemented, and a tiny &#8220;Powered by Referrer Detector&#8221; line added into the welcome div. If you mind, both options can be turned off via Options panel &#8211; one-click. Also, the CSS was nearly completely changed.</li><li>4.1.0.1 &#8211; I accidentally (and stupidly) uploaded some 4.0.2 code in place of 4.1.0, and EVERYTHING was broken. This version is actually a quick roll-back, so the features were exactly the same with 4.0.2 &#8211; oops.</li><li>4.1.1 &#8211; This IS 4.1.0 <img
src='http://www.phoenixheart.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /></li></ul><p>So today I would like to announce the release of Referrer Detector 4.2.0.1 (code name: IE-PAIN). For this version we have another option on how to display the welcome box &#8211; in light box style. This is not a joke: I saw the light box in action in <em>my dream</em>, and started to implement it right when I woke up, basing on <a
href="http://www.ericmmartin.com/projects/simplemodal/">SimpleModal</a>. <a
href="http://en.wikipedia.org/wiki/Dmitri_Mendeleev">Mendeleev</a>&#8217;s style, haha. Actually I&#8217;m not sure if this welcome style fits normal needs, but for advertisements it seems to. At least it has some cool effects.<span
id="more-302"></span></p><p>If you wonder about its cool code name, here you go:</p><ul><li>First, version 4.1.1 has a serious bug (or is it IE?) which cause the whole admin panel to stop functioning. IE informally and informatively told me that at line 303, column 13, page ?rd.class.php there was a serious &#8220;Expected identifier, string or number&#8221; error. Actually it was a <em>&lt;/tr&gt;</em> closing tag there&#8230; but I had some experience with it already: must be <a
href="http://avnetlabs.com/javascript/expected-identifier-string-or-number-say-what">an extra comma in a JavaScript array</a>. The problem is, I have like thousands of JavaScript lines within this plugin, and so it took me one hour for a single comma. Thank you, IE, for the extra headache!</li><li>Then, I spent almost 2 hours to beautify the CSS. Everything was perfect, until I switched to IE. Then, I spent almost 2 hours more to beautify THE CSS on IE. Everything was perfect, until I switched back to Firefox. Then, I reverted back my files, and gave up, and went to sleep. Everything was perfect &#8211; damn it.</li><li>After the CSS is ok on all major browsers, the welcome box disappeared on IE. Just the same eval() code worked on Firefox and all others! I tried execScripts(), I tried jQuery plugins, I tried alert(), I tried document.write() &#8211; hopelessly. PHP returned the correct string, Base64 plugin decoded it with no hassle, but IE failed to properly insert it into DOM. Stupid. One more night, and I discovered that in some Ajax circumstances, $(&#8221;element&#8221;).replaceWith(html) doesn&#8217;t work in IE. Stupid. When $(&#8221;element&#8221;).after(html).remove() does. Stupid.</li><li>Troubles wouldn&#8217;t stop there. The message eventually showed up &#8211; kind of. It was there, but not visible. If the mouse was hovering a link &#8211; the line appeared. Moused out, and it&#8217;s gone. Have you ever imagined that? Do you have any clue why CSS <code>display: inline</code> fixed it? I&#8217;ve no idea.</li></ul><p>If it wasn&#8217;t IE, I would have released this new version 3 days ago. So I say (sorry, parental control recommended): Curse you, IE, go die pig!</p><p>Almost forgot it: nevertheless, Referrer Detector 4.2.0.1 is here, that&#8217;s what important <img
src='http://www.phoenixheart.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/05/referrer-detector-4-2-0-1-ie-pain/&amp;title=Referrer+Detector+4.2.0.1+%28and+the+IE+pain%29" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=Referrer+Detector+4.2.0.1+%28and+the+IE+pain%29+-+http://tr.im/Cn5C+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/05/referrer-detector-4-2-0-1-ie-pain/&amp;title=Referrer+Detector+4.2.0.1+%28and+the+IE+pain%29" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/05/referrer-detector-4-2-0-1-ie-pain/&amp;title=Referrer+Detector+4.2.0.1+%28and+the+IE+pain%29" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/05/referrer-detector-4-2-0-1-ie-pain/&amp;title=Referrer+Detector+4.2.0.1+%28and+the+IE+pain%29" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/05/referrer-detector-4-2-0-1-ie-pain/&amp;t=Referrer+Detector+4.2.0.1+%28and+the+IE+pain%29" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/05/referrer-detector-4-2-0-1-ie-pain/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/4RU-Lva-1VE" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/05/referrer-detector-4-2-0-1-ie-pain/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Referrer Detector 4.0 is out!</title><link>http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/</link> <comments>http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/#comments</comments> <pubDate>Tue, 12 May 2009 16:51:48 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Freebies]]></category> <category><![CDATA[plugin]]></category> <category><![CDATA[Referrer Detector]]></category> <category><![CDATA[wordpress]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=290</guid> <description><![CDATA[Finally I&#8217;ve made it! Referrer Detector version 4.0 is now out! I decided to mark this as a major version increment because of these reasons:The code has been COMPLETELY re-written from scratch. As of the previous versions, it was one big file that handled everything from admin to front-end control. Needless to say how inconvenient [...]]]></description> <content:encoded><![CDATA[<p>Finally I&#8217;ve made it! Referrer Detector version 4.0 is now out! I decided to mark this as a major version increment because of these reasons:</p><ol><li>The code has been COMPLETELY re-written from scratch. As of the previous versions, it was one big file that handled everything from admin to front-end control. Needless to say how inconvenient this approach had become when bug fixes and new features were added&#8230; too bad that I have decided to throw all away and build a brand new <abbr
title="Object Oriented Programming">OOP</abbr> Referrer Detector. Well, it was a looong and tough way, but I&#8217;ve never looked back!</li><li>The data are now in (ahem) database. I was thinking (and convincing myself) that a JavaScript file is faster, as it reduces the number of database requests. But with time, it becomes too bloat and too hard for me to track bugs as well as to add improvements. So I told myself: hell with this sacrifice, I better obey the power of MySQL.</li><li>The biggest new feature that I&#8217;m really excited of is the ability to add <em>localized messages</em>. In the past, your users were welcomed the the same (English) greetings regardless of which country they were from. Now you can specify a localized message for those from Vietnam, another for Brazilians, Portuguese, and so on. The plugin will try to detect users&#8217; country and decide which message to show. Isn&#8217;t it cool?</li><li>The second new feature is the ability to backup and restore stuffs, including entries, excluded URLs, and options. For restoring, in order to keep the administration panel AJAX&#8217;ed, I go with <a
href="http://www.uploadify.com/">Uploadify</a>, a wonderful jQuery file upload plugin. This plugin uses a bit of Flash, but no worries, it will still works if your browser has no Flash player installed.</li><li>For the Stats panel, there were two problems that caused me much of headache. One is Google TLDs which are hundreds in number thus totally ruined the chart. The other is the chart itself: PHP/SWF Charts library is too darn heavy and often broke my SVN commits. So I wrote some code to group those annoying TLDs into one group, and use <a
href="http://code.google.com/apis/chart/">Google Chart</a> instead.</li><li>In the admin panel, I added a &#8220;Support this plugin!&#8221; tab. Just a bit about myself, like &#8220;Follow me on Twitter&#8221;. Hope you aren&#8217;t pissed of with this change.</li></ol><p>As usual, the plugin is downloadable at <a
href="http://wordpress.org/extend/plugins/referrer-detector/">WordPress Codex</a>. Your comments are always welcome here and in the <a
href="/wp-plugins/referrer-detector/">plugin page</a>. Let me know if you&#8217;re happy with the new version, or about the bugs you encounter!</p><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/&amp;title=Referrer+Detector+4.0+is+out%21" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=Referrer+Detector+4.0+is+out%21+-+http://tr.im/Cn5D+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/&amp;title=Referrer+Detector+4.0+is+out%21" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/&amp;title=Referrer+Detector+4.0+is+out%21" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/&amp;title=Referrer+Detector+4.0+is+out%21" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/&amp;t=Referrer+Detector+4.0+is+out%21" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/5njAnqessUM" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/05/referrer-detector-4-is-out/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Code snippet #2 – Shorten Links Using Bit.ly</title><link>http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/</link> <comments>http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/#comments</comments> <pubDate>Sat, 09 May 2009 02:29:26 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[bit.ly]]></category> <category><![CDATA[snippet]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=285</guid> <description><![CDATA[Following the first snippet, here is another small one to shorten a link using Bit.ly. I chose Bit.ly over other link shortening services due to a simple reason: Twitter uses it as default (by now).
Code snippet #2 &#8211; Shorten Links Using Bit.ly
Purpose
Create a shortened URL using Bit.ly. This is extremely useful for your visitors, as [...]]]></description> <content:encoded><![CDATA[<p>Following <a
href="/2009/05/code-snippet-1-get-latest-tweet/">the first snippet</a>, here is another small one to shorten a link using <a
href="http://bit.ly">Bit.ly</a>. I chose Bit.ly over other link shortening services due to a simple reason: Twitter uses it as default (by now).</p><h3>Code snippet #2 &#8211; Shorten Links Using Bit.ly</h3><h3>Purpose</h3><p>Create a shortened URL using Bit.ly. This is extremely useful for your visitors, as they don&#8217;t have to look any further &#8211; just grab it to use right away.</p><h3>Requirements</h3><ul><li>A Bit.ly account (and API key). You can <a
href="http://bit.ly/account/register?rd=/">create one for free</a> at their site in seconds. Once logged in, head to Account section and look for a long ugly string starting with &#8220;R_&#8221;.</li><li>PHP 5 &gt;= 5.2.0. Your host doesn&#8217;t support PHP 5? Claim your money back and find another host.</li></ul><h3>The Code</h3><p><span
id="more-285"></span></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
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_shortened_url<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$bitly_api_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR KEY HERE'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$bitly_username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR BIT.LY USERNAME HERE'</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://api.bit.ly/shorten?version=2.0.1&amp;longUrl=<span style="color: #006699; font-weight: bold;">$url</span>&amp;login=<span style="color: #006699; font-weight: bold;">$bitly_username</span>&amp;apiKey=<span style="color: #006699; font-weight: bold;">$bitly_api_key</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> json_decode<span style="color: #009900;">&#40;</span><span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errorCode</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> 
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$url</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">shortUrl</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now use it</span>
<span style="color: #990000;">echo</span> get_shortened_url<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.example.com/this-is-a-stupid-long-long-long-url.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// how about WordPress?</span>
<span style="color: #666666; font-style: italic;">// Just place the function above somewhere in your functions.php theme page and then</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 1. inside The Loop</span>
<span style="color: #990000;">echo</span> get_shortened_url<span style="color: #009900;">&#40;</span>get_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 2. outside The Loop</span>
<span style="color: #990000;">echo</span> get_shortened_url<span style="color: #009900;">&#40;</span>get_permalink<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;</pre></td></tr></table></div><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/&amp;title=Code+snippet+%232+-+Shorten+Links+Using+Bit.ly" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=Code+snippet+%232+-+Shorten+Links+Using+Bit.ly+-+http://tr.im/Cn5E+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/&amp;title=Code+snippet+%232+-+Shorten+Links+Using+Bit.ly" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/&amp;title=Code+snippet+%232+-+Shorten+Links+Using+Bit.ly" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/&amp;title=Code+snippet+%232+-+Shorten+Links+Using+Bit.ly" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/&amp;t=Code+snippet+%232+-+Shorten+Links+Using+Bit.ly" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/TMkMkyS4Jq4" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/05/code-snippet-2-shorten-links-using-bitly/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Code snippet #1 – Get Latest Tweet</title><link>http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/</link> <comments>http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/#comments</comments> <pubDate>Wed, 06 May 2009 12:29:41 +0000</pubDate> <dc:creator>phoenix.heart</dc:creator> <category><![CDATA[Featured]]></category> <category><![CDATA[Server stuffs]]></category> <category><![CDATA[snippet]]></category> <category><![CDATA[twitter]]></category><guid isPermaLink="false">http://www.phoenixheart.net/?p=271</guid> <description><![CDATA[Ok so I&#8217;m following some guy&#8217;s recommendation (sorry, I really forgot his name as well as his blog address) to try to post something useful even when I don&#8217;t have anything to blog about. How about a series of code snippets? To some they are old but to the others they may help &#8211; for [...]]]></description> <content:encoded><![CDATA[<p>Ok so I&#8217;m following some guy&#8217;s recommendation (sorry, I really forgot his name as well as his blog address) to try to post something useful even when I don&#8217;t have anything to blog about. How about a series of code snippets? To some they are old but to the others they may help &#8211; for example to myself two months later that is.</p><div
class="snipper-name"><h3>Code Snippet #1 &#8211; Get Latest Tweet</h3></div><div
class="snipper-purpose"><h3>Purpose</h3><ul><li>Get your latest tweet from Twitter to show up instead of an unconvincing &#8220;Follow Me&#8221; link -or-</li><li>Display your idol&#8217;s latest tweet to wow your readers</li></ul></div><div
class="snipper-require"><h3>Requirements</h3><ul><li>A Twitter account. Don&#8217;t have one? Head <a
href="http://twitter.com/signup">here</a> or <a
href="http://www.iliveunderarock.com/">here</a></li></ul></div><p><span
id="more-271"></span></p><div
class="snipper-code"><h3>The Code</h3><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
</pre></td><td
class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the function</span>
<span style="color: #009933; font-style: italic;">/**
 * @desc Get latest tweet from a Twitter account
 * @param string The account's username
 * @return string The tweet
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> get_latest_tweet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$username</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://search.twitter.com/search.atom?q=from:<span style="color: #006699; font-weight: bold;">$username</span>&amp;rpp=1&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;content type=&quot;html&quot;&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&lt;/content&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</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: #b1b100;">return</span> <span style="color: #990000;">html_entity_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</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: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now use it</span>
<span style="color: #000088;">$my_username</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Phoenixheart'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">echo</span> get_latest_tweet<span style="color: #009900;">&#40;</span><span style="color: #000088;">$my_username</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></div><div
class="sexy-bookmarks sexy-bookmarks-expand sexy-bookmarks-bg-sexy"><ul
class="socials"><li
class="sexy-digg"><a
href="http://digg.com/submit?phase=2&amp;url=http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/&amp;title=+Code+snippet+%231+-+Get+Latest+Tweet" rel="nofollow" title="Digg this!">Digg this!</a></li><li
class="sexy-twitter"><a
href="http://twitter.com/home?status=+Code+snippet+%231+-+Get+Latest+Tweet+-+http://tr.im/Cn5F+(via+@Phoenixheart)" rel="nofollow" title="Tweet This!">Tweet This!</a></li><li
class="sexy-delicious"><a
href="http://del.icio.us/post?url=http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/&amp;title=+Code+snippet+%231+-+Get+Latest+Tweet" rel="nofollow" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="sexy-reddit"><a
href="http://reddit.com/submit?url=http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/&amp;title=+Code+snippet+%231+-+Get+Latest+Tweet" rel="nofollow" title="Share this on Reddit">Share this on Reddit</a></li><li
class="sexy-stumbleupon"><a
href="http://www.stumbleupon.com/submit?url=http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/&amp;title=+Code+snippet+%231+-+Get+Latest+Tweet" rel="nofollow" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a></li><li
class="sexy-facebook"><a
href="http://www.facebook.com/share.php?u=http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/&amp;t=+Code+snippet+%231+-+Get+Latest+Tweet" rel="nofollow" title="Share this on Facebook">Share this on Facebook</a></li><li
class="sexy-comfeed"><a
href="http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/feed" rel="nofollow" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/phoenixheart/~4/F1zLVmWnplk" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.phoenixheart.net/2009/05/code-snippet-1-get-latest-tweet/feed/</wfw:commentRss> <slash:comments>11</slash:comments> </item> </channel> </rss><!--
This site's performance optimized by W3 Total Cache:

W3 Total Cache improves the user experience of your blog by caching
frequent operations, reducing the weight of various files and providing
transparent content delivery network integration.

Learn more about our WordPress Plugins: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk

Served from: wp06.secure-dns.net @ 2009-11-12 15:31:40 -->
