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

<channel>
	<title>Jeffri Hong &#187; Programming</title>
	<atom:link href="http://jeffri.net/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeffri.net</link>
	<description> </description>
	<lastBuildDate>Thu, 05 Apr 2012 23:49:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Sort by latest post for wp_list_categories</title>
		<link>http://jeffri.net/2012/01/sort-by-latest-post-for-wp_list_categories/</link>
		<comments>http://jeffri.net/2012/01/sort-by-latest-post-for-wp_list_categories/#comments</comments>
		<pubDate>Wed, 25 Jan 2012 20:18:53 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[source code]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=816</guid>
		<description><![CDATA[One of my developer friend asked me if we could sort by last post using wp_list_categories function for WordPress. By default, wp_list_categories accept arguments for order by ID, name, slug, count or term_group. Order by latest post is not possible by default, but with a little of tweak using filter hook, we can. First, we&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>One of my developer friend asked me if we could sort by last post using <a href="http://codex.wordpress.org/Template_Tags/wp_list_categories" target="_blank"><em>wp_list_categories</em></a> function for WordPress. By default, <em>wp_list_categories</em> accept arguments for order by <em>ID</em>, <em>name</em>, <em>slug</em>, <em>count</em> or <em>term_group</em>. Order by latest post is not possible by default, but with a little of tweak using filter hook, we can. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>First, we&#8217;ll looking at the function <em>wp_list_categories</em>. This function made call to <em>get_categories</em> to get the list of categories, which made another call to <em>get_terms</em>. Categories in WordPress is basically <em>terms</em> with category <em>taxonomy</em>. Finally, looking on the <em>get_terms</em> function, we will find some delicious filter hook that suitable for our customization.</p>
<p><span id="more-816"></span></p>
<p>To be able to change the order, we will have to find a way to manipulate the SQL query and add our magic. Finally, we found the filter hook to add the magic, <em><strong>terms_clauses</strong></em>. The code to call the apply filter is as below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pieces</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'fields'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'join'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'where'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'orderby'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'order'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'limits'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$clauses</span> <span style="color: #339933;">=</span> apply_filters<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'terms_clauses'</span><span style="color: #339933;">,</span> <span style="color: #990000;">compact</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pieces</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxonomies</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As you can see, the $pieces hold a separate pieces of the SQL query. We can add our own query to this separate pieces to form a query for our need. The code we used is as follow:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> filter_term_sort_by_latest_post_clauses<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pieces</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxonomies</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'category'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxonomies</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'orderby'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'latest_post'</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'fields'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;, MAX(p.post_date) AS last_date&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'join'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_relationships</span> AS tr JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> AS p ON p.ID=tr.object_id AND tr.term_taxonomy_id=tt.term_taxonomy_id&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'where'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; GROUP BY t.term_id&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'orderby'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ORDER BY last_date&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$pieces</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'order'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;DESC&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// DESC or ASC</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$pieces</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'terms_clauses'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'filter_term_sort_by_latest_post_clauses'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Add this to <em>functions.php</em> or on your own plugin code, or where ever you wanted, it just needed to be placed before any call of <em>wp_list_categories</em>. Note that we only add this filter if the taxonomy is category, and if the <em>orderby</em> argument is set to <em>latest_post</em>. So our call to <em>wp_list_categories</em> is now:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">wp_list_categories<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'orderby'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'latest_post'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Now your <em>wp_list_categories</em> call will have option to sort by latest post. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope it&#8217;s useful for you. Comments is appreciated. <img src='http://jeffri.net/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2012/01/sort-by-latest-post-for-wp_list_categories/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>WordPress Snippets part 1</title>
		<link>http://jeffri.net/2011/05/wordpress-snippets-part-1/</link>
		<comments>http://jeffri.net/2011/05/wordpress-snippets-part-1/#comments</comments>
		<pubDate>Tue, 10 May 2011 07:33:27 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[snippets]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=764</guid>
		<description><![CDATA[Well, this will be a short list of WordPress snippets that we could use. This snippets is collected from my daily work on WordPress and should be useful for daily basis. WordPress mail snippets WordPress has its own improved function for sending email, it is wp_mail. We should always use this function if we wanted [...]]]></description>
			<content:encoded><![CDATA[<p>Well, this will be a short list of WordPress snippets that we could use. This snippets is collected from my daily work on WordPress and should be useful for daily basis.</p>
<h2>WordPress mail snippets</h2>
<p>WordPress has its own improved function for sending email, it is <a href="http://codex.wordpress.org/Function_Reference/wp_mail">wp_mail</a>. We should always use this function if we wanted to send email within WordPress. With this function, we don&#8217;t need to worry much about the mail header and stuff. More over, there is plenty of filter we can use to customize it.</p>
<p><span id="more-764"></span></p>
<h3>Set content type to HTML</h3>
<p>We can send HTML email by set the content type of email sent by WordPress. To do this, add this function to functions.php in your theme file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_email_html_type<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$content</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'text/html'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_content_type'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_email_html_type'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Set mail sender name</h3>
<p>This function will set the mail sender name.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_mail_from_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Jeffri Hong&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_from_name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_mail_from_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We could improve it by automatically set it to the first administrator name.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_mail_from_name<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$admins</span> <span style="color: #339933;">=</span> get_users<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'role'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'administrator'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$admins</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$first_name</span> <span style="color: #339933;">=</span> get_user_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$admins</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'first_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$last_name</span> <span style="color: #339933;">=</span> get_user_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$admins</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'last_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$first_name</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$last_name</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$first_name</span> <span style="color: #006699; font-weight: bold;">$last_name</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Jeffri Hong&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// the default value when it is failed to retrieve first/last name</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_from_name'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_mail_from_name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Set mail sender email</h3>
<p>This function will set the mail sender email address.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_mail_from<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$email</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;info@jeffri.net&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_from'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_mail_from'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As with the previous one, it is also possible to automatically set it to the first administrator email.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> set_mail_from<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$email</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$admins</span> <span style="color: #339933;">=</span> get_users<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'role'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'administrator'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$admins</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$admins</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_email</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$admins</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">user_email</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;info@jeffri.net&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_mail_from'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'set_mail_from'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h2>Get post meta ordered by id</h2>
<p>Getting a post meta, especially an array, can some time return an unexpected result. The problem is the <a href="http://codex.wordpress.org/Function_Reference/get_post_meta">get_post_meta</a> function rely on the default SQL order. In few of my job, I need to use multiple post meta and keep it in order with each order, and it won&#8217;t work correctly unless we add the ORDER BY clause to the query. Unfortunately, the metadata functions didn&#8217;t have many filters we can work with. However, there is a filter we can use to override the function return value.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> filter_post_meta<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$object_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$meta_key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$single</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_results</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT m.meta_value FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;postmeta</span> m WHERE m.meta_key = '<span style="color: #006699; font-weight: bold;">$meta_key</span>' AND m.post_id = '<span style="color: #006699; font-weight: bold;">$object_id</span>' ORDER BY m.meta_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$ret</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$result</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #000088;">$ret</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">meta_value</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$ret</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_post_metadata'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'filter_post_meta'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>With this, every call of get_post_meta will be filtered by this function and return all post meta filtered by meta_id. The good news is, this filter run on top of get_metadata function and returned it immediately when there is a value.</p>
<h2>Add post slug as CSS class to page lists</h2>
<p>Often, we need to have a more definitive and unique class for page lists generated by function like <a href="http://codex.wordpress.org/Function_Reference/wp_list_pages">wp_list_pages</a> and other. By default, there is unique class for the page ID, but since page ID is generated automatically, this value vary on site to site &#8211; for example when you have a development version and live version of a site, you will need an alternative for unique class other than ID. Post slug is the better solution in this case, since it is defined by us and unique.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> add_page_css_class<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$classes</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$classes</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'page-'</span><span style="color: #339933;">.</span><span style="color: #000088;">$page</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">post_name</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$classes</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page_css_class'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'add_page_css_class'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Add this function to functions.php of your themes. Now, you will have one more class selector for your CSS.</p>
<h2>And more to come</h2>
<p>That&#8217;s it folks for the part 1. More will come in near future as I still have many interesting case I experienced and a nice solution that is re-usable anywhere. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope that useful for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2011/05/wordpress-snippets-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Customize Custom Post Type Landing Page with Clean URL</title>
		<link>http://jeffri.net/2011/05/customize-custom-post-type-landing-page-with-clean-url/</link>
		<comments>http://jeffri.net/2011/05/customize-custom-post-type-landing-page-with-clean-url/#comments</comments>
		<pubDate>Wed, 04 May 2011 01:00:32 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[post type]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=672</guid>
		<description><![CDATA[WordPress has been long capable on using custom post type. Since WordPress 3.0, we are now easier to create a custom post type. I have blogged some tips about it. However, many didn&#8217;t know that the custom post type already had it&#8217;s own landing page, though, you will need to pass the post type variable [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress has been long capable on using custom post type. Since WordPress 3.0, we are now easier to create a custom post type. I have <a href="http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/">blogged some tips about it</a>.</p>
<p>However, many didn&#8217;t know that the custom post type already had it&#8217;s own landing page, though, you will need to pass the post type variable to the URL. This post will guide you to the step by step on customizing the post type landing page, as well as adding a new rewrite rule for a cleaner URL.</p>
<p><span id="more-672"></span></p>
<h2>Register the Post Type</h2>
<p>First of all, let&#8217;s create our custom post type. In this example, we will register a <em>book</em> post type.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> theme_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$labels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
		<span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Books'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'singular_name'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'add_new'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Add Book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'add_new_item'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Add New Book Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'edit_item'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Edit Book Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'new_item'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'New Book Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'view_item'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'View Book Item'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'search_items'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Search Books'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'not_found'</span> <span style="color: #339933;">=&gt;</span>  __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Nothing found'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #0000ff;">'not_found_in_trash'</span> <span style="color: #339933;">=&gt;</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Nothing found in Trash'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> 
		<span style="color: #0000ff;">'parent_item_colon'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">''</span>
	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    	<span style="color: #0000ff;">'labels'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$labels</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'public'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'publicly_queryable'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'show_ui'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
    	<span style="color: #0000ff;">'query_var'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'rewrite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'capability_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'hierarchical'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'menu_position'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    	<span style="color: #0000ff;">'supports'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'editor'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'author'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'thumbnail'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'excerpt'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'comments'</span><span style="color: #009900;">&#41;</span>
  	<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  	register_post_type<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'theme_init'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Add this code to the theme functions.php. We hook the function to the <em>init</em> action. In case you didn&#8217;t know the WordPress hook, <a href="http://codex.wordpress.org/Plugin_API">you can take a good read on codex</a>.</p>
<p>The post type is now ready to use, it appeared on the back-end, you can add, edit and remove the book. The single post is accessible now. The landing page is accessible by appending <strong><em>?post_type=book</em></strong> to your website URL.</p>
<h2>Clean the URL</h2>
<p>Now, we want to make the URL cleaner. Instead of accessing the landing page with <em>yourwebsite.com/?post_type=book</em>, we want to make it <em>yourwebsite.com/book</em> instead. We can do this by adding a new rewrite rule.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> theme_rewrite<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	add_rewrite_rule<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book/?$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'index.php?post_type=book'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'top'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	add_rewrite_rule<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book/page/(\d+)/?$'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'index.php?post_type=book&amp;paged=$matches[1]'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'top'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">//flush_rewrite_rules(); // run one time only</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'theme_rewrite'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Again, add this to the theme functions.php file. We add a new rewrite rule and redirect it to <strong><em>index.php?post_type=book</em></strong>, don&#8217;t forget to use <em>&#8216;top&#8217;</em> in the third parameter, this is to make sure this rule must be run before any other WordPress rule. We also add another rule to work with pagination.</p>
<p>Notice the commented line above? After we add a new rewrite rule, we must flush the rewrite rules. This must be done one time when it is changed. So when you have it run once, comment or remove that line as it is not needed anymore. The rules will also flushed when you go to Settings -> Permalinks in back-end.</p>
<p>However, the add rewrite rule line must be keep though, as if you remove it, the next time it flushed, you will lost the rule.</p>
<h2>Customizing the Landing Page</h2>
<p>The current landing page used the same template as the home page, which in most case is either <em>front-page.php</em>, <em>home.php</em> or <em>index.php</em>. This isn&#8217;t good as they are shared with any other post type. So, we must create a new filter, to look for <em>home-book.php</em> before falling to another template.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> theme_home_template<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$template</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post_type'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$new_template</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'home-'</span><span style="color: #339933;">.</span>get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post_type'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">'.php'</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$templates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #000088;">$new_template</span><span style="color: #339933;">,</span>
			<span style="color: #000088;">$template</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> locate_template<span style="color: #009900;">&#40;</span><span style="color: #000088;">$templates</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$template</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'home_template'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'theme_home_template'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Add this code again to theme&#8217;s functions.php. Then, create the <em>home-book.php</em> and customize it to the way you need. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h2>Conclusion</h2>
<p>That&#8217;s it. So to conclude it, we first register the post type as usual, then adding a rewrite rule for the sake of cleaner URL. Finally, we create a new <em>home-{post_type}.php</em> file to serve as the template of the landing page. Now that we have the template, we can customize it to every way we wanted.</p>
<p>A different approach can be done though, such as creating a page and a custom template that query to the post type. I have used this on some of my clients. The downside is we will create a blank page, not that it&#8217;s bad, but it&#8217;s always good to not leave any <em>junk</em> in the administration back-end &#8211; so user can&#8217;t remove it accidentally and break the site.</p>
<p>This approach, although a little tricky, is a good way to make everything work seamlessly without any configuration needed. <img src='http://jeffri.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2011/05/customize-custom-post-type-landing-page-with-clean-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple jQuery and CSS3 Slideshow Tabs</title>
		<link>http://jeffri.net/2010/10/simple-jquery-and-css3-slideshow-tabs/</link>
		<comments>http://jeffri.net/2010/10/simple-jquery-and-css3-slideshow-tabs/#comments</comments>
		<pubDate>Fri, 15 Oct 2010 01:00:21 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Designing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[slideshow]]></category>
		<category><![CDATA[tab]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=688</guid>
		<description><![CDATA[A slideshow and tabs is pretty common nowadays in any modern website. There is a ton of way to do this and many free and paid tools available for this feature. This make it very easy to create and it will always amaze people who see it. Basically, we will need at least Javascript to [...]]]></description>
			<content:encoded><![CDATA[<p>A slideshow and tabs is pretty common nowadays in any modern website. There is a ton of way to do this and many free and paid tools available for this feature. This make it very easy to create and it will always amaze people who see it. Basically, we will need at least Javascript to do this, CSS to style the slideshow and tabs as we wanted, and then finally the special HTML markup which make it possible. There is also a slideshow which make use of Flash.</p>
<p>The solution I tried to made right now is a very simple and basic one. It make use of simple jQuery to catch the click event and switching tabs, as well as the slideshow, the CSS to style it as much as we wanted and a simple HTML markup in one <em>unordered list</em>.</p>
<p><img src="http://jeffri.net/wp-content/uploads/slideshow-tabs.jpg" alt="" title="slideshow-tabs" width="540" height="300" class="alignnone size-full wp-image-689" /></p>
<p><span id="more-688"></span></p>
<h3>The Markup</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>ul <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tabs tabs-1&quot;</span><span style="color: #339933;">&gt;</span> 
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span> 
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-select&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#tab1&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> 
		<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-content&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab1&quot;</span><span style="color: #339933;">&gt;</span> 
			<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-images/tab1.jpg&quot;</span> alt<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Tab 1&quot;</span> <span style="color: #339933;">/&gt;</span> 
		<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span> 
	<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span> 
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span> 
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-select&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#tab2&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> 
		<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-content&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab2&quot;</span><span style="color: #339933;">&gt;</span> 
			<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-images/tab2.jpg&quot;</span> alt<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Tab 2&quot;</span> <span style="color: #339933;">/&gt;</span> 
		<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span> 
	<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span> 
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span> 
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-select&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#tab3&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> 
		<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-content&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab3&quot;</span><span style="color: #339933;">&gt;</span> 
			<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-images/tab3.jpg&quot;</span> alt<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Tab 3&quot;</span> <span style="color: #339933;">/&gt;</span> 
		<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span> 
	<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span> 
	<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;</span> 
		<span style="color: #339933;">&lt;</span>a <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-select&quot;</span> href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;#tab4&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">4</span><span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;</span> 
		<span style="color: #339933;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-content&quot;</span> id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab4&quot;</span><span style="color: #339933;">&gt;</span> 
			<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;tab-images/tab4.jpg&quot;</span> alt<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Tab 4&quot;</span> <span style="color: #339933;">/&gt;</span> 
		<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span> 
	<span style="color: #339933;">&lt;/</span>li<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>The markups is as simple as it can get. It consists of an <em>un-ordered list</em>, with each list containing one <em>anchor</em> for navigation and one <em>div</em> for content.</p>
<h3>The Javascripts</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span> src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span> 
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span> 
	jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>ready<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.tabs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #990000;">each</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>this<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>find<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'li:first'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>addClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// set the first tab to display</span>
			repeat_slideshow<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>this<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		$<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.tabs li .tab-select'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>click<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			$<span style="color: #009900;">&#40;</span>this<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>closest<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.tabs'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>find<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>not<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>this<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>parent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>removeClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// hide all tabs except for the current</span>
			$<span style="color: #009900;">&#40;</span>this<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>parent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>addClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// set the current tab to display</span>
			reset_slideshow<span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>this<span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>closest<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'.tabs'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">function</span> slideshow<span style="color: #009900;">&#40;</span>slide<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">var</span> index <span style="color: #339933;">=</span> slide<span style="color: #339933;">.</span>find<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'li.current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>index<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000000; font-weight: bold;">var</span> total <span style="color: #339933;">=</span> slide<span style="color: #339933;">.</span>find<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>length<span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> index<span style="color: #339933;">+</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">&gt;=</span> total <span style="color: #009900;">&#41;</span>
				<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #990000;">next</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">else</span>
				<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #990000;">next</span> <span style="color: #339933;">=</span> index <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
			slide<span style="color: #339933;">.</span>find<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'li.current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>removeClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			slide<span style="color: #339933;">.</span>find<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'li'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>eq<span style="color: #009900;">&#40;</span><span style="color: #990000;">next</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span>addClass<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'current'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">function</span> repeat_slideshow<span style="color: #009900;">&#40;</span>slide<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			slide<span style="color: #339933;">.</span>data<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'slideshow'</span><span style="color: #339933;">,</span> setTimeout<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					slideshow<span style="color: #009900;">&#40;</span>slide<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					repeat_slideshow<span style="color: #009900;">&#40;</span>slide<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5000</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">function</span> stop_slideshow<span style="color: #009900;">&#40;</span>slide<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			clearTimeout<span style="color: #009900;">&#40;</span>slide<span style="color: #339933;">.</span>data<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'slideshow'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">function</span> reset_slideshow<span style="color: #009900;">&#40;</span>slide<span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			stop_slideshow<span style="color: #009900;">&#40;</span>slide<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			repeat_slideshow<span style="color: #009900;">&#40;</span>slide<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></pre></td></tr></table></div>

<p>This code is pretty much simple, it only toggles tab by changing <em>class</em> properties. It relies to the CSS to deliver the actual tabs toggling.</p>
<h3>The CSS</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>style type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;text/css&quot;</span><span style="color: #339933;">&gt;</span> 
	<span style="color: #339933;">.</span>tabs
	<span style="color: #009900;">&#123;</span>
		position<span style="color: #339933;">:</span> relative<span style="color: #339933;">;</span>
		height<span style="color: #339933;">:</span> 430px<span style="color: #339933;">;</span>
		width<span style="color: #339933;">:</span> 640px<span style="color: #339933;">;</span>
		list<span style="color: #339933;">-</span>style<span style="color: #339933;">-</span>type<span style="color: #339933;">:</span> none<span style="color: #339933;">;</span>
		margin<span style="color: #339933;">:</span> 10px <span style="color: #cc66cc;">0</span> 40px<span style="color: #339933;">;</span>
		padding<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		overflow<span style="color: #339933;">:</span> hidden<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">.</span>tabs<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span>
	<span style="color: #009900;">&#123;</span>
		width<span style="color: #339933;">:</span> 380px<span style="color: #339933;">;</span>
		padding<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span> 260px<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">.</span>tabs li
	<span style="color: #009900;">&#123;</span>
		float<span style="color: #339933;">:</span> left<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">.</span>tabs li <span style="color: #339933;">.</span>tab<span style="color: #339933;">-</span>select
	<span style="color: #009900;">&#123;</span>
		display<span style="color: #339933;">:</span> block<span style="color: #339933;">;</span>
		float<span style="color: #339933;">:</span> left<span style="color: #339933;">;</span>
		margin<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span> 5px <span style="color: #cc66cc;">0</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		padding<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span> 10px<span style="color: #339933;">;</span>
		background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#e6e6e6;
</span>		color<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#666;
</span>		text<span style="color: #339933;">-</span>decoration<span style="color: #339933;">:</span> none<span style="color: #339933;">;</span>
		cursor<span style="color: #339933;">:</span> pointer<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">.</span>tabs<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> li <span style="color: #339933;">.</span>tab<span style="color: #339933;">-</span>select
	<span style="color: #009900;">&#123;</span>
		height<span style="color: #339933;">:</span> 20px<span style="color: #339933;">;</span>
		line<span style="color: #339933;">-</span>height<span style="color: #339933;">:</span> 20px<span style="color: #339933;">;</span>
		<span style="color: #339933;">-</span>moz<span style="color: #339933;">-</span>border<span style="color: #339933;">-</span>radius<span style="color: #339933;">:</span> 10px<span style="color: #339933;">;</span>
		border<span style="color: #339933;">-</span>radius<span style="color: #339933;">:</span> 10px<span style="color: #339933;">;</span>
		font<span style="color: #339933;">-</span>size<span style="color: #339933;">:</span> 10px<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">.</span>tabs li <span style="color: #339933;">.</span>tab<span style="color: #339933;">-</span>content
	<span style="color: #009900;">&#123;</span>
		position<span style="color: #339933;">:</span> absolute<span style="color: #339933;">;</span>
		top<span style="color: #339933;">:</span> 30px<span style="color: #339933;">;</span>
		left<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		height<span style="color: #339933;">:</span> 400px<span style="color: #339933;">;</span>
		width<span style="color: #339933;">:</span> 640px<span style="color: #339933;">;</span>
		opacity<span style="color: #339933;">:</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
		<span style="color: #339933;">-</span>moz<span style="color: #339933;">-</span>transition<span style="color: #339933;">:</span> opacity 0<span style="color: #339933;">.</span>5s linear<span style="color: #339933;">;</span>
		<span style="color: #339933;">-</span>o<span style="color: #339933;">-</span>transition<span style="color: #339933;">:</span> opacity 0<span style="color: #339933;">.</span>5s linear<span style="color: #339933;">;</span>
		<span style="color: #339933;">-</span>webkit<span style="color: #339933;">-</span>transition<span style="color: #339933;">:</span> opacity 0<span style="color: #339933;">.</span>5s linear<span style="color: #339933;">;</span>
		transition<span style="color: #339933;">:</span> opacity 0<span style="color: #339933;">.</span>5s linear<span style="color: #339933;">;</span>
		z<span style="color: #339933;">-</span>index<span style="color: #339933;">:</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">.</span>tabs li <span style="color: #339933;">.</span>tab<span style="color: #339933;">-</span>select<span style="color: #339933;">:</span>hover
	<span style="color: #009900;">&#123;</span>
		background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#ccc;
</span>		color<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#666;
</span>	<span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">.</span>tabs li<span style="color: #339933;">.</span><span style="color: #990000;">current</span> <span style="color: #339933;">.</span>tab<span style="color: #339933;">-</span>select
	<span style="color: #009900;">&#123;</span>
		background<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#666;
</span>		color<span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">#fff;
</span>		text<span style="color: #339933;">-</span>shadow<span style="color: #339933;">:</span> 1px 1px <span style="color: #666666; font-style: italic;">#000;
</span>	<span style="color: #009900;">&#125;</span>
	<span style="color: #339933;">.</span>tabs li<span style="color: #339933;">.</span><span style="color: #990000;">current</span> <span style="color: #339933;">.</span>tab<span style="color: #339933;">-</span>content
	<span style="color: #009900;">&#123;</span>
		opacity<span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		z<span style="color: #339933;">-</span>index<span style="color: #339933;">:</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>style<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>The CSS define how the tabs will looks. It is mainly used the <em>position</em> properties to maintain the position of the tab content and leave the navigation to static position (either floated or none). Notice the <em>tabs-1</em> class? That is to demonstrate that even with this approach, we can customize the tab to different looks with exactly same markup structure.</p>
<p>To further explain how it works, first the main <em>.tabs</em> class is considered the area which the content will be placed, we set the <em>position</em> to <em>relative</em> and make sure it had enough <em>height</em> to accomodate the content. Then each <em>list</em> is <em>floated</em> to left, as well as the <em>.tab-select</em> <em>anchor</em> for navigation (in later example, we can also use <em>div</em>).</p>
<p>The content part of each list is set to <em>absolute position</em>. That means, they will overlap each other and make them in always same position. We then used <em>opacity</em> to hide and displaying the content with addition of the CSS3 <em>transform</em> to provide fading animation. Finally, we bring the selected content to the front by applying a bigger <em>z-index</em> value.</p>
<p>Note that, for fading animation, we used the <em>opacity</em> property. If you wanted to use another animation, you need to change it. For no animation, just use <em>display: none</em> and <em>display: block</em>.</p>
<h3>Why this approach?</h3>
<p>What make this approach better than any others tabs and slideshow?</p>
<p>Well, it might not better. It just that, it is simple and clean enough, at least for me.</p>
<h3>Only one <em>unordered list</em> markup needed</h3>
<p>Often, I find that a tab are made from two group of <em>lists</em> (when I said <em>lists</em> it can be a group of <em>div</em>, <em>li</em>, or anything). One is for the tab navigation and the other is for the content. Nothing is wrong either way, but I find it easier to create if it is only made on one <em>list</em>. This also apply when we create the markup on the fly using server script such as PHP. If we have two group of <em>lists</em>, this means we need to loop the variable twice, while using one <em>list</em> will only need one loop.</p>
<p>Using one list will also make the HTML cleaner and for those who browsing with text browser, it will degrade gracefully.</p>
<h3>Lighter, no Javascript effect</h3>
<p>I don&#8217;t know if this will be considered a good thing or not, but I don&#8217;t use any animation using Javascript nor jQuery effect. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Instead, I rely on CSS 3 transition for the <em>fade in</em> effect. This, obviously, is not yet a good practice. It is safe to use CSS 3 feature right now, but the lack of browser support make it not yet practical. By far, only new Webkit browser (Safari and Chrome), Opera 10.6+ and Firefox 4 support CSS transition.</p>
<p>Again, why CSS 3? Well, we know that the animation and transition in CSS 3 is not yet matured and experimental for now. It even lagged when we are doing some heavy animation. But when it is matured enough and become a standard, it will be better than any Javascript animation as it will build natively to the browser.</p>
<h3>Support multiple slideshow tabs</h3>
<p>Well, it make use of two jQuery features that was there but seems to rarely used. They are <a href="http://api.jquery.com/index/">jQuery.index</a> and <a href="http://api.jquery.com/data/">jQuery.data</a>. We used jQuery.index to see the current index from the <em>list</em> group to find the next tab to slide. It is more simple than storing a variable and keep it updated when the slide change. Then, the jQuery.data is used to store the setTimeout variable. Why using a jQuery.data and not a variable? jQuery.data allow us to attach data to a DOM element, this way, we can attach each setTimeout variable to the parent group and make it separated to any other slideshow in the same page. So, it support multiple slideshow in the same page without interrupting each other in just a few lines of code. Nice!</p>
<h3>The Downside</h3>
<p>After all this goodness, obviously, there is still a downside to be considered that it might not suit your need. First of all, the content height is fixed. This is because the use of <em>absolute position</em> for the content. Second, well, the lack CSS3 support on some web browser.</p>
<h3>The Demo</h3>
<p>Finally, a demo to show you how it actually works.</p>
<p><a href="http://www.keaglez.com/projects/slideshowtabs">Check out the demo here.</a></p>
<p>There is three styles of the slideshow tabs in the demo, so you can see that it is possible to style it in many way to suit your design.</p>
<p>The code is released under GNU General Public License. So feel free to use and modify it in any way you need.</p>
<h3>Browser support?</h3>
<p>Well, not much browser support this yet. It degrade nicely though. If you open it in older browser that didn&#8217;t have CSS 3, it still working all fine except the CSS 3 goodies. So we can say that the CSS 3 goodies is an extra for dude who love using updated browser. <img src='http://jeffri.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>How about Internet Explorer 6? Well, it is not working right as far as I can see. But I think there will be a workaround to solve the IE 6 problem though, but I just don&#8217;t have a time to spend on this <em>stupid</em> outdated browser.</p>
<h3>Final?</h3>
<p>Thank you and hope that useful for you. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/10/simple-jquery-and-css3-slideshow-tabs/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The Basics to Write a Secure PHP Web Application</title>
		<link>http://jeffri.net/2010/09/the-basics-to-write-a-secure-php-web-application/</link>
		<comments>http://jeffri.net/2010/09/the-basics-to-write-a-secure-php-web-application/#comments</comments>
		<pubDate>Fri, 03 Sep 2010 07:00:33 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=654</guid>
		<description><![CDATA[Securing your application is the most important things when building an application. This is the basic that every programmer should follow, it&#8217;s a must. However, sometime programmer might forgot the basic, and the more complex your application is, the harder it is to maintain and looking for security holes. While securing your application doesn&#8217;t mean [...]]]></description>
			<content:encoded><![CDATA[<p>Securing your application is the most important things when building an application. This is the basic that every programmer should follow, it&#8217;s a must. However, sometime programmer might forgot the basic, and the more complex your application is, the harder it is to maintain and looking for security holes. While securing your application doesn&#8217;t mean that you will be totally safe from a hack, since there is many factor of why a web can be hacked, but reduce the possibility is always a good practice.</p>
<p><img src="http://jeffri.net/wp-content/uploads/hack-target.jpg" alt="" title="hack-target" width="540" height="200" class="alignnone size-full wp-image-682" /></p>
<p>This article will give you walkthrough on the basics of creating a secured PHP application. I will give some step by step to filtering input, keep your code up to date and standard. I&#8217;ll also give you some good practice I always do.</p>
<p><span id="more-654"></span></p>
<h2>Filtering Input</h2>
<p>A web application is operated by retrieving inputs from user and then decide what to do based of the inputs. Filtering your input is a must to prevent any unwanted code to get executed.</p>
<h3>Making sure the input data type</h3>
<p>This is the most basic you should take care of for filtering. For example if you are retrieving integer type, you will need to make sure you always get an integer. Here is some example of how to make sure you get an integer.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'number'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You must input a number&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'number'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'number'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You must input a number&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Mostly, input retrieved will be always have a string data type, so sometime, you will need to convert it to make sure you get a correct data type.</p>
<h3>Validate the string</h3>
<p>If you have a list of string you will be accepting, you will need to make sure you only retrieve this value. Here is some example you can do.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'command'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;edit&quot;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'command'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">&quot;add&quot;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown command&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'command'</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;add&quot;</span><span style="color: #339933;">:</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;edit&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #666666; font-style: italic;">// some code you will run</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown command&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(edit|add)$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'command'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Unknown command&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>I personally like the Regular Expression (<em>regex</em>) solution, since it is simple and need less work when you decide to accept a bunch of other strings. Also when you are accepting a pattern of string, it&#8217;s always good to use <em>regex</em>. Here is some example of basic email and URL filtering.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[A-Za-z0-9_.-]@[A-Za-z0-9_.-]\.[A-Za-z0-9]{2,4}$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You entered invalid email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(http|https):\/\/[A-Za-z0-9_.-]\.[A-Za-z0-9]{2,4}$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;You entered invalid url&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Remember that while PHP have two function to execute <em>regex</em>, <a href="http://www.php.net/manual/en/book.regex.php">POSIX Extended</a> and <a href="http://php.net/manual/en/book.pcre.php">PCRE</a>, you must always use the PCRE solution as the other one has deprecated since PHP 5.3.0.</p>
<h3>Escape your string</h3>
<p>You should always escape your string before you insert it to your database. If your server have <em>magic quotes</em> turned on by default, you might be safe from this. But your code shouldn&#8217;t rely on server capabilities, your code should escape the string if the <em>magic quotes</em> is turned off. You also must check the <em>magic quotes</em> configuration to prevent you to escape your string twice.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">addslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>If you are using MySQL database, use <strong>mysql_real_escape_string</strong> instead.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Finally, if you don&#8217;t want to accept html string, you can strip it or convert it to html entities.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$no_html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'no_html'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this will strip all html tags</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$no_html</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'no_html'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this will convert all html tags to entities</span></pre></td></tr></table></div>

<h3>Wrap it out in a function</h3>
<p>Making your code easier to maintain is one way to secure your application. Wrap it out in a function is a good solution. All you need to do is making sure the retrieved input has filtered in this function. So if you find any vulnerabilities, the first you will look is the function. This should save your time looking for the security holes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> input_filter<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #000088;">$type</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$input</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">get_magic_quotes_gpc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$type</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'numeric'</span><span style="color: #339933;">:</span>
            <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'email'</span><span style="color: #339933;">:</span>
            <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^[A-Za-z0-9_.-]@[A-Za-z0-9_.-]\.[A-Za-z0-9]{2,4}$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'url'</span><span style="color: #339933;">:</span>
            <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/^(http|https):\/\/[A-Za-z0-9_.-]\.[A-Za-z0-9]{2,4}$/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> ? <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'html'</span><span style="color: #339933;">:</span>
            <span style="color: #000088;">$filtered_input</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filtered_input</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And the example to use this function.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$number</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'number'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'numeric'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will return 0 if it is not numeric</span>
<span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will return empty string if invalid</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will return empty string if invalid</span>
<span style="color: #000088;">$no_html</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'no_html'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will return all html converted to entities</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> input_filter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'string'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// it will simply escape the string</span></pre></td></tr></table></div>

<p>Depends to your preference, you can wrap it to a numbers of separated functions or to a class. But I think wrap it in a function is not bad either if you are creating a simple application and only need a small numbers of filtering.</p>
<h2>Keeping Your Code Up To Date and Standards</h2>
<p>PHP keep growing. If you are writing your application in PHP 4, most likely, it will still working in PHP 5. However, some of them might not work anymore in PHP 6. Some example is the use of deprecated function. The function that deprecated in PHP 5 will still working for backward compability, but using this function is not safe anymore as they can be removed anytime in future.</p>
<p>Make sure you are up to date to the new PHP version. Always look the PHP manual for each function you use. Some time you can find a notice of how to correctly use the function. The user contributed notes is also helpful if you are looking for a solution related to that function.</p>
<h3>Do not rely on register globals</h3>
<p><em>Register globals</em> has been turned off by default and deprecated since PHP 5.3.0. Incorrect use of <em>register globals</em> will make your code vulnerable. Always use <a href="http://www.php.net/manual/en/language.variables.superglobals.php"><em>superglobal</em></a> variable instead.</p>
<p>However, it&#8217;s also a good practice to not use <strong>$_REQUEST</strong> variable if you don&#8217;t need to. This variable contains the content of <strong>$_GET</strong>, <strong>$_POST</strong> and <strong>$_COOKIE</strong>. Using this variable will prevent you to know where the variable came from, it&#8217;s always good to know where your variable came from. For example you would like some inputs came from post or cookie, but using <strong>$_REQUEST</strong>, the user can use get and it still get executed.</p>
<h3>Always define a variable before use it</h3>
<p>You must always define your variable before you use it. In some server, <em>register globals</em> might be turned on, and failing to do so might result in vulnerable codes.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> validate_user<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$safe</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$safe</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #666666; font-style: italic;">// some sensitive code</span></pre></td></tr></table></div>

<p>The above code is vulnerable if <em>register globals</em> is turned on. If user run the script by calling <em>user.php?safe=1</em>, the user will always get validated. You can do this instead.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$safe</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> validate_user<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$safe</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$safe</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #666666; font-style: italic;">// some sensitive code</span></pre></td></tr></table></div>

<h3>Use strict error reporting in development</h3>
<p>Checking each function you use to find a deprecated function is not fun. So in development environment you must show all PHP errors. You can do this by calling this function on top of your application.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This way, you will know if you are using deprecated function. You will also know if there is any undefined variable. This also includes the array in <em>superglobals</em> variable. Making sure to check undefined variable with <strong>isset</strong> before you use it is also a good practice.</p>
<h3>Turn off all error reporting in production</h3>
<p>If your website is ready now, you will need to turn off all error reporting. Showing your error to everyone will not be a good thing, one thing is you will reveal something to your visitor, such as file structure. For example, your main code might not be vulnerable, but some of included codes might be vulnerable if requested directly. Keeping your file structure a secret might give you a better security.</p>
<p>One thing to note, you also shouldn&#8217;t show <strong>mysql_error</strong> value to your visitor. If somehow you don&#8217;t filter the inputs properly and you have a code like this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM some_table WHERE some_field='<span style="color: #006699; font-weight: bold;">$field_value</span>'&quot;</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code above might look familiar as new programmer tends to use it to check for query error. However, if the <strong>$field_value</strong> variable is not filtered properly and vulnerable to injection, you will show the visitor the error and at the same time, you will reveal that your code is vulnerable. So instead of showing the error, you must log the error instead, this way only you can see the error.</p>
<h2>Conclusion</h2>
<p>So this is the basic you can use to secure your web application properly. Meeting this standard will make your application less vulnerable. Again, it will not guarantee that your application will be perfectly safe, as there is many other factors such as using an outdated web server or using some vulnerable library.</p>
<p>Hopefully, this will be useful for you. Do you also have a technique to write a secure web application? Please share in comment.</p>
<p>Final words, thank you to read this. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/09/the-basics-to-write-a-secure-php-web-application/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Best practice to clearing default text with jQuery</title>
		<link>http://jeffri.net/2010/08/best-practice-to-clearing-default-text-with-jquery/</link>
		<comments>http://jeffri.net/2010/08/best-practice-to-clearing-default-text-with-jquery/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 02:45:00 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=620</guid>
		<description><![CDATA[Having default text that we later removed when retrieving user input is common in today&#8217;s website. Many web developer use this to enhance usability of the website, so user know what to type in what field. There is a lot of way to do this, using both focus and blur event. For example, this is [...]]]></description>
			<content:encoded><![CDATA[<p>Having default text that we later removed when retrieving user input is common in today&#8217;s website. Many web developer use this to enhance usability of the website, so user know what to type in what field. There is a lot of way to do this, using both <em>focus</em> and <em>blur</em> event. For example, this is the shortest and easiest way that is commonly used:</p>
<input type="text" id="example-field" name="example-field" value="Your name" onfocus="if (this.value=='Your name') this.value=''" onblur="if (this.value=='') this.value='Your name'" />
&nbsp;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;example-field&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;example-field&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Your name&quot;</span> <span style="color: #000066;">onfocus</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value=='Your name') this.value=''&quot;</span> <span style="color: #000066;">onblur</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;if (this.value=='') this.value='Your name'&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></td></tr></table></div>

<p>However, this approach will be a pain when you are managing a lot of input fields. You will need to type the default text three times, which can lead to a trivial typo. Also, since we check againts the default value, we will not be able to accept this value. Here is the solution, by using the simplicity and power of jQuery, this is a clean and a better way to do this.</p>
<p><span id="more-620"></span></p>
<input type="text" id="example-field2" name="example-field2" value="Your name" />
<p><script type="text/javascript">
jQuery(document).ready(function($){
	$('#example-field2').focus(example_clean_field).blur(example_reset_field).each(example_populate_field);
	function example_populate_field()
	{
		if ( ! $.data(this, 'default') )
			$.data(this, 'default', $(this).val());	
	}
	function example_clean_field()
	{
		if ( ! $(this).hasClass('hasfocus') )
			$(this).addClass('hasfocus').val('');
	}
	function example_reset_field()
	{
		if ( $(this).val() == '' )
			$(this).removeClass('hasfocus').val($.data(this, 'default'));
	}
});
</script></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">&lt;input type=&quot;text&quot; id=&quot;example-field2&quot; name=&quot;example-field2&quot; value=&quot;Your name&quot; /&gt;
&nbsp;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
jQuery<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#example-field2'</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span>example_clean_field<span style="color: #009900;">&#41;</span>.<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span>example_reset_field<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>example_populate_field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #003366; font-weight: bold;">function</span> example_populate_field<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> $.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'default'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
			$.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'default'</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">function</span> example_clean_field<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hasfocus'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hasfocus'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #003366; font-weight: bold;">function</span> example_reset_field<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span> <span style="color: #009900;">&#41;</span>
			$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'hasfocus'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'default'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>You can see that we define three functions here, one for <em>focus</em> event, one for <em>blur</em> event and the other one to populate the default value. I&#8217;ll briefly explain how they work in short.</p>
<p>We add a callback to each fields with function <strong>example_populate_field</strong>, this function will make use of <a href="http://api.jquery.com/jQuery.data/">jQuery.data</a>, which allow us to attach any data to DOM element. We will iterate to each fields, and store the data in &#8220;default&#8221; key.</p>
<p>Next, we have a <em>focus</em> callback, <strong>example_clean_field</strong>, this function check whether the field has &#8220;hasfocus&#8221; class, if it doesn&#8217;t then we add the class and clear the field. Note that we use the &#8220;hasfocus&#8221; class to determine the state of the field, whether it has been cleared or not.</p>
<p>Lastly, we have <strong>example_reset_field</strong> that we use for <em>blur</em> event. This function will check the current value, when it&#8217;s empty, we remove the &#8220;hasfocus&#8221; class and set the value back to the default.</p>
<p>The usage as shown above is very simple, you just need to select any input or textarea element, and it will work. For example, using this selector below will make this work to all text input and textarea element.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input[type=text], textarea'</span><span style="color: #009900;">&#41;</span> .<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span>example_clean_field<span style="color: #009900;">&#41;</span> .<span style="color: #000066;">blur</span><span style="color: #009900;">&#40;</span>example_reset_field<span style="color: #009900;">&#41;</span> .<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>example_populate_field<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s all. It&#8217;s simple, clean and it looks like the most elegant solution I can find. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope that will be useful for you as well. Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/08/best-practice-to-clearing-default-text-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Filter get_terms to return only terms with published post</title>
		<link>http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/</link>
		<comments>http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 22:37:03 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=609</guid>
		<description><![CDATA[So you are using custom taxonomy. You add terms to the taxonomy, and add the terms to your posts. Then, you use get_terms and list all your terms. Everything works fine, terms that doesn&#8217;t have any posts will not be returned if you set hide_empty argument to true. Now you move some of your posts [...]]]></description>
			<content:encoded><![CDATA[<p>So you are using custom taxonomy. You add terms to the taxonomy, and add the terms to your posts. Then, you use <strong>get_terms</strong> and list all your terms. Everything works fine, terms that doesn&#8217;t have any posts will not be returned if you set <em>hide_empty</em> argument to true. Now you move some of your posts to draft and you get a problem. Terms that doesn&#8217;t have any published posts still returned. This lead to 404 error when you click on the link of this term. It doesn&#8217;t look good now, we need to remove term that doesn&#8217;t have any published post.</p>
<p>So how to do that? The answer is by using WordPress filter. This piece of code below will solve the problem.</p>
<p><span id="more-609"></span></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_terms_filter<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$terms</span><span style="color: #339933;">,</span> <span style="color: #000088;">$taxonomies</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$taxonomy</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$taxonomies</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$terms</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$terms</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$filtered_terms</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$terms</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$term</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$wpdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_var</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT COUNT(*) FROM <span style="color: #006699; font-weight: bold;">$wpdb-&gt;posts</span> p JOIN <span style="color: #006699; font-weight: bold;">$wpdb-&gt;term_relationships</span> rl ON p.ID = rl.object_id WHERE rl.term_taxonomy_id = <span style="color: #006699; font-weight: bold;">$term-&gt;term_id</span> AND p.post_status = 'publish' LIMIT 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #000088;">$filtered_terms</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$term</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$filtered_terms</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'get_terms'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'get_terms_filter'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The code work like this. We add a filter to the <strong>get_terms</strong> hook. This hook is called before the terms is returned, so it is perfect to place the filter here. Next, we check each returned terms and see if they have any published post, by using our own query. Why didn&#8217;t I use the WordPress WP_Query instead? The answer is I can&#8217;t get it to work, beside, it builds a pretty complicated query that might increase your WordPress site load time if it is used too much. Using our own query, we make sure it only use the query that we need. If our query returned that the term has a published post, we add this term to our new array which we use to store the filtered terms. Finally, we return the filtered terms.</p>
<p>Now, when you are using <strong>get_terms</strong>, you will filter out all terms that doesn&#8217;t have any published post. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Hope that helps. I have a hard time to explain the situation, so let me know if it doesn&#8217;t clear.</p>
<p>Thank you.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/08/filter-get_terms-to-return-only-terms-with-published-post/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>CSS Captcha 0.2 beta</title>
		<link>http://jeffri.net/2010/08/css-captcha-0-2-beta/</link>
		<comments>http://jeffri.net/2010/08/css-captcha-0-2-beta/#comments</comments>
		<pubDate>Sun, 08 Aug 2010 19:48:20 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[captcha]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS Captcha]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=597</guid>
		<description><![CDATA[Here I update the CSS Captcha, it is now on 0.2 beta! Thank you for the response of the previous version. While this can work nicely on major new browser, the compatibility with older browser is still minimum. Some mobile browser also haven&#8217;t implement some of CSS3 functionality, such as Opera Mini and Opera Mobile. [...]]]></description>
			<content:encoded><![CDATA[<p>Here I update the CSS Captcha, it is now on 0.2 beta! Thank you for the response of the previous version. While this can work nicely on major new browser, the compatibility with older browser is still minimum. Some mobile browser also haven&#8217;t implement some of CSS3 functionality, such as Opera Mini and Opera Mobile. So this is still experimental and shouldn&#8217;t be used yet on production website.</p>
<p><img src="http://jeffri.net/wp-content/uploads/csscaptcha0.2.jpg" alt="" title="csscaptcha0.2" width="540" height="189" class="alignnone size-full wp-image-598" /></p>
<p><a href="http://jeffri.net/2010/06/css-captcha-0-1-beta/">Please read the older version information here</a>.</p>
<p>This update is merely a security update. The inline style is randomized now, which make it more difficult for bot to read. The order of the character is also randomized. Sure, it still doesn&#8217;t prevent the bot to read or make it impossible to read at all, but it should give more difficulty on creating such bot. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Still, no support for IE browser at all. <img src='http://jeffri.net/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Change log 0.2b:<br />
- Automatically randomize inline style<br />
- Automatically randomize character order</p>
<p><a href="http://csscaptcha.keaglez.com">Demonstration</a><br />
<a href="http://csscaptcha.keaglez.com/csscaptcha0.2b.zip">Download</a></p>
<p>Enjoy! Any comment is appreciated.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/08/css-captcha-0-2-beta/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Some useful tips for WordPress custom post type and taxonomy</title>
		<link>http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/</link>
		<comments>http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/#comments</comments>
		<pubDate>Mon, 19 Jul 2010 23:28:08 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=569</guid>
		<description><![CDATA[Custom post type is one of the most powerful WordPress feature. It allows us to create just any content we need, combined with custom taxonomy, the possibility is just endless. WordPress have allowed to have custom post type and taxonomy for some while, but with WordPress 3, everything is far more easier. We have register_post_type [...]]]></description>
			<content:encoded><![CDATA[<p>Custom post type is one of the most powerful WordPress feature. It allows us to create just any content we need, combined with custom taxonomy, the possibility is just endless. WordPress have allowed to have custom post type and taxonomy for some while, but with WordPress 3, everything is far more easier. We have <a href="http://codex.wordpress.org/Function_Reference/register_post_type">register_post_type</a> and <a href="http://codex.wordpress.org/Function_Reference/register_taxonomy">register_taxonomy</a> created specifically for this purpose.</p>
<p>While this two combined to be a wonderful addition for your WordPress blog, there is still a lot of things that is not yet documented. Here is a few useful tips I collected when working with custom post type and taxonomy.</p>
<p><span id="more-569"></span></p>
<h2>Add taxonomy to more than one post type</h2>
<p>There is few ways to doing this, depend to your code. If you register the post type first, then when register taxonomy, you&#8217;ll be able to include it to any post types you want.</p>
<p>Example (taken from codex).</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  register_taxonomy<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'magazine'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'post'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'hierarchical'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'labels'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$labels</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'show_ui'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'query_var'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'rewrite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'slug'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'genre'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>See the line 1 above, the second parameter for register_taxonomy function is the list of post types you can include. It accept both string and array, as you can expect, we use array when we want to include many post type, and use string when you only need one. The same thing is applied if you register the taxonomy first, you can assign any taxonomy you like when register the post type.</p>
<p>Example (taken from codex)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'labels'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$labels</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'public'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'publicly_queryable'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'show_ui'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span> 
    <span style="color: #0000ff;">'query_var'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'rewrite'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'capability_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'hierarchical'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'menu_position'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'taxonomies'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'grade'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'category'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'supports'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'editor'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'author'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'thumbnail'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'excerpt'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'comments'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  register_post_type<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As you can see, the <em>taxonomies</em> parameter can be used to assign any taxonomy you like to the post type you just registered. Again, it accept both array and string.</p>
<p>But what if you want to add taxonomy to certain post type later, after they are defined? WordPress have a function to do this, it is <a href="http://codex.wordpress.org/Function_Reference/register_taxonomy_for_object_type">register_taxonomy_for_object_type</a>. Take a look on the example below.</p>
<p>Example</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">register_taxonomy_for_object_type<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'book'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Handy, huh?</p>
<h2>Querying more than one post type</h2>
<p>Let&#8217;s say, for example, you have defined post type for book, comic and magazine. Then, somewhere in your web page, you want to show the latest list of all of these post types together. As you might know already, custom post type is not included in normal WordPress loop, so you&#8217;ll need to add your own query and loop, whether by using query_posts, get_posts or by creating a new instance of WP_Query object.</p>
<p>Example</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'comic'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'magazine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'posts_per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> the_post<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// and so on</span>
<span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>See the line 2. You can add any post type you need in the <em>post_type</em> parameter. As always, it also accept both array and string.</p>
<h2>Querying the custom taxonomy</h2>
<p>You know how to querying multiple post type now, but how if you want to filter it by taxonomy? While post category and tags offer many ways to filter it, the custom taxonomy is still limited. The only way I can find right now is to filter the taxonomy by the slug.</p>
<p>Example</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'book'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'comic'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'magazine'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'taxonomy'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'genre'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'term'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'science'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'fiction'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'posts_per_page'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">:</span> the_post<span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// and so on</span>
<span style="color: #b1b100;">endwhile</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>From the example above, I filtered the result by including only science and fiction genre from all three post types we defined. Unfortunately, I haven&#8217;t find any way to filter it by multiple taxonomies at once.</p>
<h2>The custom post type feed</h2>
<p>Custom post type also have its own feed. Depend to your permalink structure, you can find it by either</p>
<blockquote><p>http://www.yourwordpresswebsite.com/?feed=rss2&#038;post_type=book</p></blockquote>
<p>or</p>
<blockquote><p>http://www.yourwordpresswebsite.com/feed/?post_type=book</p></blockquote>
<p>Unfortunately, again, I can&#8217;t find any way to get the feed for multiple post type.</p>
<p>So that&#8217;s it for now. If you find any good tips, you can share it in comment. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/07/some-useful-tips-for-wordpress-custom-post-type-and-taxonomy/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Blog redesign</title>
		<link>http://jeffri.net/2010/07/blog-redesign/</link>
		<comments>http://jeffri.net/2010/07/blog-redesign/#comments</comments>
		<pubDate>Mon, 12 Jul 2010 01:46:22 +0000</pubDate>
		<dc:creator>keaglez</dc:creator>
				<category><![CDATA[Designing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[xhtml]]></category>

		<guid isPermaLink="false">http://jeffri.net/?p=553</guid>
		<description><![CDATA[Welcome to my new redesigned blog! It&#8217;s been a year and a half since the last time I changed the design. I have always wanted to change it, but I just always don&#8217;t have enough time. I know I&#8217;m not that good enough in designing, but at least, this is the best design I ever [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to my new redesigned blog! It&#8217;s been a year and a half since the last time I changed the design. I have always wanted to change it, but I just always don&#8217;t have enough time. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I know I&#8217;m not that good enough in designing, but at least, this is the best design I ever made until now!</p>
<p><img src="http://jeffri.net/wp-content/uploads/blog_design.jpg" alt="" title="blog_design" width="560" height="238" class="alignnone size-full wp-image-554" /></p>
<p>The idea behind this design is, to make a simple, usable layout and still looks beautiful. There is a lot of tutorial on the web that helped me when making this. I try to make the website small in size, so you&#8217;ll able to load it fast enough. There is also a cool trick on the background, which still make the markup small without much wrapping div over div to accomplish the effect. I also realize the importance on social networking nowadays, so I add links to some of my account. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><span id="more-553"></span></p>
<p>With CSS 3 coming, I won&#8217;t forget to add some CSS 3 elements. I add a little transition, on the sidebar links and the social network icon above. You won&#8217;t see it if you don&#8217;t use webkit browser though, so if you use Google Chrome 5 and Safari 5, then you are in. This transition have no importance at all, the purpose is just to enrich the user experience, but won&#8217;t lose any functionality without it. Another CSS 3 property I used is the <em>border-radius</em>. I used this on some input fields you can find. There is also some jQuery here, although I&#8217;m only using it on input fields now, it might become handy in future.</p>
<p>While I haven&#8217;t check this on all browser yet, but I&#8217;m sure it will work fine on all major browser, includes Internet Explorer 8, Mozilla Firefox 3.6, Opera 10, Google Chrome 5 and Safari 5. While I&#8217;m not supporting older Internet Explorer version, it still, on some basic level, works fine. I even can confirm that the IE 7 load almost everything fine, but it just not perfect yet. I have some issues on the logo and button, same as IE 6. This might get fixed soon. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>I wish I could write the process on making this design. I might be able write it, but I won&#8217;t promise that. In short, there is 2 steps at least. First is design it in PSD, the second is convert it to WordPress. I&#8217;m skipping the step to convert it to XHTML/CSS first though, well, that&#8217;s just how I work. <img src='http://jeffri.net/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>I hope you will have one or two minute to type down a comment to share your opinion, so I can get better. <img src='http://jeffri.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffri.net/2010/07/blog-redesign/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

