<?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>CatsWhoCode.com</title> <link>http://www.catswhocode.com/blog</link> <description>Web Development Blog</description> <lastBuildDate>Wed, 25 Aug 2010 00:26:12 +0000</lastBuildDate> <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" type="application/rss+xml" href="http://feeds.feedburner.com/Catswhocode" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="catswhocode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">Catswhocode</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item><title>How to easily create charts using jQuery and HTML5</title><link>http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5</link> <comments>http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5#comments</comments> <pubDate>Mon, 23 Aug 2010 08:30:22 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[Web development]]></category> <category><![CDATA[javascript]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3858</guid> <description><![CDATA[For years, Flash was the only solution to display a dynamic chart on a website. But thanks to modern techniques, the dying Flash isn't needed anymore. In this tutorial, I'll show you how easy it is to transform a basic HTML table into a profesionnal looking chart using visualize.js, a very useful jQuery plugin.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5">How to easily create charts using jQuery and HTML5</a></p> ]]></description> <content:encoded><![CDATA[<h2>Step 1: Preparing files</h2><p>Here we go! The first thing to do is obviously to create a directory on your Mac (or PC, nobody&#8217;s perfect <img
src='http://www.catswhocode.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ). You should name it <em>chart</em> for example. Once done, <a
href="http://code.google.com/p/dwpe/downloads/list">download the required Javascript and CSS files</a> and save it on your hard drive.</p><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/08/chart.png" alt="" /></p><p>Now, create a new html document, named <em>charts.html</em>.  Copy the html structure below, and paste it into your newly created <em>charts.html</em> file.</p><pre class="brush: html">&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;meta http-equiv="content-type" content="text/html;charset=UTF-8" /&gt;
	&lt;title&gt;Charts!&lt;/title&gt;       

&lt;/head&gt;

&lt;body&gt;

&lt;/body&gt;
&lt;/html&gt;</pre><p>Did you notice that I used the &lt;!DOCTYPE html&gt; doctype? It is the right doctype to use, as the javscript code we&#8217;ll add later will turn a html table into a HTML 5 canvas.</p><h2>Step 2: Adding data</h2><p>Now that we downloaded the javascript files and created an html document, it&#8217;s time to add data. Copy the code below and paste it within the &lt;<em>body&gt;</em> and <em>&lt;/body&gt;</em> tags of your <em>charts.html</em> file.</p><pre class="brush: html">&lt;table&gt;
	&lt;caption&gt;Visits from August 16 to August 21&lt;/caption&gt;
	&lt;thead&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;/td&gt;
			&lt;th scope="col"&gt;Monday&lt;/th&gt;
			&lt;th scope="col"&gt;Tuesday&lt;/th&gt;
			&lt;th scope="col"&gt;Wednesday&lt;/th&gt;
			&lt;th scope="col"&gt;Thursday&lt;/th&gt;
			&lt;th scope="col"&gt;Friday&lt;/th&gt;
			&lt;th scope="col"&gt;Saturday&lt;/th&gt;
			&lt;th scope="col"&gt;Sunday&lt;/th&gt;
		&lt;/tr&gt;
	&lt;/thead&gt;
	&lt;tbody&gt;

		&lt;tr&gt;
			&lt;th scope="row"&gt;CatsWhoCode.com&lt;/th&gt;
			&lt;td&gt;12541&lt;/td&gt;
			&lt;td&gt;11204&lt;/td&gt;
			&lt;td&gt;11354&lt;/td&gt;
			&lt;td&gt;10058&lt;/td&gt;
			&lt;td&gt;9871&lt;/td&gt;
			&lt;td&gt;8254&lt;/td&gt;
			&lt;td&gt;5477&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;th scope="row"&gt;WpRecipes.com&lt;/th&gt;
			&lt;td&gt;9855&lt;/td&gt;
			&lt;td&gt;8870&lt;/td&gt;
			&lt;td&gt;8731&lt;/td&gt;
			&lt;td&gt;7488&lt;/td&gt;
			&lt;td&gt;8159&lt;/td&gt;
			&lt;td&gt;6547&lt;/td&gt;
			&lt;td&gt;4512&lt;/td&gt;
		&lt;/tr&gt;
		&lt;tr&gt;
			&lt;th scope="row"&gt;CatsWhoBlog.com&lt;/th&gt;
			&lt;td&gt;3241&lt;/td&gt;
			&lt;td&gt;2544&lt;/td&gt;
			&lt;td&gt;2597&lt;/td&gt;
			&lt;td&gt;3108&lt;/td&gt;
			&lt;td&gt;2114&lt;/td&gt;
			&lt;td&gt;2045&lt;/td&gt;
			&lt;td&gt;950&lt;/td&gt;
		&lt;/tr&gt;
	&lt;/tbody&gt;
&lt;/table&gt;</pre><p>Of course, feel free to add your own data to make the example more interesting for you.</p><h2>Step 3: Let the magic happen</h2><p>Alright, now we have a bunch of downloaded files and an html document. It&#8217;s time to merge them all together and finally generate the chart.<br
/> Unzip the downloaded file and open the extracted directory. Copy the following files into your <em>chart</em> directory.</p><ul><li>charting/css/basic.css</li><li>charting/css/visualize.css</li><li>charting/css/visualize-light.css</li><li>charting/js/visualize.js</li></ul><p>Once done, we obviously have to link the css and javascript files to our document. Paste the following after the &lt;title&gt; tag of the document:</p><pre class="brush: html">&lt;link href="basic.css" type="text/css" rel="stylesheet" /&gt;
&lt;link href="visualize.css" type="text/css" rel="stylesheet" /&gt;
&lt;link href="visualize-light.css" type="text/css" rel="stylesheet" /&gt;
&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="visualize.js"&gt;&lt;/script&gt;</pre><p>It&#8217;s time to give life to our chart. Paste the final piece of code after the script calls:</p><pre class="brush: javascript">&lt;script type="text/javascript"&gt;
	$(function(){
		$('table').visualize();
	});
&lt;/script&gt;</pre><p>Once you saved the file, your HTML table should be displayed along with a good looking chart. If you don&#8217;t want the table to be visible, simply hide it using the <em>display:none</em> css property.</p><h2>Additional explanations</h2><p>Generating bar charts is definitely great, but what if your preference goes to a pie chart? No problem, visualize.js allows 4 different type of charts: Bar, area, pie and line.</p><p>Changing the default type is pretty easy: Just add the type parameter as shown below:</p><pre class="brush: javascript">$('table').visualize({type: 'pie'});</pre><p>Cool, isn&#8217;t it? Visualize.js accepts even more parameters to make sure your chart will look exactly how you want. Here are the parameters which can be used:</p><ul><li><strong>type:</strong> <em>string</em>. Accepts &#8216;bar&#8217;, &#8216;area&#8217;, &#8216;pie&#8217;, &#8216;line&#8217;. Default: &#8216;bar&#8217;.</li><li><strong>width:</strong> <em>number</em>. Width of chart. Defaults to table width</li><li><strong>height:</strong> <em>number</em>. Height of chart. Defaults to table height</li><li><strong>appendTitle:</strong> <em>boolean</em>. Add title to chart. Default: true.</li><li><strong>title:</strong> <em>string</em>. Title for chart. Defaults to text of table&#8217;s Caption element.</li><li><strong>appendKey:</strong> <em>boolean</em>. Adds the color key to the chart. Default: true.</li><li><strong>colors:</strong> <em>array</em>. Array items are hex values, used in order of appearance. Default: ['#be1e2d','#666699','#92d5ea','#ee8310','#8d10ee','#5a3b16','#26a4ed','#f45a90','#e9e744']</li><li><strong>textColors:</strong> <em>array</em>. Array items are hex values. Each item corresponds with colors array. null/undefined items will fall back to CSS text color. Default: [].</li><li><strong>parseDirection:</strong> <em>string</em>. Direction to parse the table data. Accepts &#8216;x&#8217; and &#8216;y&#8217;. Default: &#8216;x&#8217;.</li><li><strong>pieMargin:</strong> <em>number</em>. Space around outer circle of Pie chart. Default: 20.</li><li><strong>pieLabelPos:</strong> <em>string</em>. Position of text labels in Pie chart. Accepts &#8216;inside&#8217; and &#8216;outside&#8217;. Default: &#8216;inside&#8217;.</li><li><strong>lineWeight:</strong> <em>number</em>. Stroke weight for lines in line and area charts. Default: 4.</li><li><strong>barGroupMargin:</strong> <em>number</em>. Space around each group of bars in a bar chart. Default: 10.</li><li><strong>barMargin:</strong> number. Creates space around bars in bar chart (added to both sides of each bar). Default: 1</li></ul><p>That&#8217;s all for today. Have fun with the charts <img
src='http://www.catswhocode.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5">How to easily create charts using jQuery and HTML5</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5&amp;title=How+to+easily+create+charts+using+jQuery+and+HTML5" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5&amp;title=How+to+easily+create+charts+using+jQuery+and+HTML5&amp;body=For%20years%2C%20Flash%20was%20the%20only%20solution%20to%20display%20a%20dynamic%20chart%20on%20a%20website.%20But%20thanks%20to%20modern%20techniques%2C%20the%20dying%20Flash%20isn%27t%20needed%20anymore.%20In%20this%20tutorial%2C%20I%27ll%20show%20you%20how%20easy%20it%20is%20to%20transform%20a%20basic%20HTML%20table%20into%20a%20profesionnal%20looking%20chart%20using%20visualize.js%2C%20a%20very%20useful%20jQuery%20plugin." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5&amp;title=How+to+easily+create+charts+using+jQuery+and+HTML5" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5&amp;title=How+to+easily+create+charts+using+jQuery+and+HTML5" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5&amp;t=How+to+easily+create+charts+using+jQuery+and+HTML5" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5&amp;title=How+to+easily+create+charts+using+jQuery+and+HTML5&amp;body=For%20years%2C%20Flash%20was%20the%20only%20solution%20to%20display%20a%20dynamic%20chart%20on%20a%20website.%20But%20thanks%20to%20modern%20techniques%2C%20the%20dying%20Flash%20isn%27t%20needed%20anymore.%20In%20this%20tutorial%2C%20I%27ll%20show%20you%20how%20easy%20it%20is%20to%20transform%20a%20basic%20HTML%20table%20into%20a%20profesionnal%20looking%20chart%20using%20visualize.js%2C%20a%20very%20useful%20jQuery%20plugin." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/pSyeP4d84as" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/how-to-easily-create-charts-using-jquery-and-html5/feed</wfw:commentRss> <slash:comments>8</slash:comments> </item> <item><title>8 examples of stunning CSS3 text effects</title><link>http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects</link> <comments>http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects#comments</comments> <pubDate>Sun, 15 Aug 2010 16:36:38 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[Web development]]></category> <category><![CDATA[css3]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3835</guid> <description><![CDATA[Among many other things, CSS3 has lots of nice effects to enhance the typography of your website. In this article, I have compiled 8 extremely promising typography techniques done using CSS3.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects">8 examples of stunning CSS3 text effects</a></p> ]]></description> <content:encoded><![CDATA[<h2>How to Create Inset Typography with CSS3</h2><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/08/1.jpg" alt="" /><br
/> Just two years ago, we all used Photoshop to create beautiful inset typography. Now, you can do it using only CSS3. This great tutorial will help you getting started.<br
/> <strong>View tutorial: <a
href="http://sixrevisions.com/css/how-to-create-inset-typography-with-css3/">http://sixrevisions.com/css/how-to-create-inset-typography-with-css3/</a></strong></p><h2>Create Beautiful CSS3 Typography</h2><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/08/2.jpg" alt="" /><br
/> Technically speaking, styling text is very simple. The hard part is the artistic part: How to make text easy to read and look good? This is the focus of the tutorial, which is a must read for all web developers and designers.<br
/> <strong>View tutorial: <a
href="http://blog.echoenduring.com/2010/05/13/create-beautiful-css3-typography/">http://blog.echoenduring.com/2010/05/13/create-beautiful-css3-typography/</a></strong></p><h2>Create a Letterpress Effect with CSS Text-Shadow</h2><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/08/3.jpg" alt="" /><br
/> The &#8220;letterpress&#8221; effect is very popular in web design. Many people do it using Photoshop, but you can do it extremely easily using CSS3 only. How? Chris Spooner shows you how in this interesting article.<br
/> <strong>View tutorial: <a
href="http://line25.com/tutorials/create-a-letterpress-effect-with-css-text-shadow">http://line25.com/tutorials/create-a-letterpress-effect-with-css-text-shadow</a></strong></p><h2>How to Create a Cool Anaglyphic Text Effect with CSS</h2><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/08/4.jpg" alt="" /><br
/> Chris Spooner again! This time, the talented British web designer comes back with a tutorial showing you how you can create a anaglyphic effect with CSS. Not sure I&#8217;ll use it on a live site, but it&#8217;s always interesting to know how to do it.<br
/> <strong>View tutorial: <a
href="http://line25.com/tutorials/how-to-create-a-cool-anaglyphic-text-effect-with-css">http://line25.com/tutorials/how-to-create-a-cool-anaglyphic-text-effect-with-css</a></strong></p><h2>Text Rotation with CSS</h2><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/08/6.jpg" alt="" /><br
/> Why should text always be displayed horizontally? CSS3 has the <em>transform: rotate</em> property, which allow you to rotate any elements, including text. The following tutorial, written by Jonathan Snook, will show how in details how to achieve an awesome rotated text effect.<br
/> <strong>View tutorial: <a
href="http://snook.ca/archives/html_and_css/css-text-rotation">http://snook.ca/archives/html_and_css/css-text-rotation</a></strong></p><h2>Text Embossing Technique With CSS</h2><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/08/5.jpg" alt="" /><br
/> One more technique I&#8217;ve done exclusively in photoshop in the past. Thanks to CSS3, I&#8217;m now able to do it entirely in CSS.<br
/> <strong>View tutorial: <a
href="http://www.reynoldsftw.com/2009/03/text-embossing-technique-with-css/">http://www.reynoldsftw.com/2009/03/text-embossing-technique-with-css/</a></strong></p><h2>Adding an outline to your text using the CSS3 text-stroke property</h2><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/08/7.jpg" alt="" /><br
/> Although this technique only works in webkit for now, I must admit that I really love it. You can add an outline to text, and even better, use transparent text with a solid outline.<br
/> <strong>View tutorial: <a
href="http://www.cardeo.ca/2010/adding-an-outline-to-your-text-using-the-css3-text-stroke-property">http://www.cardeo.ca/2010/adding-an-outline-to-your-text-using-the-css3-text-stroke-property</a></strong></p><h2>CSS textured text</h2><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/08/8.jpg" alt="" /><br
/> Ok, this isn&#8217;t new, and this isn&#8217;t CSS3, but this example is so great that I can&#8217;t not feature it on this post. Nick La from WebDesignerWall explains how you can make gradients or textured texts using CSS.<br
/> <strong>View tutorial: <a
href="http://www.webdesignerwall.com/tutorials/css-gradient-text-effect/">http://www.webdesignerwall.com/tutorials/css-gradient-text-effect/</a></strong></p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects">8 examples of stunning CSS3 text effects</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects&amp;title=8+examples+of+stunning+CSS3+text+effects" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects&amp;title=8+examples+of+stunning+CSS3+text+effects&amp;body=Among%20many%20other%20things%2C%20CSS3%20has%20lots%20of%20nice%20effects%20to%20enhance%20the%20typography%20of%20your%20website.%20In%20this%20article%2C%20I%20have%20compiled%208%20extremely%20promising%20typography%20techniques%20done%20using%20CSS3." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects&amp;title=8+examples+of+stunning+CSS3+text+effects" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects&amp;title=8+examples+of+stunning+CSS3+text+effects" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects&amp;t=8+examples+of+stunning+CSS3+text+effects" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects&amp;title=8+examples+of+stunning+CSS3+text+effects&amp;body=Among%20many%20other%20things%2C%20CSS3%20has%20lots%20of%20nice%20effects%20to%20enhance%20the%20typography%20of%20your%20website.%20In%20this%20article%2C%20I%20have%20compiled%208%20extremely%20promising%20typography%20techniques%20done%20using%20CSS3." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/4aodForQ02M" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/8-examples-of-stunning-css3-text-effects/feed</wfw:commentRss> <slash:comments>11</slash:comments> </item> <item><title>8 CSS preprocessors to speed up development time</title><link>http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time</link> <comments>http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time#comments</comments> <pubDate>Mon, 09 Aug 2010 14:05:10 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[Web development]]></category> <category><![CDATA[css]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3803</guid> <description><![CDATA[CSS allow you to do lots of things, but some features are missing to developers. What about variable, constants, and general faster syntax? As normal CSS can't do that, people created preprocessors to allow the use of variables on CSS files and then parse it to regular stylesheets.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time">8 CSS preprocessors to speed up development time</a></p> ]]></description> <content:encoded><![CDATA[<h2>Less CSS</h2><p>Less is probably the most well known CSS preprocessor. It allow a simplified syntax and the use of variables. Less CSS is for the Ruby programming language, however it looks like Aaron Russel created <a
href="http://github.com/aaronrussell/less_php_cacher">an extension</a> for creating cached stylesheets your PHP projects can use.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/07/lesscss.png" alt="" /><br
/> <strong>Get it: <a
href="http://lesscss.org/">http://lesscss.org</a></strong></p><h2>Sass</h2><p>On their website, Sass claims to make CSS fun again. To be honest, I must admit that what this project is capable of is very interesting. Like Less CSS, it allow the use of variables and have a simplified syntax. Sass is definitely a great tool, unfortunely only available for Ruby, as far as I know.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/07/sass.png" alt="" /><br
/> <strong>Get it: <a
href="http://sass-lang.com/">http://sass-lang.com/</a></strong></p><h2>Turbine</h2><p>If like me, you&#8217;re a <a
href="http://www.phpsnippets.info">PHP Lover</a>, here is a css preprocessor made for your favorite language. I haven&#8217;t tested it yet, but Turbine looks very cool. It allow a minimal syntax, automatically gzip multiple css, fix cross-browser issues, and a lot more. A must check if you&#8217;re into PHP.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/07/turbine.png" alt="" /><br
/> <strong>Get it: <a
href="http://turbine.peterkroener.de/index.php">http://turbine.peterkroener.de/index.php</a></strong></p><h2>Switch CSS</h2><p>Switch is a full featured, production ready CSS preprocessor. It runs under Apache with mod_python, or as an environment-agnostic command line tool.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/07/switchcss.png" alt="" /><br
/> <strong>Get it: <a
href="http://sourceforge.net/projects/switchcss/">http://sourceforge.net/projects/switchcss/</a></strong></p><h2>CSS Cacheer</h2><p>CSS Cacheer is a very cool preprocessor which allows developers to create plugins. It requires PHP and Apache with mod_deflate and mod_rewrite in order to work.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/07/csscacheer.png" alt="" /><br
/> <strong>Get it: <a
href="http://retired.haveamint.com/archive/2008/05/30/check_out_css_cacheer">http://retired.haveamint.com/archive/2008/05/30/check_out_css_cacheer</a></strong></p><h2>CSS Preprocessor</h2><p>Another interesting preprocessor, written in PHP 5. Among other things, this tool allow you to use expressions such as <em>margin-left: (200px * 3/2 – 10px);</em> in your stylesheets.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/07/csspreprocessor.png" alt="" /><br
/> <strong>Get it: <a
href="http://pornel.net/css">http://pornel.net/css</a></strong></p><h2>DT CSS</h2><p>DtCSS speeds up CSS coding by extending the features to CSS. Such as nested selectors, color mixing and more. DtCSS reads the CSS file with special syntax written for DtCSS, and outputs the standard CSS. It also comes with a smart caching system.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/07/dtcss.png" alt="" /><br
/> <strong>Get it: <a
href="http://code.google.com/p/dtcss/">http://code.google.com/p/dtcss/</a></strong></p><h2>CSS PP</h2><p>Unfortunely, CSS PP is still in alpha status, but the authors says the code will be released very soon. One of the good points of this projects is that it will be available in PHP, Python and Ruby. Great news for developpers who work with all of these languages.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/07/csspp.png" alt="" /><br
/> <strong>Get it: <a
href="http://csspp.org/">http://csspp.org/</a></strong></p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time">8 CSS preprocessors to speed up development time</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time&amp;title=8+CSS+preprocessors+to+speed+up+development+time" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time&amp;title=8+CSS+preprocessors+to+speed+up+development+time&amp;body=CSS%20allow%20you%20to%20do%20lots%20of%20things%2C%20but%20some%20features%20are%20missing%20to%20developers.%20What%20about%20variable%2C%20constants%2C%20and%20general%20faster%20syntax%3F%20As%20normal%20CSS%20can%27t%20do%20that%2C%20people%20created%20preprocessors%20to%20allow%20the%20use%20of%20variables%20on%20CSS%20files%20and%20then%20parse%20it%20to%20regular%20stylesheets." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time&amp;title=8+CSS+preprocessors+to+speed+up+development+time" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time&amp;title=8+CSS+preprocessors+to+speed+up+development+time" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time&amp;t=8+CSS+preprocessors+to+speed+up+development+time" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time&amp;title=8+CSS+preprocessors+to+speed+up+development+time&amp;body=CSS%20allow%20you%20to%20do%20lots%20of%20things%2C%20but%20some%20features%20are%20missing%20to%20developers.%20What%20about%20variable%2C%20constants%2C%20and%20general%20faster%20syntax%3F%20As%20normal%20CSS%20can%27t%20do%20that%2C%20people%20created%20preprocessors%20to%20allow%20the%20use%20of%20variables%20on%20CSS%20files%20and%20then%20parse%20it%20to%20regular%20stylesheets." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/QfViSWPQDNk" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/8-css-preprocessors-to-speed-up-development-time/feed</wfw:commentRss> <slash:comments>12</slash:comments> </item> <item><title>WordPress: How to insert data programmatically</title><link>http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically</link> <comments>http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically#comments</comments> <pubDate>Sun, 01 Aug 2010 17:56:28 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[WordPress]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3789</guid> <description><![CDATA[Recently, <a
href="http://www.webdevcat.com">a client of mine</a> asked me to write an article importer for his WordPress powered site, which was a very interesting project for me. In this article, I'll show you how you can easily add data (posts, comments, categories, etc) to your WordPress blog, without any manual effort.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically">WordPress: How to insert data programmatically</a></p> ]]></description> <content:encoded><![CDATA[<h2>Inserting posts</h2><p>Do you remember back in 2008, when I created WP Vote? This site was the first (as far as I know) social voting site created 100% within WordPress. Users were able to submit a story, which was automatically published on the blog.</p><p>Inserting a post programmatically in WordPress is extremely easy. You have to use the wp_insert_post() function, which takes an array as a parameter.<br
/> Here is a working example. If you want to test it, paste the code below on your <em>functions.php</em> file.</p><pre class="brush: php">global $user_ID;
$new_post = array(
    'post_title' =&gt; 'My New Post',
    'post_content' =&gt; 'Lorem ipsum dolor sit amet...',
    'post_status' =&gt; 'publish',
    'post_date' =&gt; date('Y-m-d H:i:s'),
    'post_author' =&gt; $user_ID,
    'post_type' =&gt; 'post',
    'post_category' =&gt; array(0)
);
$post_id = wp_insert_post($new_post);</pre><p>Cool, isn&#8217;t it? Let have a closer look to the parameters specified in the <em>$new_post</em> array:</p><ul><li><strong>post_title</strong>: the name of the post.</li><li><strong>post_content</strong>: the content of the post</li><li><strong>post_status</strong>: the post status (published, draft, etc)</li><li><strong>post_date</strong>: use date() or specify a custom date</li><li><strong>post_author</strong>: Author id of the post author</li><li><strong>post_type</strong>: Can be post, page, or a custom post type</li><li><strong>post_category</strong> An array of categories ids</li></ul><p><strong>Source: <a
href="http://www.webmaster-source.com/2010/02/09/programmatically-creating-posts-in-wordpress">http://www.webmaster-source.com/2010/02/09/programmatically-creating-posts-in-wordpress</a></strong></p><h2>Inserting comments</h2><p>Inserting comments is not harder than inserting posts. I personally never used this code, but here is it in case you need it. To give it a try, simply paste it in your <em>functions.php</em> file.</p><pre class="brush: php">$data = array(
	'comment_post_ID' =&gt; 1,
	'comment_author' =&gt; 'admin',
	'comment_author_email' =&gt; 'admin@admin.com',
	'comment_author_url' =&gt; 'http://www.catswhocode.com',
	'comment_content' =&gt; 'Lorem ipsum dolor sit amet...',
	'comment_author_IP' =&gt; '127.0.0.1',
	'comment_agent' =&gt; 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; fr; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3',
	'comment_date' =&gt; date('Y-m-d H:i:s'),
	'comment_date_gmt' =&gt; date('Y-m-d H:i:s'),
	'comment_approved' =&gt; 1,
);

$comment_id = wp_insert_comment($data);</pre><p>Just like the wp_insert_post() function, wp_insert_comment() takes an array as a parameter. Here are the data used:</p><ul><li><strong>comment_post_ID</strong>: ID of the commented post</li><li><strong>comment_author</strong>: Name of the comment author</li><li><strong>comment_author_email</strong>: Email address of the comment author</li><li><strong>comment_author_url</strong>: Website of the comment author</li><li><strong>comment_content</strong>: Text of the comment</li><li><strong>comment_author_IP</strong>: IP address of the comment author</li><li><strong>comment_agent</strong>: User agent of the commenter browser</li><li><strong>comment_date</strong>: Date of the comment</li><li><strong>comment_date_gmt</strong>: GMT date of the comment</li><li><strong>comment_approved</strong>: Is the comment approved? 1 for yes and 0 for &#8220;awaiting moderation&#8221;</li></ul><h2>Adding categories to a post</h2><p>Now that we saw how to insert a post or a comment into WordPress database, let&#8217;s see how to make a post part of one (or more) categories. WordPress has a built-in function for that, named <em>wp_set_object_terms()</em>.</p><p>What you have to do is to create an array with the desired categories ID, and then use the function as shown below:</p><pre class="brush: php">$category_ids = array(4, 5, 6);
wp_set_object_terms( $post_id, $category_ids, 'category');</pre><p>The <em>wp_set_object_terms()</em> function take 3 parameters: The post ID, an array of categories ID, and the taxonomy type (In this example, category).</p><h2>Adding tags to a post</h2><p>Adding tags to a post is extremely simple as well. Even better, it does not require a new function, you can do so by using <em>wp_set_object_terms()</em>.<br
/> Take a look at the example below:</p><pre class="brush: php">$tag_ids = array(7, 8, 9);
wp_set_object_terms( $post_id, $tag_ids, 'post_tag');</pre><p>Looks very similar with the previous piece of code, which allowed us to add categories to a post, isn&#8217;t it? In fact, the only difference is the taxonomy type: Here the parameter is <em>post_tag</em> instead of <em>category</em>.<br
/> <strong>Source: <a
href="http://wpprogrammer.com/snippets/add-a-category-or-tag-to-a-post-programatically/">http://wpprogrammer.com/snippets/add-a-category-or-tag-to-a-post-programatically/</a></strong></p><h2>Automatically create a custom field when a post is published</h2><p>I recently had <a
href="http://www.webdevcat.com">a client</a> who wanted to have a custom field created automatically, each time he published a new post, so he wouldn&#8217;t have to create a custom field with a default value for each article he wrote.<br
/> This piece of code was a real life-saver: Just paste it on your <em>functions.php</em> file and publish a new post: A custom field has been created automatically.</p><pre class="brush: php">function add_custom_field_automatically($post_ID) {
	global $wpdb;
	if(!wp_is_post_revision($post_ID)) {
		add_post_meta($post_ID, 'field-name', 'custom value', true);
	}
}
add_action('publish_page', 'add_custom_field_automatically');
add_action('publish_post', 'add_custom_field_automatically');</pre><p>So how does it work? First, a function has been created. This function make sure the post isn&#8217;t a revision on then adds a custom field named <em>field-name</em>, with <em>custom value</em> as a value.<br
/> Then, a &#8220;hook&#8221; is used to make sure that every time a post or page will be published, the <em>add_custom_field_automatically()</em> function will be called.<br
/> <strong>Source: <a
href="http://wpcanyon.com/tipsandtricks/adding-a-custom-field-automatically-on-postpage-publish/">http://wpcanyon.com/tipsandtricks/adding-a-custom-field-automatically-on-postpage-publish/</a></strong></p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically">WordPress: How to insert data programmatically</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically&amp;title=WordPress%3A+How+to+insert+data+programmatically" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically&amp;title=WordPress%3A+How+to+insert+data+programmatically&amp;body=Recently%2C%20a%20client%20of%20mine%20asked%20me%20to%20write%20an%20article%20importer%20for%20his%20WordPress%20powered%20site%2C%20which%20was%20a%20very%20interesting%20project%20for%20me.%20In%20this%20article%2C%20I%27ll%20show%20you%20how%20you%20can%20easily%20add%20data%20%28posts%2C%20comments%2C%20categories%2C%20etc%29%20to%20your%20WordPress%20blog%2C%20without%20any%20manual%20effort." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically&amp;title=WordPress%3A+How+to+insert+data+programmatically" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically&amp;title=WordPress%3A+How+to+insert+data+programmatically" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically&amp;t=WordPress%3A+How+to+insert+data+programmatically" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically&amp;title=WordPress%3A+How+to+insert+data+programmatically&amp;body=Recently%2C%20a%20client%20of%20mine%20asked%20me%20to%20write%20an%20article%20importer%20for%20his%20WordPress%20powered%20site%2C%20which%20was%20a%20very%20interesting%20project%20for%20me.%20In%20this%20article%2C%20I%27ll%20show%20you%20how%20you%20can%20easily%20add%20data%20%28posts%2C%20comments%2C%20categories%2C%20etc%29%20to%20your%20WordPress%20blog%2C%20without%20any%20manual%20effort." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/_TXeAHyqQZI" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/wordpress-how-to-insert-data-programmatically/feed</wfw:commentRss> <slash:comments>17</slash:comments> </item> <item><title>10 life-saving PHP snippets</title><link>http://www.catswhocode.com/blog/10-life-saving-php-snippets</link> <comments>http://www.catswhocode.com/blog/10-life-saving-php-snippets#comments</comments> <pubDate>Mon, 19 Jul 2010 14:06:16 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[Web development]]></category> <category><![CDATA[php]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3777</guid> <description><![CDATA[In order to be efficient, a web developer should have a toolbox with code snippets he can use and reuse when needed. In this article, I'm going to show you 10 extremely useful PHP code snippets to add to your web developer toolbox.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/10-life-saving-php-snippets">10 life-saving PHP snippets</a></p> ]]></description> <content:encoded><![CDATA[<h2>Highlight specific words in a phrase</h2><p>Sometimes, for example, when displaying search results, it is a great idea to highlight specific words. This is exactly what the following function can do:</p><pre class="brush: php">function highlight($sString, $aWords) {
	if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
		return false;
	}

	$sWords = implode ('|', $aWords);
 	return preg_replace ('@\b('.$sWords.')\b@si', '&lt;strong style="background-color:yellow"&gt;$1&lt;/strong&gt;', $sString);
}</pre><p><strong>Source: <a
href="http://www.phpsnippets.info/highlights-words-in-a-phrase">http://www.phpsnippets.info/highlights-words-in-a-phrase</a></strong></p><h2>Get your average Feedburner subscribers</h2><p>Recently, Feedburner counts had lots of problems and it&#8217;s hard to say that the provided info is still relevant. This code will grab your subscriber count from the last 7 days and will return the average.</p><pre class="brush: php">function get_average_readers($feed_id,$interval = 7){
	$today = date('Y-m-d', strtotime("now"));
	$ago = date('Y-m-d', strtotime("-".$interval." days"));
	$feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&amp;dates=".$ago.",".$today;
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_URL, $feed_url);
	$data = curl_exec($ch);
	curl_close($ch);
	$xml = new SimpleXMLElement($data);
	$fb = $xml-&gt;feed-&gt;entry['circulation'];

	$nb = 0;
	foreach($xml-&gt;feed-&gt;children() as $circ){
		$nb += $circ['circulation'];
	}

	return round($nb/$interval);
}</pre><p><strong>Source: <a
href="http://www.catswhoblog.com/how-to-get-a-more-relevant-feedburner-count">http://www.catswhoblog.com/how-to-get-a-more-relevant-feedburner-count</a></strong></p><h2>Automatic password creation</h2><p>Although I personally prefer leaving users to choose their password themselves, a client recently asked me to generate passwords automatically when a new account is created.<br
/> The following function is flexible: You can choose the desired length and strength for the password.</p><pre class="brush: php">function generatePassword($length=9, $strength=0) {
	$vowels = 'aeuy';
	$consonants = 'bdghjmnpqrstvz';
	if ($strength &gt;= 1) {
		$consonants .= 'BDGHJLMNPQRSTVWXZ';
	}
	if ($strength &gt;= 2) {
		$vowels .= "AEUY";
	}
	if ($strength &gt;= 4) {
		$consonants .= '23456789';
	}
	if ($strength &gt;= 8 ) {
		$vowels .= '@#$%';
	}

	$password = '';
	$alt = time() % 2;
	for ($i = 0; $i &lt; $length; $i++) {
		if ($alt == 1) {
			$password .= $consonants[(rand() % strlen($consonants))];
			$alt = 0;
		} else {
			$password .= $vowels[(rand() % strlen($vowels))];
			$alt = 1;
		}
	}
	return $password;
}</pre><p><strong>Source: </strong><strong><a
href="http://www.phpsnippets.info/generate-a-password-in-php">http://www.phpsnippets.info/generate-a-password-in-php</a></strong></p><h2>Compress multiple CSS files</h2><p>If you&#8217;re using different CSS files on your site, they might take quite long to load. Using PHP, you can compress them into a single file with no unnecessary white spaces or comments.<br
/> This snippet has been previously discussed on my &#8220;<a
href="http://www.catswhocode.com/blog/3-ways-to-compress-css-files-using-php">3 ways to compress CSS files using PHP</a>&#8221; article.</p><pre class="brush: php">header('Content-type: text/css');
ob_start("compress");
function compress($buffer) {
  /* remove comments */
  $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
  /* remove tabs, spaces, newlines, etc. */
  $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
  return $buffer;
}

/* your css files */
include('master.css');
include('typography.css');
include('grid.css');
include('print.css');
include('handheld.css');

ob_end_flush();</pre><p><strong>Source: <a
href="http://www.phpsnippets.info/compress-css-files-using-php">http://www.phpsnippets.info/compress-css-files-using-php</a></strong></p><h2>Get short urls for Twitter</h2><p>Are you on <a
href="http://www.twitter.com/catswhocode">Twitter</a>? If yes, you probably use a url shortener such as bit.ly or TinyUrl to share your favorite blog posts and links on the network.<br
/> This snippet take a url as a parameter and will return a short url.</p><pre class="brush: php">function getTinyUrl($url) {
    return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
}</pre><p><strong>Source: <a
href="http://www.phpsnippets.info/convert-url-to-tinyurl">http://www.phpsnippets.info/convert-url-to-tinyurl</a></strong></p><h2>Calculate age using date of birth</h2><p>Pass a birth date to this function, and it will return the age of the person; very useful when building communities or social media sites.</p><pre class="brush: php">function age($date){
	$year_diff = '';
	$time = strtotime($date);
	if(FALSE === $time){
		return '';
	}

	$date = date('Y-m-d', $time);
	list($year,$month,$day) = explode("-",$date);
	$year_diff = date("Y") – $year;
	$month_diff = date("m") – $month;
	$day_diff = date("d") – $day;
	if ($day_diff &lt; 0 || $month_diff &lt; 0) $year_diff–;

	return $year_diff;
}</pre><p><strong>Source: <a
href="http://www.phpsnippets.info/calculate-age-of-a-person-using-date-of-birth#comment-25">John Karry on http://www.phpsnippets.info/calculate-age-of-a-person-using-date-of-birth</a></strong></p><h2>Calculate execution time</h2><p>For debugging purposes, it is a good thing to be able to calculate the execution time of a script. This is exactly what this piece of code can do.</p><pre class="brush: php">//Create a variable for start time
$time_start = microtime(true);

// Place your PHP/HTML/JavaScript/CSS/Etc. Here

//Create a variable for end time
$time_end = microtime(true);
//Subtract the two times to get seconds
$time = $time_end - $time_start;

echo 'Script took '.$time.' seconds to execute';</pre><p><strong>Source: <a
href="http://phpsnips.com/snippet.php?id=26">http://phpsnips.com/snippet.php?id=26</a></strong></p><h2>Maintenance mode with PHP</h2><p>When updating your site, it is generally a good thing to temporarily redirect your users to a &#8220;Maintenance&#8221; page so they will not see any critical info such as error messages.<br
/> This is generally done using an .htaccess file, but it can be done easily with PHP:</p><pre class="brush: php">function maintenance($mode = FALSE){
    if($mode){
        if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){
            header("Location: http://example.com/maintenance.php");
            exit;
        }
    }else{
        if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){
            header("Location: http://example.com/");
            exit;
        }
    }
}</pre><p><strong>Source: <a
href="http://www.phpsnippets.info/easy-maintenance-mode-with-php">http://www.phpsnippets.info/easy-maintenance-mode-with-php</a></strong></p><h2>Prevent js and css files from being cached</h2><p>By default, external files such as javascript and css are cached by the browser. If you want to prevent this from caching, simply use this easy tip:</p><pre class="brush: php">&lt;link href="/stylesheet.css?&lt;?php echo time(); ?&gt;" rel="stylesheet" type="text/css" /&amp;glt;</pre><p>The result will look like this:</p><pre class="brush: php">&lt;link href="/stylesheet.css?1234567890" rel="stylesheet" type="text/css" /&amp;glt;</pre><p><strong>Source: <a
href="http://davidwalsh.name/prevent-cache">http://davidwalsh.name/prevent-cache</a></strong></p><h2>Add (th, st, nd, rd, th) to the end of a number</h2><p>Another useful snippet which will automatically add <em>st</em>, <em>nd</em>, <em>rd</em> or <em>th</em> after a number.</p><pre class="brush: php">function make_ranked($rank) {
	$last = substr( $rank, -1 );
	$seclast = substr( $rank, -2, -1 );
	if( $last &gt; 3 || $last == 0 ) $ext = 'th';
	else if( $last == 3 ) $ext = 'rd';
	else if( $last == 2 ) $ext = 'nd';
	else $ext = 'st'; 

	if( $last == 1 &amp;&amp; $seclast == 1) $ext = 'th';
	if( $last == 2 &amp;&amp; $seclast == 1) $ext = 'th';
	if( $last == 3 &amp;&amp; $seclast == 1) $ext = 'th'; 

	return $rank.$ext;
}</pre><p><strong>Source: <a
href="http://phpsnips.com/snippet.php?id=37">http://phpsnips.com/snippet.php?id=37</a></strong></p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/10-life-saving-php-snippets">10 life-saving PHP snippets</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/10-life-saving-php-snippets&amp;title=10+life-saving+PHP+snippets+" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/10-life-saving-php-snippets&amp;title=10+life-saving+PHP+snippets+&amp;body=In%20order%20to%20be%20efficient%2C%20a%20web%20developer%20should%20have%20a%20toolbox%20with%20code%20snippets%20he%20can%20use%20and%20reuse%20when%20needed.%20In%20this%20article%2C%20I%27m%20going%20to%20show%20you%2010%20extremely%20useful%20PHP%20code%20snippets%20to%20add%20to%20your%20web%20developer%20toolbox." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/10-life-saving-php-snippets&amp;title=10+life-saving+PHP+snippets+" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/10-life-saving-php-snippets&amp;title=10+life-saving+PHP+snippets+" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/10-life-saving-php-snippets&amp;t=10+life-saving+PHP+snippets+" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/10-life-saving-php-snippets&amp;title=10+life-saving+PHP+snippets+&amp;body=In%20order%20to%20be%20efficient%2C%20a%20web%20developer%20should%20have%20a%20toolbox%20with%20code%20snippets%20he%20can%20use%20and%20reuse%20when%20needed.%20In%20this%20article%2C%20I%27m%20going%20to%20show%20you%2010%20extremely%20useful%20PHP%20code%20snippets%20to%20add%20to%20your%20web%20developer%20toolbox." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/CsWDXAnJY0Y" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/10-life-saving-php-snippets/feed</wfw:commentRss> <slash:comments>43</slash:comments> </item> <item><title>How to create a built-in contact form for your WordPress theme</title><link>http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme</link> <comments>http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme#comments</comments> <pubDate>Mon, 28 Jun 2010 14:23:07 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[WordPress]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3751</guid> <description><![CDATA[Many WordPress plugins can add a contact form to your blog, but a plugin is not necessary. In this tutorial, I'm going to show you how you can create a built-in contact form for your WordPress theme.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme">How to create a built-in contact form for your WordPress theme</a></p> ]]></description> <content:encoded><![CDATA[<h2>Getting Ready</h2><p>A buil-in WordPress form was initially created on my site <a
href="http://www.phpsnippets.info">PHP Snippets</a>, however, thanks to stupid people who sent hundreds of &#8220;test&#8221; emails from this form, I have taken it offline.<br
/> It looked like this:<br
/> <a
href="http://www.phpsnippets.info/contact"><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/06/wordpress-built-in-contact-form.jpg" alt="" /></a></p><h2>Step 1: Creating the page template</h2><p>The first step is to create a page template. To do so, copy the <em>page.php</em> code into a new file named <em>page-contact.php</em>.</p><p>We have to add a comment at the beginning of the <em>contact.php</em> file to make sure WordPress will treat the file as a page template. Here&#8217;s the code:</p><pre class="brush: php">&lt;?php
/*
Template Name: Contact
*/
?&gt;</pre><p>Your contact.php file should look like this:</p><pre class="brush: php">&lt;?php
/*
Template Name: Contact
*/
?&gt;

&lt;?php get_header() ?&gt;

	&lt;div id="container"&gt;
		&lt;div id="content"&gt;
			&lt;?php the_post() ?&gt;
			&lt;div id="post-&lt;?php the_ID() ?&gt;" class="post"&gt;
				&lt;div class="entry-content"&gt;
				&lt;/div&gt;&lt;!-- .entry-content -&gt;
			&lt;/div&gt;&lt;!-- .post--&gt;
		&lt;/div&gt;&lt;!-- #content --&gt;
	&lt;/div&gt;&lt;!-- #container --&gt;

&lt;?php get_sidebar() ?&gt;
&lt;?php get_footer() ?&gt;</pre><h2>Step 2: Building the form</h2><p>Now, we have to create a simple contact form. Simply paste the following code within the <em>entry-content</em> div.</p><pre class="brush: html">&lt;form action="&lt;?php the_permalink(); ?&gt;" id="contactForm" method="post"&gt;
	&lt;ul&gt;
		&lt;li&gt;
			&lt;label for="contactName"&gt;Name:&lt;/label&gt;
			&lt;input type="text" name="contactName" id="contactName" value="" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="email"&gt;Email&lt;/label&gt;
			&lt;input type="text" name="email" id="email" value="" /&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;label for="commentsText"&gt;Message:&lt;/label&gt;
			&lt;textarea name="comments" id="commentsText" rows="20" cols="30"&gt;&lt;/textarea&gt;
		&lt;/li&gt;
		&lt;li&gt;
			&lt;button type="submit"&gt;Send email&lt;/button&gt;
		&lt;/li&gt;
	&lt;/ul&gt;
	&lt;input type="hidden" name="submitted" id="submitted" value="true" /&gt;
&lt;/form&gt;</pre><p>Nothing hard with this pretty self-explanatory html code for our form. Note the <em>input type=&#8221;hidden&#8221;</em> I added on line 19: It will be used later to check if the form has been submitted.</p><h2>Step 3: data processing and error handling</h2><p>Our form looks pretty good, but right it is very useless because it does not send any email. What we have to do is to verify if the form has been submitted then verify if fields have been filled correctly.</p><p>If fields are correctly filled, we&#8217;ll get the blog admin email and send them the email. Otherwise, no email will be sent and errors will be displayed to the user.</p><p>Paste the following code between the Page Template declaration and the get_header() function:</p><pre class="brush: php">&lt;?php
if(isset($_POST['submitted'])) {
	if(trim($_POST['contactName']) === '') {
		$nameError = 'Please enter your name.';
		$hasError = true;
	} else {
		$name = trim($_POST['contactName']);
	}

	if(trim($_POST['email']) === '')  {
		$emailError = 'Please enter your email address.';
		$hasError = true;
	} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
		$emailError = 'You entered an invalid email address.';
		$hasError = true;
	} else {
		$email = trim($_POST['email']);
	}

	if(trim($_POST['comments']) === '') {
		$commentError = 'Please enter a message.';
		$hasError = true;
	} else {
		if(function_exists('stripslashes')) {
			$comments = stripslashes(trim($_POST['comments']));
		} else {
			$comments = trim($_POST['comments']);
		}
	}

	if(!isset($hasError)) {
		$emailTo = get_option('tz_email');
		if (!isset($emailTo) || ($emailTo == '') ){
			$emailTo = get_option('admin_email');
		}
		$subject = '[PHP Snippets] From '.$name;
		$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
		$headers = 'From: '.$name.' &lt;'.$emailTo.'&gt;' . "\r\n" . 'Reply-To: ' . $email;

		mail($emailTo, $subject, $body, $headers);
		$emailSent = true;
	}

} ?&gt;</pre><p>What I&#8217;ve done here was simply to make sure that the form has been submitted and filled correctly. If an error, such as an empty field or incorrect email address occurred, a message is returned and the form isn&#8217;t submitted.</p><p>Now we have to display error messages below the related field, for example &#8220;Please enter your name&#8221;. <strong>Below you&#8217;ll find the complete form page template that you can use &#8220;as it&#8221;.</strong></p><pre class="brush: php">&lt;?php
/*
Template Name: Contact
*/
?&gt;

&lt;?php
if(isset($_POST['submitted'])) {
	if(trim($_POST['contactName']) === '') {
		$nameError = 'Please enter your name.';
		$hasError = true;
	} else {
		$name = trim($_POST['contactName']);
	}

	if(trim($_POST['email']) === '')  {
		$emailError = 'Please enter your email address.';
		$hasError = true;
	} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
		$emailError = 'You entered an invalid email address.';
		$hasError = true;
	} else {
		$email = trim($_POST['email']);
	}

	if(trim($_POST['comments']) === '') {
		$commentError = 'Please enter a message.';
		$hasError = true;
	} else {
		if(function_exists('stripslashes')) {
			$comments = stripslashes(trim($_POST['comments']));
		} else {
			$comments = trim($_POST['comments']);
		}
	}

	if(!isset($hasError)) {
		$emailTo = get_option('tz_email');
		if (!isset($emailTo) || ($emailTo == '') ){
			$emailTo = get_option('admin_email');
		}
		$subject = '[PHP Snippets] From '.$name;
		$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
		$headers = 'From: '.$name.' &lt;'.$emailTo.'&gt;' . "\r\n" . 'Reply-To: ' . $email;

		mail($emailTo, $subject, $body, $headers);
		$emailSent = true;
	}

} ?&gt;
&lt;?php get_header(); ?&gt;
	&lt;div id="container"&gt;
		&lt;div id="content"&gt;

			&lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
			&lt;div &lt;?php post_class() ?&gt; id="post-&lt;?php the_ID(); ?&gt;"&gt;
				&lt;h1 class="entry-title"&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
					&lt;div class="entry-content"&gt;
						&lt;?php if(isset($emailSent) &amp;&amp; $emailSent == true) { ?&gt;
							&lt;div class="thanks"&gt;
								&lt;p&gt;Thanks, your email was sent successfully.&lt;/p&gt;
							&lt;/div&gt;
						&lt;?php } else { ?&gt;
							&lt;?php the_content(); ?&gt;
							&lt;?php if(isset($hasError) || isset($captchaError)) { ?&gt;
								&lt;p class="error"&gt;Sorry, an error occured.&lt;p&gt;
							&lt;?php } ?&gt;

						&lt;form action="&lt;?php the_permalink(); ?&gt;" id="contactForm" method="post"&gt;
							&lt;ul class="contactform"&gt;
							&lt;li&gt;
								&lt;label for="contactName"&gt;Name:&lt;/label&gt;
								&lt;input type="text" name="contactName" id="contactName" value="&lt;?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?&gt;" class="required requiredField" /&gt;
								&lt;?php if($nameError != '') { ?&gt;
									&lt;span class="error"&gt;&lt;?=$nameError;?&gt;&lt;/span&gt;
								&lt;?php } ?&gt;
							&lt;/li&gt;

							&lt;li&gt;
								&lt;label for="email"&gt;Email&lt;/label&gt;
								&lt;input type="text" name="email" id="email" value="&lt;?php if(isset($_POST['email']))  echo $_POST['email'];?&gt;" class="required requiredField email" /&gt;
								&lt;?php if($emailError != '') { ?&gt;
									&lt;span class="error"&gt;&lt;?=$emailError;?&gt;&lt;/span&gt;
								&lt;?php } ?&gt;
							&lt;/li&gt;

							&lt;li&gt;&lt;label for="commentsText"&gt;Message:&lt;/label&gt;
								&lt;textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField"&gt;&lt;?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?&gt;&lt;/textarea&gt;
								&lt;?php if($commentError != '') { ?&gt;
									&lt;span class="error"&gt;&lt;?=$commentError;?&gt;&lt;/span&gt;
								&lt;?php } ?&gt;
							&lt;/li&gt;

							&lt;li&gt;
								&lt;input type="submit"&gt;Send email&lt;/input&gt;
							&lt;/li&gt;
						&lt;/ul&gt;
						&lt;input type="hidden" name="submitted" id="submitted" value="true" /&gt;
					&lt;/form&gt;
				&lt;?php } ?&gt;
				&lt;/div&gt;&lt;!-- .entry-content --&gt;
			&lt;/div&gt;&lt;!-- .post --&gt;

				&lt;?php endwhile; endif; ?&gt;
		&lt;/div&gt;&lt;!-- #content --&gt;
	&lt;/div&gt;&lt;!-- #container --&gt;

&lt;?php get_sidebar(); ?&gt;
&lt;?php get_footer(); ?&gt;</pre><h2>Step 4: Adding jQuery verification</h2><p>Our form is now working perfectly. But we can enhance it by adding a client side verification. To do so, I&#8217;m going to use jQuery and the <a
href="http://docs.jquery.com/Plugins/Validation">validate jQuery plugin</a>. This plugin is great because it allows you to verify that a form has been filled correctly, quickly and easily.</p><p>The first thing to do is to <a
href="http://docs.jquery.com/Plugins/Validation">download the validate plugin</a> and upload it into your theme file (under a <em>/js</em> directory). Once done, paste the following into a new file:</p><pre class="brush: javascript">$(document).ready(function(){
	$("#contactForm").validate();
});</pre><p>Save it as <em>verif.js</em> in your /js directory.</p><p>Now we have to link the javascript files to our theme. Open your <em>header.php</em> file and paste the following within the &lt;head&gt; and &lt;/head&gt; tags:</p><pre class="brush: html">&lt;?php if( is_page('contact') ){ ?&gt;
	&lt;script type="text/javascript" src="&lt;?php bloginfo('template_directory'); ?&gt;/js/jquery.validate.min.js"&gt;&lt;/script&gt;
	&lt;script type="text/javascript" src="&lt;?php bloginfo('template_directory'); ?&gt;/js/verif.js"&gt;&lt;/script&gt;
&lt;?php }?&gt;</pre><p>Once done, your form will be validated on the client side by the jQuery validate plugin. How does it work? It simply picks form element which have the css class <em>required</em> and verifies if they&#8217;re filled correctly. If not, a message is displayed.<br
/> The plugin is powerful and you can do lots of things with it, however this isn&#8217;t the purpose of this article. Hope you enjoy your new WordPress form!</p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme">How to create a built-in contact form for your WordPress theme</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme&amp;title=How+to+create+a+built-in+contact+form+for+your+WordPress+theme" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme&amp;title=How+to+create+a+built-in+contact+form+for+your+WordPress+theme&amp;body=Many%20WordPress%20plugins%20can%20add%20a%20contact%20form%20to%20your%20blog%2C%20but%20a%20plugin%20is%20not%20necessary.%20In%20this%20tutorial%2C%20I%27m%20going%20to%20show%20you%20how%20you%20can%20create%20a%20built-in%20contact%20form%20for%20your%20WordPress%20theme." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme&amp;title=How+to+create+a+built-in+contact+form+for+your+WordPress+theme" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme&amp;title=How+to+create+a+built-in+contact+form+for+your+WordPress+theme" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme&amp;t=How+to+create+a+built-in+contact+form+for+your+WordPress+theme" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme&amp;title=How+to+create+a+built-in+contact+form+for+your+WordPress+theme&amp;body=Many%20WordPress%20plugins%20can%20add%20a%20contact%20form%20to%20your%20blog%2C%20but%20a%20plugin%20is%20not%20necessary.%20In%20this%20tutorial%2C%20I%27m%20going%20to%20show%20you%20how%20you%20can%20create%20a%20built-in%20contact%20form%20for%20your%20WordPress%20theme." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/eGagKTSLvfs" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme/feed</wfw:commentRss> <slash:comments>49</slash:comments> </item> <item><title>How to create a side blog with WordPress 3.0</title><link>http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0</link> <comments>http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0#comments</comments> <pubDate>Mon, 21 Jun 2010 14:11:42 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[WordPress]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3731</guid> <description><![CDATA[Finally! WordPress 3.0 was released last week. Among other exiting features, custom post types are bringing lots of new possibilities to bloggers. In this tutorial, I'll show you how to create a side blog listing products using the WordPress 3.0 custom post type feature.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0">How to create a side blog with WordPress 3.0</a></p> ]]></description> <content:encoded><![CDATA[<h2>Getting ready</h2><p>So, what are custom post types? That&#8217;s simple, custom post types are like a blog post or page, but of a custom defined type.<br
/> As an example, I decided to list some <a
href="http://www.catswhoblog.com/coupons">promo codes</a> on my other blog CatsWhoBlog. I could have used a good old static page, but updating it and adding new promo codes would have been a pain.</p><p>So I created a custom post type, named <em>coupon</em> and a page template to list all <em>coupons</em>. It&#8217;s as simple as that, and now managing coupons &amp; promo codes is extremely easy:</p><p><a
href="http://www.catswhoblog.com/coupons"><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/06/wp2.jpg" alt="" /></a></p><h2>Creating the post type</h2><p>Ok, let&#8217;s code. The first thing to do is to create a custom post type. To do so, pick up your theme functions.php file, and add the following:</p><pre class="brush: php">function create_my_post_types() {
    register_post_type('coupons',
        array(
            'label' =&gt; __('Coupons'),
            'singular_label' =&gt; __('Coupon'),
            'public' =&gt; true,
            'supports' =&gt; array(
                'title',
                'excerpt',
                'comments',
                'custom-fields'
	    ),
	    'rewrite' =&gt; array(
	        'slug' =&gt; 'coupons',
	        'with_front' =&gt; false
	    ),
        )
    );
}
add_action( 'init', 'create_my_post_types' );</pre><p>Once you saved functions.php, you should notice that a new tab appeared in your WordPress dashboard, as shown in the picture below:<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/06/wordpress-30-custom.jpg" alt="" /></p><p>So what does this code do?<br
/> First, I have created a function which registers a new post type, named <em>coupons</em>. I gave the following parameters to the register_post_type() function:</p><ul><li><strong>label</strong>: Nicename of your post type.</li><li><strong>singular_label</strong>: Pretty self explanatory, the singular label of your post type.</li><li><strong>public</strong>: Allows post type to be seen publicly.</li><li><strong>supports</strong>: Array of data of what the post type supports (editor, excerpt, comments, custom fields, etc&#8230;)</li><li><strong>rewrite</strong>: Parameters for url rewriting and general post type display.</li></ul><p>The complete parameter list can be found on <a
rel="nofollow" href="http://codex.wordpress.org/Function_Reference/register_post_type">WordPress Codex</a>.</p><p>Then, I &#8220;hooked&#8221; this function to WordPress <em>init()</em> function using <em>add_action()</em>.</p><h2>Adding data</h2><p>Now that the post type has been created, you can add data by clicking on the &#8220;Add Coupon&#8221; (Or whatever you named it) link in WordPress dashboard menu.</p><p>You should see the following:<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/06/wordpress-custom-post-types.jpg" alt="" /></p><h2>Creating a page template to list custom post types</h2><p>Now that we have created a custom post type and added some custom posts, we still have to display it. To do so, I have used a page template. You can easily reuse the following code, or adapt it to display in your blog sidebar, for example.<br
/> If you want to see a demo of the page template, just <a
href="http://www.catswhoblog.com/coupons">click here</a>.</p><pre class="brush: php">&lt;?php
/*
Template Name: Promo codes Page
*/
?&gt;
&lt;?php get_header() ?&gt;

	&lt;div id="container"&gt;
		&lt;div id="content" class="coupons"&gt;
			&lt;h1 class="entry-title"&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
			&lt;?php the_content(); ?&gt;

			&lt;?php global $wp_query;
			$page_num = $paged;
			if($pagenum='') $pagenum=1;

			$wp_query = new WP_Query("showposts=20&amp;post_type=coupons&amp;post_status=publish&amp;paged=".$page_num);

			while ($wp_query-&gt;have_posts()) : $wp_query-&gt;the_post(); ?&gt;

				&lt;div class="post" id="post-&lt;?php the_ID(); ?&gt;"&gt;
					&lt;h2&gt;&lt;?php the_title(); ?&gt;&lt;/h2&gt;
					&lt;div class="exerpt"&gt;&lt;?php the_excerpt(); ?&gt;&lt;/div&gt;
				&lt;/div&gt;&lt;!-- .post --&gt;

			&lt;?php endwhile; ?&gt;

			&lt;div class="navigation"&gt;&lt;p&gt;&lt;?php posts_nav_link(); ?&gt;&lt;/p&gt;&lt;/div&gt;

		&lt;/div&gt;&lt;!-- #content --&gt;
	&lt;/div&gt;&lt;!-- #container --&gt;

&lt;?php get_sidebar() ?&gt;
&lt;?php get_footer() ?&gt;</pre><p>As you can see, the code I&#8217;ve used is definitely easy and self-explanatory. In order to fetch a specific post type, you have to specify the parameter <em>post_type=coupons</em>.</p><p>That&#8217;s all for today, hope you enjoyed this tutorial!</p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0">How to create a side blog with WordPress 3.0</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0&amp;title=How+to+create+a+side+blog+with+WordPress+3.0" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0&amp;title=How+to+create+a+side+blog+with+WordPress+3.0&amp;body=Finally%21%20WordPress%203.0%20was%20released%20last%20week.%20Among%20other%20exiting%20features%2C%20custom%20post%20types%20are%20bringing%20lots%20of%20new%20possibilities%20to%20bloggers.%20In%20this%20tutorial%2C%20I%27ll%20show%20you%20how%20to%20create%20a%20side%20blog%20listing%20products%20using%20the%20WordPress%203.0%20custom%20post%20type%20feature." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0&amp;title=How+to+create+a+side+blog+with+WordPress+3.0" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0&amp;title=How+to+create+a+side+blog+with+WordPress+3.0" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0&amp;t=How+to+create+a+side+blog+with+WordPress+3.0" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0&amp;title=How+to+create+a+side+blog+with+WordPress+3.0&amp;body=Finally%21%20WordPress%203.0%20was%20released%20last%20week.%20Among%20other%20exiting%20features%2C%20custom%20post%20types%20are%20bringing%20lots%20of%20new%20possibilities%20to%20bloggers.%20In%20this%20tutorial%2C%20I%27ll%20show%20you%20how%20to%20create%20a%20side%20blog%20listing%20products%20using%20the%20WordPress%203.0%20custom%20post%20type%20feature." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/EU1XZgIQU2Y" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/how-to-create-a-side-blog-with-wordpress-3-0/feed</wfw:commentRss> <slash:comments>56</slash:comments> </item> <item><title>10 PHP code snippets for working with strings</title><link>http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings</link> <comments>http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings#comments</comments> <pubDate>Mon, 07 Jun 2010 16:09:57 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[Web development]]></category> <category><![CDATA[php]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3720</guid> <description><![CDATA[Strings are a very important kind of data, and you have to deal with them daily with web development tasks. In this article, I have compiled 10 extremely useful functions and code snippets to make your php developer life easier.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings">10 PHP code snippets for working with strings</a></p> ]]></description> <content:encoded><![CDATA[<h2>Automatically remove html tags from a string</h2><p>On user-submitted forms, you may want to remove all unnecessary html tags. Doing so is easy using the strip_tags() function:</p><pre class="brush: php">$text = strip_tags($input, "");</pre><p><strong>Source: <a
href="http://phpbuilder.com/columns/Jason_Gilmore060210.php3?page=2">http://phpbuilder.com/columns/Jason_Gilmore060210.php3?page=2</a></strong></p><h2>Get the text between $start and $end</h2><p>This is the kind of function every web developer should have in their toolbox for future use: give it a string, a start, and an end, and it will return the text contained with $start and $end.</p><pre class="brush: php">function GetBetween($content,$start,$end){
    $r = explode($start, $content);
    if (isset($r[1])){
        $r = explode($end, $r[1]);
        return $r[0];
    }
    return '';
}</pre><p><strong>Source: <a
href="http://www.jonasjohn.de/snippets/php/get-between.htm">http://www.jonasjohn.de/snippets/php/get-between.htm</a></strong></p><h2>Transform URL to hyperlinks</h2><p>If you leave a URL in the comment form of a WordPress blog, it will be automatically transformed into a hyperlink. If you want to implement the same functionality in your own website or web app, you can use the following code:</p><pre class="brush: php">
$url = "Jean-Baptiste Jung (http://www.webdevcat.com)";
$url = preg_replace("#http://([A-z0-9./-]+)#", '<a href="$1">$0</a>', $url);
</pre><p><strong>Source: <a
href="http://phpbuilder.com/columns/Jason_Gilmore060210.php3?page=2">http://phpbuilder.com/columns/Jason_Gilmore060210.php3?page=2</a></strong></p><h2>Split text up into 140 char array for Twitter</h2><p>As you probably know, <a
href="http://www.twitter.com/catswhocode">Twitter</a> only accepts messages of 140 characters or less. If you want to interact with the popular social messaging site, you&#8217;ll enjoy this function for sure, which will allow you to truncate your message to 140 characters.</p><pre class="brush: php">function split_to_chunks($to,$text){
	$total_length = (140 - strlen($to));
	$text_arr = explode(" ",$text);
	$i=0;
	$message[0]="";
	foreach ($text_arr as $word){
		if ( strlen($message[$i] . $word . ' ') &lt;= $total_length ){
			if ($text_arr[count($text_arr)-1] == $word){
				$message[$i] .= $word;
			} else {
				$message[$i] .= $word . ' ';
			}
		} else {
			$i++;
			if ($text_arr[count($text_arr)-1] == $word){
				$message[$i] = $word;
			} else {
				$message[$i] = $word . ' ';
			}
		}
	}
	return $message;
}</pre><p><strong>Source: <a
href="http://www.phpsnippets.info/split-text-up-into-140-char-array-for-twitter">http://www.phpsnippets.info/split-text-up-into-140-char-array-for-twitter</a></strong></p><h2>Remove URLs from string</h2><p>When I see the amount of URLs people try to leave in my blog comments to get traffic and/or backlinks, I think I should definitely give a go to this snippet!</p><pre class="brush: php">$string = preg_replace('/\b(https?|ftp|file):\/\/[-A-Z0-9+&amp;@#\/%?=~_|$!:,.;]*[A-Z0-9+&amp;@#\/%=~_|$]/i', '', $string);</pre><p><strong>Source: <a
href="http://snipplr.com/view.php?codeview&amp;id=15236">http://snipplr.com/view.php?codeview&amp;id=15236</a></strong></p><h2>Convert strings to slugs</h2><p>Need to generate slugs (permalinks) that are SEO friendly? The following function takes a string as a parameter and will return a SEO friendly slug. Simple and efficient!</p><pre class="brush: php">function slug($str){
	$str = strtolower(trim($str));
	$str = preg_replace('/[^a-z0-9-]/', '-', $str);
	$str = preg_replace('/-+/', "-", $str);
	return $str;
}</pre><p><strong>Source: <a
href="http://snipplr.com/view.php?codeview&amp;id=2809">http://snipplr.com/view.php?codeview&amp;id=2809</a></strong></p><h2>Parse CSV files</h2><p>CSV (Coma separated values) files are an easy way to store data, and parsing them using PHP is dead simple. Don&#8217;t believe me? Just use the following snippet and see for yourself.</p><pre class="brush: php">$fh = fopen("contacts.csv", "r");
while($line = fgetcsv($fh, 1000, ",")) {
    echo "Contact: {$line[1]}";
}</pre><p><strong>Source: <a
href="http://phpbuilder.com/columns/Jason_Gilmore060210.php3?page=1">http://phpbuilder.com/columns/Jason_Gilmore060210.php3?page=1</a></strong></p><h2>Search for a string in another string</h2><p>If a string is contained in another string and you need to search for it, this is a very clever way to do it:</p><pre class="brush: php">function contains($str, $content, $ignorecase=true){
    if ($ignorecase){
        $str = strtolower($str);
        $content = strtolower($content);
    }
    return strpos($content,$str) ? true : false;
}</pre><p><strong>Source: <a
href="http://www.jonasjohn.de/snippets/php/contains.htm">http://www.jonasjohn.de/snippets/php/contains.htm</a></strong></p><h2>Check if a string starts with a specific pattern</h2><p>Some languages such as Java have a startWith method/function which allows you to check if a string starts with a specific pattern. Unfortunately, PHP does not have a similar built-in function.<br
/> Whatever- we just have to build our own, which is very simple:</p><pre class="brush: php">function String_Begins_With($needle, $haystack {
    return (substr($haystack, 0, strlen($needle))==$needle);
}</pre><p><strong>Source: <a
href="http://snipplr.com/view.php?codeview&amp;id=2143">http://snipplr.com/view.php?codeview&amp;id=2143</a></strong></p><h2>Extract emails from a string</h2><p>Ever wondered how spammers can get your email address? That&#8217;s simple, they get web pages (such as forums) and simply parse the html to extract emails. This code takes a string as a parameter, and will print all emails contained within. Please don&#8217;t use this code for spam <img
src='http://www.catswhocode.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /></p><pre class="brush: php">function extract_emails($str){
    // This regular expression extracts all emails from a string:
    $regexp = '/([a-z0-9_\.\-])+\@(([a-z0-9\-])+\.)+([a-z0-9]{2,4})+/i';
    preg_match_all($regexp, $str, $m);

    return isset($m[0]) ? $m[0] : array();
}

$test_string = 'This is a test string...

        test1@example.org

        Test different formats:
        test2@example.org;
        &lt;a href="test3@example.org"&gt;foobar&lt;/a&gt;
        &lt;test4@example.org&gt;

        strange formats:
        test5@example.org
        test6[at]example.org
        test7@example.net.org.com
        test8@ example.org
        test9@!foo!.org

        foobar
';

print_r(extract_emails($test_string));</pre><p><strong>Source: <a
href="http://www.jonasjohn.de/snippets/php/extract-emails.htm">http://www.jonasjohn.de/snippets/php/extract-emails.htm</a></strong></p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings">10 PHP code snippets for working with strings</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings&amp;title=10+PHP+code+snippets+for+working+with+strings" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings&amp;title=10+PHP+code+snippets+for+working+with+strings&amp;body=Strings%20are%20a%20very%20important%20kind%20of%20data%2C%20and%20you%20have%20to%20deal%20with%20them%20daily%20with%20web%20development%20tasks.%20In%20this%20article%2C%20I%20have%20compiled%2010%20extremely%20useful%20functions%20and%20code%20snippets%20to%20make%20your%20php%20developer%20life%20easier." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings&amp;title=10+PHP+code+snippets+for+working+with+strings" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings&amp;title=10+PHP+code+snippets+for+working+with+strings" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings&amp;t=10+PHP+code+snippets+for+working+with+strings" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings&amp;title=10+PHP+code+snippets+for+working+with+strings&amp;body=Strings%20are%20a%20very%20important%20kind%20of%20data%2C%20and%20you%20have%20to%20deal%20with%20them%20daily%20with%20web%20development%20tasks.%20In%20this%20article%2C%20I%20have%20compiled%2010%20extremely%20useful%20functions%20and%20code%20snippets%20to%20make%20your%20php%20developer%20life%20easier." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/IolpH500vFs" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/10-php-code-snippets-for-working-with-strings/feed</wfw:commentRss> <slash:comments>33</slash:comments> </item> <item><title>Best practices for coding HTML emails</title><link>http://www.catswhocode.com/blog/best-practices-for-coding-html-emails</link> <comments>http://www.catswhocode.com/blog/best-practices-for-coding-html-emails#comments</comments> <pubDate>Mon, 24 May 2010 14:12:43 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[Web development]]></category> <category><![CDATA[html]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3687</guid> <description><![CDATA[Even if you're able to code complex website layouts, coding an html email is a hard job and there's lots of things to take into consideration. This article features the most important things I've learned in 5 years of coding html emails.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/best-practices-for-coding-html-emails">Best practices for coding HTML emails</a></p> ]]></description> <content:encoded><![CDATA[<h2>Keep it simple and lightweight</h2><p>If you have to remember only one of all the tips I&#8217;m going to give you in this post, it should be this one. In fact, an html email is not a website, so you shouldn&#8217;t try to embed a website into an email.</p><p>Some years ago, I used to work for a French TV channel and I often had to slice some PSD&#8217;s into html emails. The PSD&#8217;s contained gradients, funky fonts, and even animated gifs. As a result, the work (despite all efforts I&#8217;ve put in it) looked different from one email client to another, the fonts had to be replaced by Arial, and the whole email was extremely heavy and highly relied on images.</p><p>On the other hand, a simple html email will loaded smoothly, and will be more pleasant to read.</p><p><img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/email.png" alt="" /><br
/> <em>(<a
href="http://yoast.com/wordpress-newsletter/">Yoast.com Newsletter</a>)</em></p><h2>Don&#8217;t abuse images</h2><p>An image is worth a thousand words, but it may also take forever to load. I have received many emails that consisted of a few lines of text and nothing else but big images. As a result, the recipient had to wait until the image was loaded (Which can sometimes takes up to 5 seconds!) in order to read the information embedded in the email.</p><p>This is, in my opinion, a waste of time for the recipient, as well as a waste of money for the sender: Most people won&#8217;t wait 5 seconds in order to have the big image you send them loaded. They&#8217;ll trash the email. It&#8217;s as simple as that.</p><p>An html email should be beautiful and pleasurable to view, but don&#8217;t over do it. Like I&#8217;ve just said, keep it simple, you won&#8217;t regret it.</p><h2>Work with tables</h2><p>As many email clients handle CSS worst than IE6 (Yes, I&#8217;m not joking), you shouldn&#8217;t even try to make advanced layouts using CSS. Instead, you should do a jump 10 years ago and say hello to <em>tables</em>, <em>tr</em>&#8217;s and <em>td</em>&#8217;s again.</p><p>If you&#8217;re like me, you&#8217;re a CSS fan, and this might sound very frustrating. In fact, having to code the dirty way is never pleasant, but you don&#8217;t have much of a choice. Do not hesitate to test by yourself: Chances are that you&#8217;ll soon be using tables again.</p><h2>Always use images from your server</h2><p>Among html email worst practices I ever saw, embedding images directly in the email definitely arrived at good place. This is wrong in many points: First, it will make the email heavier (I&#8217;ve seen 300ko messages!), and secondly, there&#8217;s a strong risk that the recipient email client block those images.</p><p>What you should do is to create a hierarchy of directories on your server, for example <em>Newsletters</em> and then <em>May_2010</em>, and upload images for your html email in it. Once done, simply call them using absolute url paths:</p><pre class="brush: html">&lt;img src="http://www.catswhocode.com/images/cat.jpg" alt="A cat" /&gt;</pre><h2>Write your CSS inline and use html attributes</h2><p>In email clients, the lack of CSS support is definitely something to keep in mind. Don&#8217;t try linking to an external CSS file, and try to avoid as many CSS declarations as possible in the &lt;head&gt; section of your document.</p><p>It may be dirty, but the best way to make sure your CSS will be (quite) correctly interpreted by the recipient&#8217;s client is to code your CSS the inline way, as shown in the example below:</p><pre class="brush: html">&lt;p style="background:#069; color: white;"&gt;A new background and font color with inline CSS&lt;/p&gt;</pre><p>Another &#8220;dirty but effective&#8221; option to consider is the use of html attributes, such as <em>background</em> or <em>bgcolor</em>:</p><pre class="brush: html">&lt;body bgcolor="#069"&gt;</pre><h2>Don&#8217;t forget the text format</h2><p>It may seems a bit obsolete in 2010, but many people, including myself, prefers the good old &#8220;plain text&#8221; format than html emails.  When creating an email list subscription form, try to allow the visitor to choose between the html and plain text format.</p><h2>Make sure your emails display in various clients</h2><p>When creating a website, any serious developer will test its render on various browsers. This should be the same with html emails: people use a wide variety of clients and in order to be professional you should try to support most of them.</p><p>In my opinion, the following clients should be supported: Gmail, Yahoo mail, Mozilla Thunderbird, Apple Mail and Microsoft Outlook. below, you&#8217;ll find two great guides about CSS in html emails:</p><ul><li><strong><a
href="http://www.campaignmonitor.com/css/">Guide to CSS support in email clients</a>: </strong>A very interesting guide describing which CSS properties can be used depending on the user&#8217;s email client. PDF and Excel versions are downloadable.</li><li><strong><a
href="http://www.campaignmonitor.com/blog/post/3107/css3-support-in-email-clients">CSS3 support in email clients</a> :</strong> Enjoying CSS3? Here&#8217;s another great resource brought to you by Campaign Monitor, showing the few CSS3 properties you can already use in your html emails.</li></ul><h2>Use Google Analytics to track conversions</h2><p>Sending a good html email is definitely a great thing, but your goal is to have people click on it and visit your site. There&#8217;s lots of way to track clicks on emails, but one of the easiest is to use Google Analytics, that you&#8217;re probably already using on your website.</p><p>I&#8217;ve never been a big email list sender so I never experimented with Google Analytics conversion tracking. Though, it looks like doing so is very easy: All you have to do is to add some GET parameters to your links, as shown in the example below:</p><pre class="brush: html">&lt;a href="http://www.mysite.com/page.php?utm_campaign=fall-sale&amp;utm_medium=email&amp;utm_source=female-list"&gt;Click here&lt;/a&gt;</pre><p>However, if you want to know more about click tracking using Google Analytics, you should have a look at <a
href="http://cutroni.com/blog/2008/11/04/email-tracking-with-google-analytics/">this article</a>.</p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/best-practices-for-coding-html-emails">Best practices for coding HTML emails</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/best-practices-for-coding-html-emails&amp;title=Best+practices+for+coding+HTML+emails" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/best-practices-for-coding-html-emails&amp;title=Best+practices+for+coding+HTML+emails&amp;body=Even%20if%20you%27re%20able%20to%20code%20complex%20website%20layouts%2C%20coding%20an%20html%20email%20is%20a%20hard%20job%20and%20there%27s%20lots%20of%20things%20to%20take%20into%20consideration.%20This%20article%20features%20the%20most%20important%20things%20I%27ve%20learned%20in%205%20years%20of%20coding%20html%20emails." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/best-practices-for-coding-html-emails&amp;title=Best+practices+for+coding+HTML+emails" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/best-practices-for-coding-html-emails&amp;title=Best+practices+for+coding+HTML+emails" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/best-practices-for-coding-html-emails&amp;t=Best+practices+for+coding+HTML+emails" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/best-practices-for-coding-html-emails&amp;title=Best+practices+for+coding+HTML+emails&amp;body=Even%20if%20you%27re%20able%20to%20code%20complex%20website%20layouts%2C%20coding%20an%20html%20email%20is%20a%20hard%20job%20and%20there%27s%20lots%20of%20things%20to%20take%20into%20consideration.%20This%20article%20features%20the%20most%20important%20things%20I%27ve%20learned%20in%205%20years%20of%20coding%20html%20emails." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/uWNtAkJbo5k" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/best-practices-for-coding-html-emails/feed</wfw:commentRss> <slash:comments>40</slash:comments> </item> <item><title>Thematic WordPress Theme Toolbox: 10 extremely useful hooks</title><link>http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks</link> <comments>http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks#comments</comments> <pubDate>Tue, 18 May 2010 15:42:08 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[WordPress]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3672</guid> <description><![CDATA[Do you like WordPress Theme Frameworks? To be honest, I'm a Thematic addict. Using this theme, I'm able to create any kind of site extremely fast. To help you getting started with Thematic child theme development, I have compiled 10 useful WordPress hooks in this article.<p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks">Thematic WordPress Theme Toolbox: 10 extremely useful hooks</a></p> ]]></description> <content:encoded><![CDATA[<p
class="alert">If you&#8217;re looking for a tutorial on how to create a Thematic child theme, you should read <a
href="http://www.catswhocode.com/blog/wordpress-how-to-easily-create-a-thematic-child-theme">this post</a>.</p><h2>Add a favicon</h2><p>A favicon is a small image displayed by modern web browsers. It is a must have for all websites, because it allows your visitors to quickly visualize your site among others when they have lots of browser tabs open at the same time.<br
/> This handy code will add your favicon to your theme. Make sure a favicon.png file is in your child theme images directory, and then paste the code in your functions.php file:</p><pre class="brush: php">function childtheme_favicon() { ?&gt;
	&lt;link rel="shortcut icon" href="&lt;?php echo bloginfo('stylesheet_directory') ?&gt;/images/favicon.png" /&gt;
&lt;?php }
add_action('wp_head', 'childtheme_favicon');</pre><p><strong>Source: <a></a></strong></p><h2>Add an Internet Explorer specific stylesheet</h2><p>Who doesn&#8217;t hate Internet Explorer? Unfortunately, most clients will require developers to make their site IE-compliant. And the best way to do so is to use some conditional comments and a dedicated stylesheet.<br
/> Create a file named ie.css in your child theme directory, and then insert the following code in your functions.php file:</p><pre class="brush: php">function childtheme_ie_style() { ?&gt;
	&lt;!--[if IE]&gt;
		&lt;link rel="stylesheet" type="text/css" href="http://www.webdevcat.com/wp-content/themes/webdevcat/ie.css" /&gt;
	&lt;![endif]--&gt;
&lt;?php }
add_action('wp_head', 'childtheme_ie_style');</pre><h2>Modify Doctype</h2><p>By default, Thematic outputs a XHTML 1.0 transitional doctype. If for some reason, you prefer using another kind of doctype, pasting the code below in your functions.php will do the trick.</p><pre class="brush: php">function childtheme_create_doctype($content) {
 $content = '&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;';
 $content .= "\n";
 $content .= '&lt;html xmlns="http://www.w3.org/1999/xhtml"';
 return $content;
}
add_filter('thematic_create_doctype', 'childtheme_create_doctype');</pre><h2>Use Feedburner RSS feeds</h2><p>Feedburner is very popular among bloggers. If you want to replace default rss feeds by feedburner feeds in Thematic, this code is for you.</p><pre class="brush: php">function childtheme_rssfeeds($content) {
	$content = "\t";
	$content .= "&lt;link rel=\"alternate\" type=\"application/rss+xml\" href=\"";
	$content .= "http://feeds2.feedburner.com/Catswhocode";
	$content .= "\" title=\"";
	$content .= wp_specialchars(get_bloginfo('name'), 1);
	$content .= " " . __('RSS feed', 'thematic');
	$content .= "\" /&gt;";
	$content .= "\n";
	return $content;
}
add_filter('thematic_rss', 'childtheme_rssfeeds');</pre><h2>Add Google Analytics code to your Thematic child theme</h2><p>Google Analytics is another free and very useful service. In order to allow GA to collect your visitor information and create your stats, you have to insert a small piece of Javascript on your footer.php file.<br
/> Insert this code in your functions.php file, save it, and you&#8217;re done. Of course, don&#8217;t forget to replace the Google Analytics code with your own!</p><pre class="brush: php">function ga(){ ?&gt;
	&lt;script type="text/javascript"&gt;
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	&lt;/script&gt;
	&lt;script type="text/javascript"&gt;
	try {
	var pageTracker = _gat._getTracker("UA-XXXXX-10");
	pageTracker._trackPageview();
	} catch(err) {}&lt;/script&gt;
&lt;?php }
add_action('wp_footer', 'ga');</pre><h2>Modify Thematic footer credit</h2><p>If you&#8217;re building a Thematic child theme for a client, you may want to insert your credit link in the footer text. The following code will do it. Don&#8217;t forget to give credit to Ian Stewart for his awesome work on the Thematic theme framework!</p><pre class="brush: php">function my_footer($thm_footertext) {
	$thm_footertext = 'Copyright &amp;copy; 2010 Jean-Baptiste Jung. &lt;a href="http://www.webdevcat.com"&gt;WebDevCat.com&lt;/a&gt; is obviously powered by &lt;a href="http://www.wordpress.org"&gt;WordPress&lt;/a&gt; &amp;amp; the &lt;a href="http://www.themeshaper.com/thematic"&gt;Thematic Theme framework&lt;/a&gt;.';
	return $thm_footertext;
}
add_filter('thematic_footertext', 'my_footer');</pre><h2>Display Thematic menu above header</h2><p>Want to have your navigation menu above your site header? That&#8217;s not a problem. This code will remove the menu from below the header and then put it above it.</p><pre class="brush: php">function remove_thematic_actions() {
    remove_action('thematic_header','thematic_access',9);
    add_action('thematic_aboveheader','search_access',9);
}
add_action('wp','remove_thematic_actions');</pre><h2>Change &#8220;more&#8221; link text</h2><p>Thematic &#8220;Read More&#8221; link displays <em>Read More »</em> by default. The following code allow you to change the &#8220;Read More&#8221; text.</p><pre class="brush: php">function childtheme_more_text($content) {
	$content = 'Click to read the rest!';
	return $content;
}
add_filter('more_text', 'childtheme_more_text');</pre><h2>Change gravatar size in Thematic comments</h2><p>By default, Thematic displays 35px*35px gravatars. If you want to change this size, no problem: Just paste this code in, as usual, your beloved functions.php file.</p><pre class="brush: php">function childtheme_avatarsize() {
    return '56';
}
add_action( 'avatar_size', 'childtheme_avatarsize' );</pre><p><strong>Source: <a
href="http://themeshaper.com/forums/topic/on-using-avatars-in-thematic">http://themeshaper.com/forums/topic/on-using-avatars-in-thematic</a></strong></p><h2>Remove Thematic menu on specific page template</h2><p>If you want to make a squeeze page on your site or blog using Thematic, that&#8217;s quite easy: You only have to create a page template and remove the menu. To do so, just paste the code below in your functions.php file.<br
/> Don&#8217;t forget to set your page template name on line 2.</p><pre class="brush: php">function remove_access() {
    if (is_page_template('affiliate.php')) {
        remove_action('thematic_header','thematic_access',9);
    }

add_action('wp_head','remove_access');
}</pre><p><strong>Source: <a
href="http://themeshaper.com/forums/topic/conditionally-removing-thematic_access">http://themeshaper.com/forums/topic/conditionally-removing-thematic_access</a></strong></p><p><strong><em>By the way, if you&#8217;re interested in Thematic Theme help, don&#8217;t hesitate to <a
href="http://www.catswhocode.com/blog/contact">ask me</a>. I just started freelancing and I&#8217;m ready to help you for a reasonable price.</em></strong></p><p><em>Like CatsWhoCode? If yes, don't hesitate to check my other blog <a
href="http://www.catswhoblog.com">CatsWhoBlog</a>: It's all about blogging!</em><br/><br/><a
href="http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks">Thematic WordPress Theme Toolbox: 10 extremely useful hooks</a></p><div
class="shr-bookmarks shr-bookmarks-center shr-bookmarks-bg-shr"><ul
class="socials"><li
class="shr-delicious"> <a
href="http://delicious.com/post?url=http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks&amp;title=Thematic+WordPress+Theme+Toolbox%3A+10+extremely+useful+hooks" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a></li><li
class="shr-designbump"> <a
href="http://designbump.com/submit?url=http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks&amp;title=Thematic+WordPress+Theme+Toolbox%3A+10+extremely+useful+hooks&amp;body=Do%20you%20like%20WordPress%20Theme%20Frameworks%3F%20To%20be%20honest%2C%20I%27m%20a%20Thematic%20addict.%20Using%20this%20theme%2C%20I%27m%20able%20to%20create%20any%20kind%20of%20site%20extremely%20fast.%20To%20help%20you%20getting%20started%20with%20Thematic%20child%20theme%20development%2C%20I%20have%20compiled%2010%20useful%20WordPress%20hooks%20in%20this%20article." rel="nofollow" class="external" title="Bump this on DesignBump">Bump this on DesignBump</a></li><li
class="shr-designfloat"> <a
href="http://www.designfloat.com/submit.php?url=http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks&amp;title=Thematic+WordPress+Theme+Toolbox%3A+10+extremely+useful+hooks" rel="nofollow" class="external" title="Submit this to DesignFloat">Submit this to DesignFloat</a></li><li
class="shr-digg"> <a
href="http://digg.com/submit?phase=2&amp;url=http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks&amp;title=Thematic+WordPress+Theme+Toolbox%3A+10+extremely+useful+hooks" rel="nofollow" class="external" title="Digg this!">Digg this!</a></li><li
class="shr-facebook"> <a
href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks&amp;t=Thematic+WordPress+Theme+Toolbox%3A+10+extremely+useful+hooks" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a></li><li
class="shr-webblend"> <a
href="http://thewebblend.com/submit?url=http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks&amp;title=Thematic+WordPress+Theme+Toolbox%3A+10+extremely+useful+hooks&amp;body=Do%20you%20like%20WordPress%20Theme%20Frameworks%3F%20To%20be%20honest%2C%20I%27m%20a%20Thematic%20addict.%20Using%20this%20theme%2C%20I%27m%20able%20to%20create%20any%20kind%20of%20site%20extremely%20fast.%20To%20help%20you%20getting%20started%20with%20Thematic%20child%20theme%20development%2C%20I%20have%20compiled%2010%20useful%20WordPress%20hooks%20in%20this%20article." rel="nofollow" class="external" title="Blend this!">Blend this!</a></li></ul><div
style="clear:both;"></div></div><img src="http://feeds.feedburner.com/~r/Catswhocode/~4/_UFgnQ212rE" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/thematic-wordpress-theme-toolbox-10-extremely-useful-hooks/feed</wfw:commentRss> <slash:comments>20</slash:comments> </item> </channel> </rss><!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk
Database Caching using disk

Served from: www.catswhocode.com @ 2010-08-25 00:26:19 -->
