<?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>Mon, 19 Jul 2010 14:08:03 +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>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>0</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>You can see the working form on my site <a
href="http://www.phpsnippets.info">PHP Snippets</a>. It is a site of mine, so don&#8217;t hesitate to grab the <a
href="http://feeds.feedburner.com/phps">RSS feed</a> and <a
href="http://twitter.com/phpsnippets">follow it on Twitter</a> if you want.<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>33</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>49</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>31</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>37</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>15</slash:comments> </item> <item><title>10 sites developers should have in their bookmarks</title><link>http://www.catswhocode.com/blog/10-sites-developers-should-have-in-their-bookmarks</link> <comments>http://www.catswhocode.com/blog/10-sites-developers-should-have-in-their-bookmarks#comments</comments> <pubDate>Sat, 08 May 2010 12:30:40 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[Web development]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3639</guid> <description><![CDATA[Over the millions of websites available, some are true tools for us web developers. In this article, I have compiled 10 extremely useful sites for web developers, that should definitely be added to your bookmarks.<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-sites-developers-should-have-in-their-bookmarks">10 sites developers should have in their bookmarks</a></p> ]]></description> <content:encoded><![CDATA[<h2>Mysql Format Date</h2><p>MySQL Format Date helps you to format your dates using the MySQL DATE_FORMAT function.  Just select a common date format and then change it to your suit your needs. The MySQL DATE_FORMAT code will be generated at the bottom of the page which you can then copy into your query.<img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/mysql.png" alt="" /><br
/> <strong>Visit site: <a
href="http://www.mysqlformatdate.com/">http://www.mysqlformatdate.com</a></strong></p><h2>Script Src</h2><p>Are you tired of hunting the Internet in order to find the script tag for the latest version of the Javascript library of your choice? ScriptSrc.net has compiled all the latest versions of jQuery, Mootools, Prototype and more in a single page which lets you copy it in your browser clipboard with a single click.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/scriptsrc.png" alt="" /><br
/> <strong>Visit site: <a
href="http://scriptsrc.net/">http://scriptsrc.net</a></strong></p><h2>Em Chart</h2><p>I never been a fan of <em>ems</em> in CSS files, but sometimes you have to deal with it. In that case, Em chart will translate ems to pixels so you&#8217;ll save time and hassle.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/emchart.png" alt="" /><br
/> <strong>Visit site: <a
href="http://aloestudios.com/tools/emchart">http://aloestudios.com/tools/emchart</a></strong></p><h2>Twitter API Explorer</h2><p>If you&#8217;re using the Twitter API in the site you build, you&#8217;ll for sure enjoy this very handy website which allow you to search through the Twitter API. Even better, the website can generate ready-to-use code snippets. A real time gain for you and your clients!<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/twitterapi.png" alt="" /><br
/> <strong>Visit site: <a
href="http://twitapi.com/explore">http://twitapi.com/explore</a></strong></p><h2>Browser Sandbox</h2><p>Cross browser compatibility is definitely one of the biggest problems a web developer has to face in his daily job. The browser sandbox lets you run any Windows browser from the web. The only bad thing is that you must run a Windows machine: The app does not work on Macs and GNU/Linux.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/spoon.png" alt="" /><br
/> <strong>Visit site: <a
href="http://spoon.net/browsers/">http://spoon.net/browsers</a></strong></p><h2>PHP Forms</h2><p>Web forms are one of the most important part of a website, but creating them is also very time-consuming. So, what about using a website that can speed up your form development for free?<br
/> PHP forms allows you to create advanced forms that can fit the needs of most websites.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/phpforms.png" alt="" /><br
/> <strong>Visit site: <a
href="http://www.phpform.org">http://www.phpform.org</a></strong></p><h2>.htaccess editor</h2><p>A .htaccess file is a must have for any website. Don&#8217;t know how to write one? No problem, just visit this site to create your .htaccess file using a wizard. It doesn&#8217;t allow very advanced stuff, but the results are great for 95% of the websites you&#8217;ll make.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/htaccess.png" alt="" /><br
/> <strong>Visit site: <a
href="http://www.htaccesseditor.com/en.shtml">http://www.htaccesseditor.com/en.shtml</a></strong></p><h2>Smush it!</h2><p>Images may be worth a thousand words, they&#8217;re also well known to use a lot of bandwidth. Images can be optimized for the web using programs like Photoshop; but if you don&#8217;t own a copy of this software or simply don&#8217;t have a clue how to do it, smush.it is what you need.<br
/> Brought to you by Yahoo developers network, Smush.it is an online tool that will reduce your image size without reducing their quality. For WordPress users, a very handy plugin for your favorite blogging engine is available <a
href="http://wordpress.org/extend/plugins/wp-smushit/">here</a>.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/smushit.png" alt="" /><br
/> <strong>Visit site: <a
href="http://developer.yahoo.com/yslow/smushit/">http://developer.yahoo.com/yslow/smushit/</a></strong></p><h2>CSS Compressor</h2><p>Especially on site with many different page layouts, CSS files can become huge and use a lot of server bandwidth. This tool, named CSS Compressor, can consequently reduce the size of any CSS file by removing comments, indentation and more.<br
/> Even better, compression level can be configured to fit your needs.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/csscompressor.png" alt="" /><br
/> <strong>Visit site: <a
href="http://www.csscompressor.com">http://www.csscompressor.com</a></strong></p><h2>Test everything</h2><p>This site is a definitive must-have for your bookmarks: As the name says, Test everything allows you to test lot of things such as XHTML and CSS markup, PageRank, back-links, and a lot more.<br
/> <img
src="http://www.catswhocode.com/blog/wp-content/uploads/2010/05/testeverything.png" alt="" /><br
/> <strong>Visit site: <a
href="http://tester.jonasjohn.de">http://tester.jonasjohn.de</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-sites-developers-should-have-in-their-bookmarks">10 sites developers should have in their bookmarks</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-sites-developers-should-have-in-their-bookmarks&amp;title=10+sites+developers+should+have+in+their+bookmarks" 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-sites-developers-should-have-in-their-bookmarks&amp;title=10+sites+developers+should+have+in+their+bookmarks&amp;body=Over%20the%20millions%20of%20websites%20available%2C%20some%20are%20true%20tools%20for%20us%20web%20developers.%20In%20this%20article%2C%20I%20have%20compiled%2010%20extremely%20useful%20sites%20for%20web%20developers%2C%20that%20should%20definitely%20be%20added%20to%20your%20bookmarks." 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-sites-developers-should-have-in-their-bookmarks&amp;title=10+sites+developers+should+have+in+their+bookmarks" 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-sites-developers-should-have-in-their-bookmarks&amp;title=10+sites+developers+should+have+in+their+bookmarks" 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-sites-developers-should-have-in-their-bookmarks&amp;t=10+sites+developers+should+have+in+their+bookmarks" 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-sites-developers-should-have-in-their-bookmarks&amp;title=10+sites+developers+should+have+in+their+bookmarks&amp;body=Over%20the%20millions%20of%20websites%20available%2C%20some%20are%20true%20tools%20for%20us%20web%20developers.%20In%20this%20article%2C%20I%20have%20compiled%2010%20extremely%20useful%20sites%20for%20web%20developers%2C%20that%20should%20definitely%20be%20added%20to%20your%20bookmarks." 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/RC5KRk2y90s" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/10-sites-developers-should-have-in-their-bookmarks/feed</wfw:commentRss> <slash:comments>60</slash:comments> </item> <item><title>Top WordPress hacks of early 2010</title><link>http://www.catswhocode.com/blog/top-wordpress-hacks-of-early-2010</link> <comments>http://www.catswhocode.com/blog/top-wordpress-hacks-of-early-2010#comments</comments> <pubDate>Mon, 26 Apr 2010 14:27:39 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[WordPress]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3626</guid> <description><![CDATA[The first months of 2010 have been extremely prolific in terms of WordPress hacks. In this article, I have compiled 10 new WordPress hacks that you should definitely add to your library.<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/top-wordpress-hacks-of-early-2010">Top WordPress hacks of early 2010</a></p> ]]></description> <content:encoded><![CDATA[<h2>Display an incrementing number on each post</h2><p>I always loved how <a
href="http://www.alistapart.com/">A List Apart</a> numbers its posts. The following hack will let you do the same with your own blog, using a custom field.</p><p>Implementing this hack is quite simple. First, paste the following function into your functions.php file:</p><pre class="brush: php">function updateNumbers() {
  global $wpdb;
  $querystr = "SELECT $wpdb-&gt;posts.* FROM $wpdb-&gt;posts WHERE $wpdb-&gt;posts.post_status = 'publish' AND $wpdb-&gt;posts.post_type = 'post' ";
$pageposts = $wpdb-&gt;get_results($querystr, OBJECT);
  $counts = 0 ;
  if ($pageposts):
    foreach ($pageposts as $post):
      setup_postdata($post);
      $counts++;
      add_post_meta($post-&gt;ID, 'incr_number', $counts, true);
      update_post_meta($post-&gt;ID, 'incr_number', $counts);
    endforeach;
  endif;
}  

add_action ( 'publish_post', 'updateNumbers' );
add_action ( 'deleted_post', 'updateNumbers' );
add_action ( 'edit_post', 'updateNumbers' );</pre><p>Once done, you can display the post number with the following code. Note that it have to be used within the loop.</p><pre class="brush: php">&lt;?php echo get_post_meta($post-&gt;ID,'incr_number',true); ?&gt;</pre><p><strong>Source: <a
href="http://www.wprecipes.com/how-to-display-an-incrementing-number-next-to-each-published-post">http://www.wprecipes.com/how-to-display-an-incrementing-number-next-to-each-published-post</a></strong></p><h2>Allow your contributors to upload files</h2><p>If you&#8217;re like me, you have guest contributing articles on your blog and you might be annoyed that the contributor role doesn&#8217;t allow file uploads. Most blog posts need images to stand out of the crowd so<br
/> this hack is extremely handy: Just paste it on your function.php file and your contributors will be allowed to upload files in the WordPress dashboard. How cool is that?</p><pre class="brush: php">if ( current_user_can('contributor') &amp;&amp; !current_user_can('upload_files') )
    add_action('admin_init', 'allow_contributor_uploads');

function allow_contributor_uploads() {
    $contributor = get_role('contributor');
    $contributor-&gt;add_cap('upload_files');
}</pre><p><strong>Source: <a
href="http://www.wprecipes.com/wordpress-tip-allow-contributors-to-upload-files">http://www.wprecipes.com/wordpress-tip-allow-contributors-to-upload-files</a></strong></p><h2>Display &#8220;time ago&#8221; dates</h2><p>Twitter has a very cool function which displays the elapsed time since a tweet has been published. What about doing the same with WordPress? Of course it&#8217;s possible!<br
/> This code just needs to be pasted in your functions.php file. Once you saved the file, posts that were published less than 24 hours ago will display &#8220;Published XX ago&#8221; instead of regular dates.</p><pre class="brush: php">add_filter('the_time', 'timeago');

function timeago() {
    global $post;
    $date = $post-&gt;post_date;
    $time = get_post_time('G', true, $post);
    $time_diff = time() - $time;
    if ( $time_diff &gt; 0 &amp;&amp; $time_diff &lt; 24*60*60 )
        $display = sprintf( __('%s ago'), human_time_diff( $time ) );
    else
        $display = date(get_option('date_format'), strtotime($date) );

    return $display;
}</pre><p>By the way, if you&#8217;re on Twitter do not hesitate to <a
href="http://www.twitter.com/catswhocode">follow me</a>!<br
/> <strong>Source: <a
href="http://aext.net/2010/04/display-timeago-for-wordpress-if-less-than-24-hours/">http://aext.net/2010/04/display-timeago-for-wordpress-if-less-than-24-hours/</a></strong></p><h2>WordPress navigation outside the loop</h2><p>WordPress provides some functions which allow you to link to the next and previous posts. However, those functions have to be used within the loop. Jeff Starr, who wrote the <a
href="http://www.catswhocode.com/blog/diw.html" target="blank">Digging into WordPress</a> book, has the solution to this problem.<br
/> Simply paste the code below on your single.php file, where you&#8217;d like to link to the next and previous posts. Or even better, put the code in a php file and then include it in your theme file.</p><pre class="brush: php">&lt;?php if(is_single()) { // single-view navigation ?&gt;
	&lt;?php $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
		&lt;?php previous_post_link(); ?&gt; | &lt;?php next_post_link(); ?&gt;
	&lt;?php endwhile; endif; ?&gt;
&lt;?php } else { // archive view navigation ?&gt;
		&lt;?php posts_nav_link(); ?&gt;
&lt;?php } ?&gt;</pre><p><strong>Source: <a
href="http://digwp.com/2010/04/post-navigation-outside-loop/">http://digwp.com/2010/04/post-navigation-outside-loop/</a></strong></p><h2>Disallow theme switching</h2><p>If you&#8217;re like me, you&#8217;ve created WordPress themes for your clients and already face a problem: The client &#8220;explored&#8221; the WordPress dashboard and &#8220;accidentally&#8221; switched the theme.<br
/> Using WordPress actions, we can easily remove the &#8220;themes&#8221; menu and consequently prevent the risk of having a client switching the theme. The code below just has to be pasted in your functions.php. The &#8220;themes&#8221; menu will be removed once the file is saved.</p><pre class="brush: php">add_action('admin_init', 'remove_theme_menus');
function remove_theme_menus() {
	global $submenu;	

	unset($submenu['themes.php'][5]);
	unset($submenu['themes.php'][15]);
}</pre><p><strong>Source: <a
href="http://soulsizzle.com/quick-tips/stopping-clients-from-switching-their-wordpress-theme/">http://soulsizzle.com/quick-tips/stopping-clients-from-switching-their-wordpress-theme/</a></strong></p><h2>Get rid of unused shortcodes in your posts</h2><p>WordPress shortcodes are extremely useful, but they have a weak point: If you use a shortcode in your posts and then stop to use it for some reason, the shortcode code (Like <em>[shortcode]</em> for example) will stay in your posts.</p><p>To get rid of unused shortcodes, you just have to execute this line of SQL code. This can be done using PhpMyAdmin or the SQL command line interpreter. Don&#8217;t forget to replace <em>[tweet]</em> by the unused shortcode you&#8217;d like to delete from your posts.</p><pre class="brush: sql">UPDATE wp_post SET post_content = replace(post_content, '[tweet]', '' );</pre><p><strong>Source: <a
href="http://www.wprecipes.com/wordpress-tip-get-rid-of-unused-shortcodes">http://www.wprecipes.com/wordpress-tip-get-rid-of-unused-shortcodes</a></strong></p><h2>Switch WordPress theme programmatically</h2><p>Recently, I worked on an interesting project where I had to switch the blog theme automatically. As the current WordPress theme name is saved in the wp_options table of your WordPress database, we can easily change it.<br
/> The cleanest way to do it is definitely to use the <em>update_option() </em>function, as shown in the function below. Paste it in your functions.php file.</p><pre class="brush: php">function updateTheme($theme){
    update_option('template', $theme);
    update_option('stylesheet', $theme);
    update_option('current_theme', $theme);
}</pre><p>Once you&#8217;ve added the function to your functions.php file, you can call it wherever you need it:</p><pre class="brush: php">&lt;php updateTheme('default'); ?&gt;</pre><h2>Modify WordPress dashboard footer text</h2><p>Another good tip for those who create WordPress themes for clients is to modify the WordPress dashboard footer text, and add (for example) a link to your support forum. The only thing you have to do is to copy this code and paste it in functions.php:</p><pre class="brush: php">function remove_footer_admin () {
    echo "Your own text";
} 

add_filter('admin_footer_text', 'remove_footer_admin');</pre><p><strong>Source: <a
href="http://www.wprecipes.com/wordpress-tip-how-to-change-the-dashboard-footer-text">http://www.wprecipes.com/wordpress-tip-how-to-change-the-dashboard-footer-text</a></strong></p><h2>Programmatically Creating Posts in WordPress</h2><p>If for some reason you need to programmatically insert posts in WordPress database, you&#8217;ll be amazed to see how easy is it. The <em>wp_insert_post()</em> takes an array of data as a parameter, and then return the post ID.</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><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>WordPress 3.0: Query custom post types</h2><p>WordPress 3.0 should be released soon. And I don&#8217;t know about you, but personally, I can&#8217;t wait. Lots of exiting features are scheduled. One of them is particularly interesting in my opinion: the custom post types, which allow you to define a custom type for a post.<br
/> In order to be able to retrieve posts of a specific type from a WordPress database, you can use the following loop, which will get the <em>albums</em> post type:</p><pre class="brush: php">&lt;ul&gt;
&lt;?php global $wp_query;
$wp_query = new WP_Query("post_type=albums&amp;post_status=publish");

while ($wp_query-&gt;have_posts()) : $wp_query-&gt;the_post(); ?&gt;
    &lt;li&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
&lt;?php endwhile; ?&gt;

&lt;/ul&gt;</pre><p><strong>Source: <a
href="http://www.catswhocode.com/blog/8-useful-code-snippets-to-get-started-with-wordpress-3-0">http://www.catswhocode.com/blog/8-useful-code-snippets-to-get-started-with-wordpress-3-0</a></strong></p><p><strong><em>Please note that I&#8217;m currently accepting freelance work; so if you need any kind of WordPress help, I&#8217;ll be happy to help you. Simply <a
href="http://www.catswhocode.com/blog/contact">send me an email</a> and I&#8217;ll get back to you.</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/top-wordpress-hacks-of-early-2010">Top WordPress hacks of early 2010</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/top-wordpress-hacks-of-early-2010&amp;title=Top+WordPress+hacks+of+early+2010" 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/top-wordpress-hacks-of-early-2010&amp;title=Top+WordPress+hacks+of+early+2010&amp;body=The%20first%20months%20of%202010%20have%20been%20extremely%20prolific%20in%20terms%20of%20WordPress%20hacks.%20In%20this%20article%2C%20I%20have%20compiled%2010%20new%20WordPress%20hacks%20that%20you%20should%20definitely%20add%20to%20your%20library." 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/top-wordpress-hacks-of-early-2010&amp;title=Top+WordPress+hacks+of+early+2010" 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/top-wordpress-hacks-of-early-2010&amp;title=Top+WordPress+hacks+of+early+2010" 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/top-wordpress-hacks-of-early-2010&amp;t=Top+WordPress+hacks+of+early+2010" 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/top-wordpress-hacks-of-early-2010&amp;title=Top+WordPress+hacks+of+early+2010&amp;body=The%20first%20months%20of%202010%20have%20been%20extremely%20prolific%20in%20terms%20of%20WordPress%20hacks.%20In%20this%20article%2C%20I%20have%20compiled%2010%20new%20WordPress%20hacks%20that%20you%20should%20definitely%20add%20to%20your%20library." 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/3NxTbam7FC4" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/top-wordpress-hacks-of-early-2010/feed</wfw:commentRss> <slash:comments>41</slash:comments> </item> <item><title>Manipulating the DOM with jQuery: 10+ useful code snippets</title><link>http://www.catswhocode.com/blog/manipulating-the-dom-with-jquery-10-useful-code-snippets</link> <comments>http://www.catswhocode.com/blog/manipulating-the-dom-with-jquery-10-useful-code-snippets#comments</comments> <pubDate>Tue, 20 Apr 2010 13:58:16 +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=3612</guid> <description><![CDATA[The Document Object Model is an API which allows developers to access, read, and modify the content of a web page. In this article, I'm going to show you 10+ extremely useful code snippets to manipulate the DOM using jQuery.<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/manipulating-the-dom-with-jquery-10-useful-code-snippets">Manipulating the DOM with jQuery: 10+ useful code snippets</a></p> ]]></description> <content:encoded><![CDATA[<h2>Add a CSS class to a specific element</h2><p>A very clean way to change an element look and feel is to add a css class, instead of adding inline styles. Using jQuery, this is pretty easy to do:</p><pre class="brush: javascript">$('#myelement').addClass('myclass');</pre><h2>Removing a CSS class from a specific element</h2><p>It&#8217;s great to be able to add some CSS classes, but we also need to know how to remove unwanted classes. The following line of code will do that:</p><pre class="brush: javascript">$('#myelement').removeClass('myclass');</pre><h2>Check if a specific element has a CSS class</h2><p>If your application or site is frequently adding and removing classes to a particular element, it can be very useful to be able to check out if an element has a certain CSS class.</p><pre class="brush: javascript">$(id).hasClass(class)</pre><h2>Switch CSS using jQuery</h2><p>As we saw with the previous examples, adding or removing css styles to an element is very simple using jQuery. But what if you want to completely remove the document css file and attach a new stylesheet? Good news, it is extremely simple as well, as shown in the following example:</p><pre class="brush: javascript">$('link[media='screen']').attr('href', 'Alternative.css');</pre><p><strong>Source: <a
href="http://addyosmani.com/blog/50-jquery-snippets-for-developers/">http://addyosmani.com/blog/50-jquery-snippets-for-developers/</a></strong></p><h2>Append HTML to an element</h2><p>When you need to append some html to an element, the <em>append()</em> method is a life saver:</p><pre class="brush: javascript">$('#lal').append('sometext');</pre><h2>Check if an element exists</h2><p>When working with Javascript, we often need to check if an element exists or not. Using jQuery and the <em>length</em> property, it is very simple to do: If length == 0, no elements are used on the page. Otherwise, some are used.</p><pre class="brush: javascript">if ($('img').length) {
    log('We found img elements on the page using "img"');
} else {
    log('No img elements found');
}</pre><p><strong>Source: <a
href="http://jqueryfordesigners.com/element-exists/">http://jqueryfordesigners.com/element-exists/</a></strong></p><h2>Get the parent element of an element</h2><p>When working with the DOM, you may need to know which element is the direct parent of another element. The <em>closest()</em> method will let you know:</p><pre class="brush: javascript">var id = $("button").closest("div").attr("id");</pre><p><strong>Source: <a
href="http://stackoverflow.com/questions/545978/finding-the-id-of-a-parent-div-using-jquery">http://stackoverflow.com/questions/545978/finding-the-id-of-a-parent-div-using-jquery</a></strong></p><h2>Get element siblings</h2><p>The siblings() method is a very handy tool to get siblings of an element. As shown below, using this method is extremely simple:</p><pre class="brush: javascript">$("div").siblings()</pre><h2>Remove an option from a select list</h2><p>When working with <em>select</em> lists, you may want to update the content according to the user actions. To remove an option from a select list, the following code will do the trick:</p><pre class="brush: javascript">$("#selectList option[value='2']").remove();</pre><p><strong>Source: <a
href="http://calisza.wordpress.com/2009/03/29/6-jquery-snippets-you-can-use-to-manipulate-select-inputs/">http://calisza.wordpress.com/2009/03/29/6-jquery-snippets-you-can-use-to-manipulate-select-inputs/</a></strong></p><h2>Get the selected option as text</h2><p>Extremely useful when you need to quickly check out what a visitor selected from your select list.</p><pre class="brush: javascript">$('#selectList :selected').text();</pre><h2>Apply a &#8220;zebra&#8221; effect on tables</h2><p>When using tables, it is a good idea, for better readability, to give a different style to one line out of two. Using jQuery, this can be done easily, without any additional html markup.</p><pre class="brush: javascript">$("tr:odd").addClass("odd");</pre><p><strong>Source: <a
href="http://web.enavu.com/tutorials/top-10-jquery-snippets-including-jquery-1-4/">http://web.enavu.com/tutorials/top-10-jquery-snippets-including-jquery-1-4/</a></strong></p><h2>Count children of an element</h2><p>If you&#8217;d like to count how many <em>div</em> elements are children of <em>#foo</em>, the following line will let you know. Simple and efficient!</p><pre class="brush: javascript">$("#foo &gt; div").length</pre><p><strong>Source: <a
href="http://tympanus.net/codrops/2010/01/05/some-useful-javascript-jquery-snippets/">http://tympanus.net/codrops/2010/01/05/some-useful-javascript-jquery-snippets/</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/manipulating-the-dom-with-jquery-10-useful-code-snippets">Manipulating the DOM with jQuery: 10+ useful code 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/manipulating-the-dom-with-jquery-10-useful-code-snippets&amp;title=Manipulating+the+DOM+with+jQuery%3A+10%2B+useful+code+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/manipulating-the-dom-with-jquery-10-useful-code-snippets&amp;title=Manipulating+the+DOM+with+jQuery%3A+10%2B+useful+code+snippets&amp;body=The%20Document%20Object%20Model%20is%20an%20API%20which%20allows%20developers%20to%20access%2C%20read%2C%20and%20modify%20the%20content%20of%20a%20web%20page.%20In%20this%20article%2C%20I%27m%20going%20to%20show%20you%2010%2B%20extremely%20useful%20code%20snippets%20to%20manipulate%20the%20DOM%20using%20jQuery." 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/manipulating-the-dom-with-jquery-10-useful-code-snippets&amp;title=Manipulating+the+DOM+with+jQuery%3A+10%2B+useful+code+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/manipulating-the-dom-with-jquery-10-useful-code-snippets&amp;title=Manipulating+the+DOM+with+jQuery%3A+10%2B+useful+code+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/manipulating-the-dom-with-jquery-10-useful-code-snippets&amp;t=Manipulating+the+DOM+with+jQuery%3A+10%2B+useful+code+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/manipulating-the-dom-with-jquery-10-useful-code-snippets&amp;title=Manipulating+the+DOM+with+jQuery%3A+10%2B+useful+code+snippets&amp;body=The%20Document%20Object%20Model%20is%20an%20API%20which%20allows%20developers%20to%20access%2C%20read%2C%20and%20modify%20the%20content%20of%20a%20web%20page.%20In%20this%20article%2C%20I%27m%20going%20to%20show%20you%2010%2B%20extremely%20useful%20code%20snippets%20to%20manipulate%20the%20DOM%20using%20jQuery." 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/j2Cesj7yTRQ" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/manipulating-the-dom-with-jquery-10-useful-code-snippets/feed</wfw:commentRss> <slash:comments>27</slash:comments> </item> <item><title>8 useful code snippets to get started with WordPress 3.0</title><link>http://www.catswhocode.com/blog/8-useful-code-snippets-to-get-started-with-wordpress-3-0</link> <comments>http://www.catswhocode.com/blog/8-useful-code-snippets-to-get-started-with-wordpress-3-0#comments</comments> <pubDate>Mon, 12 Apr 2010 07:14:38 +0000</pubDate> <dc:creator>Jean-Baptiste Jung</dc:creator> <category><![CDATA[WordPress]]></category><guid isPermaLink="false">http://www.catswhocode.com/blog/?p=3595</guid> <description><![CDATA[WordPress 3.0, scheduled to launch May 1st, 2010, will be a real revolution for the blogging system. With new functionalities such as custom post types, developers will be able to create more complex and more powerful sites based on WordPress. In this article, I have compiled the most useful resources to get you started with WordPress 3.0.<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-useful-code-snippets-to-get-started-with-wordpress-3-0">8 useful code snippets to get started with WordPress 3.0</a></p> ]]></description> <content:encoded><![CDATA[<h2>How to create a custom post type</h2><p>Custom post type are an incredible step forward for WordPress, because it will allow developers to create post types according to their needs.<br
/> For now, we have <em>posts</em>, and <em>pages</em>. With WordPress 3.0, we&#8217;ll be able to create a new post type called <em>products</em>, where a client can sell his products only, while regular post for his blog.</p><p>Creating a custom post type is easy: All you have to do is to open your theme <em>functions.php</em> file and paste the following:</p><pre class="brush: php">$args = array(
        'label' =&gt; __('Products'),
        'singular_label' =&gt; __('Product'),
        'public' =&gt; true,
        'show_ui' =&gt; true,
        'capability_type' =&gt; 'page',
        'hierarchical' =&gt; false,
        'rewrite' =&gt; true,
        'query_var' =&gt; 'products',
        'supports' =&gt; array('title', 'thumbnail')
);
register_post_type( 'product' , $args );</pre><p>Once you saved the file, login to your WordPress dashboard and have a look at the navigation on the left: A new post type, named <em>Products</em>, has been added.<br
/> <strong>Source: <a
href="http://codex.wordpress.org/Function_Reference/register_post_type">http://codex.wordpress.org/Function_Reference/register_post_type</a></strong></p><h2>Custom post types with custom taxonomies</h2><p>In the previous example, I&#8217;ve shown you how to create a custom post type, which is pretty useful to use WordPress as a real CMS and not a simple blog publishing platform.</p><p>Now, let&#8217;s see something a little bit more complex, but extremely interesting: Creating a custom post type associated with custom taxonomies. For those who don&#8217;t know, a taxonomy is a term (such as category, tag or anything else) related to posts. For more information about taxonomies, you should have a look at <a
href="http://codex.wordpress.org/WordPress_Taxonomy">WordPress Codex</a>.</p><p>In this example, we&#8217;ll create a custom post type named Albums, which belong to &#8220;Genres&#8221; (the custom categories) and have &#8220;Performer&#8221; as tags. This snippet has to be pasted in your <em>functions.php</em> file. With those 27 lines of codes, you can create a fully functional music albums archive. Ain&#8217;t that powerful?</p><pre class="brush:php">function post_type_albums() {
	register_post_type(
                     'albums',
                     array('label' =&gt; __('Albums'),
                             'public' =&gt; true,
                             'show_ui' =&gt; true,
                             'supports' =&gt; array(
                                        'post-thumbnails',
                                        'excerpts',
                                        'trackbacks',
                                        'comments')
                                )
                      );
// Adding the Custom Taxonomy for Genres. Here we can create categories specific for this post type.
	register_taxonomy( 'genres', 'albums', array( 'hierarchical' =&gt; true, 'label' =&gt; __('Genres') ) );

// Adding the Custom Taxonomy for Performer. Here we can add tags specific for this post type.
        register_taxonomy( 'performer', 'albums',
		array(
                         'hierarchical' =&gt; false,
			 'label' =&gt; __('Performer'),
			 'query_var' =&gt; 'performer',
			 'rewrite' =&gt; array('slug' =&gt; 'performer' )
		)
	);
}
add_action('init', 'post_type_albums');</pre><p><strong>Source: <a
href="http://wpspecial.net/2010/03/how-to-add-custom-post-types-in-wordpress/">http://wpspecial.net/2010/03/how-to-add-custom-post-types-in-wordpress/</a></strong></p><h2>Query custom post types</h2><p>Now that you&#8217;ve learned how to create custom post types, the next step is to learn how to retrieve them from the WordPress database and display it on your blog.</p><p>Good news for developers, there&#8217;s nothing hard or new in the process. Custom post types can be retrieved easily, using the WP_Query object.<br
/> The following example will create a custom loop which will get only the <em>albums</em> custom post type.</p><pre class="brush: php">&lt;ul&gt;

&lt;?php global $wp_query;
$wp_query = new WP_Query("post_type=albums&amp;post_status=publish");

while ($wp_query-&gt;have_posts()) : $wp_query-&gt;the_post(); ?&gt;
    &lt;li&gt;&lt;a href="&lt;?php the_permalink() ?&gt;" rel="bookmark"&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/li&gt;
&lt;?php endwhile; ?&gt;
&lt;/ul&gt;</pre><h2>Enable multisite feature</h2><p>One of the most exiting new feature of WordPress 3.0 is definitely multisite management. In brief, with a single installation of WordPress you&#8217;ll be able to run a network of WordPress blog. How cool is that?</p><p>To take advantage of this feature, simply paste the following line of code in your <em>wp-config.php</em> file. This file is located at the root of your WordPress install.</p><pre class="brush: php">define('WP_ALLOW_MULTISITE', true);</pre><p>As <a
href="http://wpmututorials.com">andrea_r</a> pointed out, once you added the code above, you have to visit Tools -> Network and set up the network.<br
/> <strong>Source: <a
href="http://wptheming.com/2010/03/wordpress-3-0-enable-network/">http://wptheming.com/2010/03/wordpress-3-0-enable-network/</a></strong></p><h2>Custom author profiles</h2><p>Most of the top blogs of the industry do not have a single author but a team of different contributors. WordPress allows you to create author pages, but WordPress 3.0 is introducing a new function which will allow you to use different templates for different authors, like we can currently do with categories.</p><p>All you have to do is to create an author page named <em>author-XX.php</em> where XX is either the author ID or nicename. For example, if your user nicename is &#8220;john&#8221;, you&#8217;ll have to call the file <em>author-john.php</em>.</p><p><strong>Source: <a
href="http://codex.wordpress.org/Function_Reference/get_author_template">http://codex.wordpress.org/Function_Reference/get_author_template</a></strong></p><h2>Add custom backgrounds</h2><p>WordPress 3.0 is introducing a new feature that will for sure be loved by non tech-friendly users: Custom background. The feature allows the user to upload a background in his WordPress dashboard, specify its position, and automatically have it added to his blog.</p><p>Of course, the theme used by the blogger has to support this feature, otherwise the uploaded background will not be visible. To do so, simply open your <em>functions.php</em> file and paste the following line:</p><pre class="brush: php">add_custom_background();</pre><h2>Style WordPress editor using CSS</h2><p>WordPress features a WYSIWYG editor, which allow you to see text in bold, italic, and so on. But some people want more, such as being able to visualize their blog post in the blog font and colors.</p><p>This new feature allows you to create a css file (named <em>editor-style.css</em> in the example below) and link it to the editor for a better WYSIWYG rendering. Simply paste this code snippet to your <em>functions.php</em> file.</p><pre class="brush: php">add_filter('mce_css', 'my_editor_style');
function my_editor_style($url) {
  if ( !empty($url) )
    $url .= ',';
  // Change the path here if using sub-directory
  $url .= trailingslashit( get_stylesheet_directory_uri() ) . 'editor-style.css';

  return $url;
}</pre><p><strong>Source: <a
href="http://azaozz.wordpress.com/2010/01/02/can-themes-style-the-visual-editor/">http://azaozz.wordpress.com/2010/01/02/can-themes-style-the-visual-editor/</a></strong></p><h2>Make your theme compatible with WordPress 3.0 menus</h2><p>WordPress 3.0 is going to feature a totally new menu system, which will allow users to add only the desired pages, add categories, and more. Good news for theme developers; adding WP 3.0 menu support to your themes is extremely easy.</p><p>To do so, open functions.php and add the following line:</p><pre class="brush: php">add_theme_support( 'nav-menus' );</pre><p>Once added, you can use the brand new <em>wp_nav_menu()</em> function in your theme files:</p><pre class="brush: php">wp_nav_menu('sort_column=menu_order&amp;container_class=navigation');</pre><p>As you can see, it accepts the same kind of parameters than the good ol&#8217; <em>wp_list_categories()</em> function.<br
/> <strong>Source: <a
href="http://wpspecial.net/2010/04/menu-support-for-wordpress-3-0-themes/">http://wpspecial.net/2010/04/menu-support-for-wordpress-3-0-themes/</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-useful-code-snippets-to-get-started-with-wordpress-3-0">8 useful code snippets to get started 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/8-useful-code-snippets-to-get-started-with-wordpress-3-0&amp;title=8+useful+code+snippets+to+get+started+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/8-useful-code-snippets-to-get-started-with-wordpress-3-0&amp;title=8+useful+code+snippets+to+get+started+with+WordPress+3.0&amp;body=WordPress%203.0%2C%20scheduled%20to%20launch%20May%201st%2C%202010%2C%20will%20be%20a%20real%20revolution%20for%20the%20blogging%20system.%20With%20new%20functionalities%20such%20as%20custom%20post%20types%2C%20developers%20will%20be%20able%20to%20create%20more%20complex%20and%20more%20powerful%20sites%20based%20on%20WordPress.%20In%20this%20article%2C%20I%20have%20compiled%20the%20most%20useful%20resources%20to%20get%20you%20started%20with%20WordPress%203.0." 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-useful-code-snippets-to-get-started-with-wordpress-3-0&amp;title=8+useful+code+snippets+to+get+started+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/8-useful-code-snippets-to-get-started-with-wordpress-3-0&amp;title=8+useful+code+snippets+to+get+started+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/8-useful-code-snippets-to-get-started-with-wordpress-3-0&amp;t=8+useful+code+snippets+to+get+started+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/8-useful-code-snippets-to-get-started-with-wordpress-3-0&amp;title=8+useful+code+snippets+to+get+started+with+WordPress+3.0&amp;body=WordPress%203.0%2C%20scheduled%20to%20launch%20May%201st%2C%202010%2C%20will%20be%20a%20real%20revolution%20for%20the%20blogging%20system.%20With%20new%20functionalities%20such%20as%20custom%20post%20types%2C%20developers%20will%20be%20able%20to%20create%20more%20complex%20and%20more%20powerful%20sites%20based%20on%20WordPress.%20In%20this%20article%2C%20I%20have%20compiled%20the%20most%20useful%20resources%20to%20get%20you%20started%20with%20WordPress%203.0." 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/4qWn8jEY8sQ" height="1" width="1"/>]]></content:encoded> <wfw:commentRss>http://www.catswhocode.com/blog/8-useful-code-snippets-to-get-started-with-wordpress-3-0/feed</wfw:commentRss> <slash:comments>90</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-07-19 14:08:39 -->
