<?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>Think Vitamin &#187; Richard Shepherd</title>
	<atom:link href="http://thinkvitamin.com/author/richardshepherd/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkvitamin.com</link>
	<description>The Web Practitioner&#039;s Blog</description>
	<lastBuildDate>Wed, 08 Feb 2012 14:00:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>HTML5 Audio Unplugged</title>
		<link>http://thinkvitamin.com/code/html5-audio-unplugged/</link>
		<comments>http://thinkvitamin.com/code/html5-audio-unplugged/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 08:00:28 +0000</pubDate>
		<dc:creator>Richard Shepherd</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://thinkvitamin.com/?p=8912</guid>
		<description><![CDATA[Before there was video, there was audio. It started with MIDI files that would happily play when you loaded a website (often accompanied by a dancing-baby.gif), moved to .wav files that you could play in the browser, and has since moved on to mp3s and other compressed audio formats. Until recently, playing audio required a [...]]]></description>
			<content:encoded><![CDATA[<p>Before there was video, there was audio. It started with MIDI files that would happily play when you loaded a website (often accompanied by a dancing-baby.gif), moved to .wav files that you could play in the browser, and has since moved on to mp3s and other compressed audio formats.</p>
<p>Until recently, playing audio required a plugin of some sort. But thanks to the evil geniuses who put together the HTML5 spec we now have a fully fledged Audio element and JavaScript API to play with. With great power comes great responsibility. Apparently. And so before we go on and take a look at how to include (and control!) audio with HTML5 a word of caution.</p>
<p><span id="more-8912"></span></p>
<p>Most users, sane ones at least, don’t like audio on web pages unless it’s a) part of a video, b) a podcast or c) a music track they’re looking for. Background music, perish the thought, is considered a no-no. And with good reason. Our hapless user might be at work, and your page autoloading Greensleeves might alert his or her boss that they&#8217;re not crossing i’s and dotting t’s. Or perhaps our user is listening to Spotify &#8211; the last thing they want is your Garage Band mashup competing for their ears.</p>
<h3>Enrich Don’t Enrage</h3>
<p>I guess there are two main reasons you’d want to include audio on your site. The first is because it’s something the user wants to listen to; perhaps a podcast, or a preview of your latest song. The second is because the audio is part of the user experience. You may have added background music to a page on your site, or even sound effects hooked into buttons and other elements. Most Flash websites come with some form of audio enrichment and with the wonderful HTML5 spec, a pinch of CSS3 and a spoonful of JavaScript you can have a Flash-esque website in a standards compliant way.</p>
<p>Do not underestimate the power audio can have on a user experience. Great audio can lift a site into the echelons of brilliance and, done well, it is arguably more powerful than visual design in creating an emotional experience (did anyone ever see the Donnie Darko website?). Bad audio will ruin a website and you won’t see your visitors again. Ever.</p>
<h3>The Basics</h3>
<p>As with most of the HTML5 spec, including audio on your page is remarkably simple (and very similar to including video which you can <a href="http://thinkvitamin.com/code/getting-started-with-html5-video/">read about in a previous post</a>).</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;audio src=”mynewtrack.mp3”&gt;&lt;/audio&gt;</pre></div></div>

<p>Yup. That’s it. There are a number of additional attributes we can add to spice things up. For example:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;audio src=”mynewtrack.mp3” loop controls autoplay preload&gt;&lt;/audio&gt;</pre></div></div>

<p>You can add any or all of these attributes. ‘Loop’ loops the track. ‘Controls’ adds the browsers native controls (see below) and ‘autoplay’ is a one-way ticket to eternal damnation.</p>
<h3>Safari 5</h3>
<p><img class="alignnone size-full wp-image-8920" src="http://thinkvitamin.com/wp-content/uploads/2010/10/Safari.png" alt="" width="398" height="172" /></p>
<h3>Chrome 6</h3>
<p><img class="alignnone size-full wp-image-8916" src="http://thinkvitamin.com/wp-content/uploads/2010/10/Chrome6.png" alt="" width="440" height="174" /></p>
<h3>Opera 10.6</h3>
<p><img class="alignnone size-full wp-image-8919" src="http://thinkvitamin.com/wp-content/uploads/2010/10/Opera106.png" alt="" width="450" height="200" /></p>
<h3>Firefox 3.6</h3>
<p><img class="alignnone size-full wp-image-8917" src="http://thinkvitamin.com/wp-content/uploads/2010/10/FireFox36.png" alt="" width="387" height="132" /></p>
<h3>Internet Explorer 9</h3>
<p><img class="alignnone size-full wp-image-8918" src="http://thinkvitamin.com/wp-content/uploads/2010/10/IE9.png" alt="" width="508" height="157" /></p>
<p>Preload has three possible incarnations. ‘Preload’ will ask the the  browser to preload the track. Note that it <em>asks</em> rather than <em>demands</em>.  This is because the browser knows best; a mobile browser or a browser  on a slow internet connection may decide to ignore the request.  ‘Preload = none’ is self explanatory. ‘Preload = metadata’ tells the  browser to grab all the meta data from the file like the track name or  duration which may come in handy when constructing your audio player.  The track itself won’t start to download until the user activates one of  the controls.</p>
<h3>The Continued Curse of Codecs</h3>
<p>Of course it’s not really that simple. The problem with mp3s, as  ubiquitous as they’ve become, is that encoding and decoding them is  subject to patents and therefore costs. And so whilst some browser  vendors can afford to support this technology, others cannot.</p>
<p>Enter .ogg, sibling of .ogv which you may have come across when  encoding video files. The Vorbis codec which is used to create .ogg  files is not subject to any patents and therefore free to use &#8211; this is  probably why browsers like Opera and Firefox support it.</p>
<p>To make sure you cover all bases, it’s useful to have fallback audio  files which you can include like so:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;audio controls&gt;
  &lt;source src=&quot;mynewtrack.ogg&quot; type=&quot;audio/ogg&quot; /&gt;
  &lt;source src=&quot;mynewtrack.mp3&quot; type=&quot;audio/mpeg&quot; /&gt;
  &lt;!-- You could even put a Flash fallback here --&gt;
&lt;/audio&gt;</pre></div></div>

<p>Here the browser will try to load the .ogg file, and if that fails it  will try the .mp3 file. If all else fails you could include a Flash  movie with the audio too.</p>
<h3>Creating OGG and mp3 Files</h3>
<p>Fortunately there are a number of ways to encode your tracks which won’t cost you a penny or a cent. For both Mac and PC users there is <a href="http://audacity.sourceforge.net/" target="_blank">Audacity</a>,  a free digital audio editor which allows you to save files as both .mp3  and .ogg. You can download Audacity from <a href="http://audacity.sourceforge.net/" target="_blank">http://audacity.sourceforge.net/</a>.</p>
<p><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/Audacity.png" alt="" width="705" height="477" /></p>
<p>If you’re using a Mac I recommend Switch. You can download a free trial from <a href="http://www.apple.com/downloads/macosx/audio/switchaudioconverter.html" target="_blank">http://www.apple.com/downloads/macosx/audio/switchaudioconverter.html</a>.</p>
<p>Switch is a sound file converter which covers a multitude of file  formats. It’s clean interface is easy to use but make sure you’re  connected to the internet the first time you encode a file as it  downloads codecs on demand. Still, once you’ve downloaded the mp3 and  ogg codecs you’re good to go.</p>
<p><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/Switch.png" alt="" width="705" height="456" /></p>
<p>If you’re on a PC and Audacity isn&#8217;t for you, check out the <a href="http://www.vorbis.com/software/#windows" target="_blank">list  of software over at Vorbis.com</a>.</p>
<h3>Controlling the Controls</h3>
<p>What we&#8217;ve learnt so far is good enough to include audio on the page but we might want a little more control, and for that we need to dip into the JavaScript API.</p>
<p>I&#8217;ve created a basic music player which you can check out over at <a href="http://www.richardshepherd.com/tv/audio/">http://www.richardshepherd.com/tv/audio/</a> . You can view the source HTML and CSS to see how it’s put together, but here&#8217;s the basic HTML:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;player&quot;&gt;
&nbsp;
 &lt;input type=&quot;button&quot; value=&quot;Play&quot; id=&quot;play&quot; /&gt;
 &lt;input type=&quot;button&quot; value=&quot;Pause&quot; id=&quot;pause&quot; /&gt;
 &lt;input type=&quot;button&quot; value=&quot;Stop&quot; id=&quot;stop&quot; /&gt;
&nbsp;
 &lt;span id=&quot;timecode&quot;&gt;&lt;/span&gt;
 &lt;span id=&quot;progressContainer&quot;&gt;
   &lt;span id=&quot;timecode&quot;&gt;&lt;/span&gt;
   &lt;span id=&quot;progress&quot;&gt;&lt;/span&gt;
 &lt;/span&gt;
&nbsp;
&lt;/div&gt;</pre></div></div>

<p>All we need to do now is attach audio events to my elements which we can do using the new Audio API and a wee bit of jQuery.</p>
<p>First we create a new audio object and point it at the right file:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> music <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Audio<span style="color: #009900;">&#40;</span>mynewtrack.<span style="color: #660066;">ogg</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Then we hook an audio play action of the play button in the HTML with some jQuery:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#play'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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>
  music.<span style="color: #660066;">play</span><span style="color: #009900;">&#40;</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></pre></div></div>

<p>We can do exactly the same with the pause button.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#pause'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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>
  music.<span style="color: #660066;">pause</span><span style="color: #009900;">&#40;</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></pre></div></div>

<p>Now let’s do something a bit tricky and create a progress bar which will move as we listen to the track.</p>
<p>All we need to know is the total duration of the track and where the imaginary playhead currently is.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Display our progress bar</span>
music.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'timeupdate'</span><span style="color: #339933;">,</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>
&nbsp;
  <span style="color: #006600; font-style: italic;">// How long is the track in seconds</span>
  <span style="color: #003366; font-weight: bold;">var</span> length <span style="color: #339933;">=</span> music.<span style="color: #660066;">duration</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Whereabouts are we in the track</span>
  <span style="color: #003366; font-weight: bold;">var</span> secs <span style="color: #339933;">=</span> music.<span style="color: #660066;">currentTime</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// How far through the track are we as a percentage</span>
  <span style="color: #003366; font-weight: bold;">var</span> progress <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>secs <span style="color: #339933;">/</span> length<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">100</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Change the width of the progress bar</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#progress'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'width'</span> <span style="color: #339933;">:</span> progress <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// And finally calculate where we are in the track</span>
  <span style="color: #003366; font-weight: bold;">var</span> tcMins <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>secs<span style="color: #339933;">/</span><span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> tcSecs <span style="color: #339933;">=</span> parseInt<span style="color: #009900;">&#40;</span>secs <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span>tcMins <span style="color: #339933;">*</span> <span style="color: #CC0000;">60</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// If the number of seconds is less than 10, add a '0'</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>tcSecs <span style="color: #339933;">&lt;</span> <span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> tcSecs <span style="color: #339933;">=</span> <span style="color: #3366CC;">'0'</span> <span style="color: #339933;">+</span> tcSecs<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// Display the time</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#timecode'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>tcMins <span style="color: #339933;">+</span> <span style="color: #3366CC;">':'</span> <span style="color: #339933;">+</span> tcSecs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The first thing we do is add an event listener which looks for the timeupdate event; once we know the music is playing (and so the time is updating) we can work out where we are in the song.</p>
<p>Next we need to find out how long the track is with music.duration and whereabouts in the track we are with music.currentTime. That&#8217;s really all the Audio API we&#8217;ll be using &#8211; the rest is vanilla JavaScript and jQuery.</p>
<p>Some pretty basic maths will then give us the percentage we are through the track, and then we can use CSS to alter the width of the progress bar. Finally I also put the time in minutes and seconds onto the page.</p>
<p>This is what it will look like:</p>
<p><img class="alignnone size-full wp-image-9091" src="http://thinkvitamin.com/wp-content/uploads/2010/10/audioplayer.png" alt="" width="280" height="87" /></p>
<h3>Turn it Up</h3>
<p>For those of you who are familiar with <a href="http://thinkvitamin.com/code/fun-with-html5-forms/">HTML5 forms</a> you may recall the range slider. Well, turns out we could use one of these for a volume control!</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input id=&quot;volumeSlider&quot; type=&quot;range&quot; min=&quot;0&quot; max=&quot;10&quot; value=&quot;8&quot; /&gt;</pre></div></div>

<p>We just need to add in a little jQuery:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Take care of the volume slider</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#volumeSlider'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</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: #006600; font-style: italic;">// Set the music volume based on the slider’s position</span>
  music.<span style="color: #660066;">volume</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: #339933;">/</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And there you have it &#8211; a perfectly respectable music player thanks to HTML5.</p>
<h3>More from the JavaScript API</h3>
<p>As well as play, pause and volume there are bunch of other JavaScript attributes and methods we can call upon. Here are a few:</p>
<ul>
<li>autoplay</li>
<li>buffered</li>
<li>currentTime</li>
<li>duration</li>
<li>ended</li>
<li>error</li>
<li>loop</li>
<li>paused</li>
<li>played</li>
<li>startTime</li>
<li>seekable</li>
<li>preload</li>
</ul>
<p>You can see a comprehensive list at <a href="http://www.w3.org/TR/html5/video.html#audio" target="_blank">http://www.w3.org/TR/html5/video.html#audio</a>.</p>
<p>Note the difference between the methods play() and pause() versus the events play and pause. A method is something you do to the audio file, whilst the event is something you can listen for and attach events to.</p>
<h3>Sounds Good</h3>
<p>So there we have it &#8211; a run through of the exciting new Audio element in HTML5. I don&#8217;t think it&#8217;s an element that you&#8217;ll use a great deal, and like HTML5 video browser support remains a little sketchy. However it&#8217;s another exciting development for web professionals and as with all new technologies it&#8217;s great to get in on the ground floor and watch it flourish.</p>
<h3>Further Reading</h3>
<p>For an in-depth look at how to create a live streaming radio player, check out the great tutorial at <a href="http://dev.opera.com/articles/view/html5-audio-radio-player/" target="_blank">http://dev.opera.com/articles/view/html5-audio-radio-player/</a></p>
<p>Here are some more interesting links:</p>
<ul>
<li><a href="http://dev.opera.com/articles/view/everything-you-need-to-know-about-html5-video-and-audio/">http://dev.opera.com/articles/view/everything-you-need-to-know-about-html5-video-and-audio/</a></li>
<li><a href="https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox">https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox</a></li>
<li><a href="http://www.apple.com/html5/showcase/audio/">http://www.apple.com/html5/showcase/audio/</a></li>
<li><a href="https://wiki.mozilla.org/Audio_Data_API">https://wiki.mozilla.org/Audio_Data_API</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/code/html5-audio-unplugged/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Fun with HTML5 Forms</title>
		<link>http://thinkvitamin.com/code/fun-with-html5-forms/</link>
		<comments>http://thinkvitamin.com/code/fun-with-html5-forms/#comments</comments>
		<pubDate>Tue, 12 Oct 2010 09:58:48 +0000</pubDate>
		<dc:creator>Richard Shepherd</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[HTML5]]></category>
		<category><![CDATA[Features]]></category>

		<guid isPermaLink="false">http://thinkvitamin.com/?p=8268</guid>
		<description><![CDATA[Do you remember your first form? The first time you realised you could send emails through your site, thanks to a hosted service like Bravenet (remember them?!). Or perhaps you hacked together a cgi script or even some PHP. But when that first email arrived with the subject line ‘test’ you realised that your website [...]]]></description>
			<content:encoded><![CDATA[<p>Do you remember your first form? The first time you realised you could send emails through your site, thanks to a hosted service like Bravenet (remember them?!).</p>
<p>Or perhaps you hacked together a cgi script or even some PHP. But when that first email arrived with the subject line ‘test’ you realised that your website had now become <em>interactive</em>. You could now not only interact with your users, but also tailor your site based on their input.<span id="more-8268"></span></p>
<h3>Long Live Forms</h3>
<p>Years later forms are still the primary way our users can interact with us, more than just clicking on links or triggering JavaScript events. Indeed, it’s likely the first thing you do when you start up your favourite browser is type a search query into a form.</p>
<p>Forms and form elements, as they appeared in the HTML4 spec, were utilitarian but hardly elegant. So we got smart. We enlisted our ever loyal friend JavaScript to lift forms to new heights, create new types of interaction, and give the user instant feedback and support.</p>
<h3>Feature Rich Forms</h3>
<p>With a combination of server-side scripting and AJAX we now have feature-rich forms, but all at the expense of programming complexity. Funny thing is, we’re now so used to this sort of code that what is about to happen almost seems <em>too</em> simple.</p>
<p>Inevitably, as is the case with much of the HTML5 spec, HTML has now caught up with the needs of web designers and developers by providing a remarkably simple way of creating usable and even extensible forms.</p>
<h2>Email Sign-up Form Demo</h2>
<p>I’ve <a href="http://richardshepherd.com/tv/forms/" target="_blank">created a demo</a> of an email sign-up form you can check out at <a href="http://richardshepherd.com/tv/forms/" target="_blank">http://richardshepherd.com/tv/forms/</a>.</p>
<p><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/html5forms-intro.jpg" alt="" /></p>
<p>Each form field uses something from the HTML5 spec and we’ll run through it one step at a time. When I refer to a browser supporting a particular feature, I mean the very latest stable release of that browser which you can download from the relevant vendor website.</p>
<p>Of course, when dealing with anything HTML5 related (like our previous post on <a href="../code/getting-started-with-html5-video/">HTML5 video</a>) there is a huge browser caveat &#8211; some browsers support all some or none of what we’re about to discuss. And it’s changing <em>really fast</em>.</p>
<p>Your best bet at the moment is to use <a href="http://www.opera.com/" target="_blank">Opera</a>, <a href="http://www.google.com/chrome" target="_blank">Chrome</a> or <a href="http://www.apple.com/iphone/features/safari.html" target="_blank">mobile Safari</a>,  although we’ll look at browser support a little later on (together with  a piece of JavaScript which helps test for certain features).</p>
<h2>A Place for Everything</h2>
<p><strong>Feature</strong>: Placeholder text<br />
<strong>Browsers</strong>: Chrome, Safari, Firefox 4</p>
<p>I think most of us have played with JavaScript enough to have placeholder text appear and disappear in a form field. You know, it’s normally light grey text which hints at what should be typed in the form field, and once the form field is selected it disappears. Then, when the cursor leaves the form field a check is made, if the field is still empty then redisplay the placeholder text. Such an intuitive thing sounds convoluted when typed out like that, and it needs a few lines of JavaScript to make it happen. But not any more.</p>
<p><a href="http://richardshepherd.com/tv/forms/"><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/html5forms-placeholder.jpg" alt="" /></a></p>
<p>Let’s start with the first name. Here’s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input name=&quot;firstName&quot; placeholder=&quot;First Name&quot; required /&gt;</pre></div></div>

<p>Wow! Seriously, go ahead and test it. Isn’t it beautiful?! Simple yes, but also elegant and highly usable. It does exactly what we need it to do, and doesn’t need to get any more complicated than that.</p>
<p>But hold on a second, there’s no sign of type=&#8221;text&#8221;. Well in the good old world of HTML5 inputs default to text unless otherwise specified; likewise forms methods default to GET unless specified.</p>
<h2>No Jacket Required</h2>
<p><strong>Feature</strong>: Required form fields<br />
<strong>Browsers</strong>: Opera, Firefox 4</p>
<p>We have all relied on Javascript to validate a form at some point or another. It’s no substitute for good server-side validation (particularly when it comes to security) but it is an excellent way of providing the user instant feedback on their input. One of the many things we validate is that a user has entered data for a required field and this is now something we can do with HTML5!</p>
<p>Here’s what it looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input id=&quot;lastName&quot; type=&quot;text&quot; name=&quot;lastName&quot; placeholder=&quot;Last Name&quot; required /&gt;</pre></div></div>

<p>One of the currently under-supported features of HTML5 forms, adding the required keyword allows browsers to validate the form on the client-side much like Javascript can. And until the feature is universally adopted you can future-proof your document by using Javascript to check for the ‘required’ attribute rather than a particular class or name.</p>
<p>Here’s how Opera renders an error on a required field:</p>
<p><a href="http://richardshepherd.com/tv/forms/"><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/html5forms-noemail1.jpg" alt="" /></a></p>
<p>When used with type=&#8221;email&#8221;, the required field can even validate an email address:</p>
<p><a href="http://richardshepherd.com/tv/forms/"><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/html5forms-illegalemail1.jpg" alt="" /></a></p>
<p>You might also notice that the validation is also affecting the border colour. Required elements start off red, and when you type something into them it changes to green. As you move onto the next field it then changes to grey &#8211; the default colour.</p>
<p>The same is true of email validation &#8211; the email field won&#8217;t turn green until you&#8217;ve entered a valid email address. This is a nice touch, and it&#8217;s added with some simple CSS pseudo-selectors:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">input <span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#999</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
input<span style="color: #3333ff;">:focus </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#6C6</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
input<span style="color: #3333ff;">:invalid </span><span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">border</span><span style="color: #00AA00;">:</span> <span style="color: #933;">2px</span> <span style="color: #993333;">solid</span> <span style="color: #cc00cc;">#F00</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<h2>Getting More Specific</h2>
<p><strong>Feature</strong>: Email, Web &amp; Phone fields<br />
<strong>Browsers</strong>: Safari for iPhones</p>
<p>First let’s take a look at the code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input id=&quot;emailaddress&quot; type=&quot;email&quot; name=&quot;emailaddress&quot; placeholder=&quot;anything@example.com&quot; required /&gt;</pre></div></div>

<p>All this is doing is telling the browser what type of data to expect which, at this point, is not really of any benefit unless you’re using mobile Safari, probably on your iPhone. Because what Safari can do is change the keyboard layout depending on the type of data that is being inputted.</p>
<p>For example, in an email field it handily offers the @ symbol in the touch screen keypad. If you change the type from email to ‘phone’ or ‘website’, mobile Safari changes the keyboard further. With more and more of us accessing the web through mobile devices this type of behaviour is only going to become more prevalent.</p>
<p><a rel="attachment wp-att-8397" href="http://thinkvitamin.com/code/fun-with-html5-forms/attachment/iphoneform-2/"><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/iphoneform1.png" alt="" /></a></p>
<h2>It&#8217;s a Date!</h2>
<p><strong>Feature</strong>: Date Picker<strong><br />
Browsers</strong>: Opera</p>
<p>This is another example of where custom built widgets may eventually be replaced by browser controls. A date selector is such a useful piece of kit and it’s great to see browser vendors slowly adopt this.</p>
<p>At the moment there is very little control over the date picker but I’m sure that will change of the coming months and years. Imagine, a browser based date picker that you can style with CSS. Perfect!</p>
<p>Here’s the code:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input id=&quot;birthday&quot; name=&quot;birthday&quot; type=&quot;date&quot; /&gt;</pre></div></div>

<p>And here’s what it looks like in Opera 10.62:</p>
<p><a href="http://richardshepherd.com/tv/forms/"><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/html5forms-date.jpg" alt="" /></a></p>
<p>Now some have commented that this Opera date-picker is far from attractive, but it does serve it’s purpose and it’s a HUGE step in the right direction. There are even some variations on a theme. The above code generates a standard date picker (day, month, year) but we can be more specific:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;datetime&quot; /&gt;
&lt;input type=&quot;datetime-local&quot; /&gt;
&lt;input type=&quot;time&quot; /&gt;
&lt;input type=&quot;week&quot; /&gt;
&lt;input type=&quot;month&quot; /&gt;</pre></div></div>

<h2>Datalist &#8211; A list of Data</h2>
<p><strong>Feature</strong>: Datalist<br />
<strong>Browsers</strong>: Opera</p>
<p>Whilst not quite an auto-suggest feature, datalists allow us to present the user with a range of options to use in a text input box. This is best used when there aren’t too many options, because with no filter applied to the list as you type it doesn’t get any shorter the more characters you input.</p>
<p>Whilst I don’t think you could list every country in the world (as the list would fall off the bottom of the page) you might want to include a few of the more popular ones like so:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input id=&quot;country&quot; type=&quot;text&quot; name=&quot;country&quot;  size=&quot;40&quot; list=&quot;countries&quot; /&gt;
 &lt;datalist id=&quot;countries&quot;&gt;
  &lt;option value=&quot;Australia&quot;&gt;
  &lt;option value=&quot;Canada&quot;&gt;
  &lt;option value=&quot;Germany&quot;&gt;
  &lt;option value=&quot;France&quot;&gt;
  &lt;option value=&quot;United Kingdom&quot;&gt;
  &lt;option value=&quot;United States&quot;&gt;
 &lt;/datalist&gt;</pre></div></div>

<p>(Forgive me if your country is not on that list &#8211; it doesn&#8217;t mean it isn’t &#8216;popular&#8217;!)</p>
<p>Whilst perhaps not a replacement for a good JavaScript based auto-suggest tool, it is still a useful enhancement to a normal text field. And like most of the HTML5 form elements, it’s easy to implement and greatly adds to user experience.</p>
<p><a href="http://richardshepherd.com/tv/forms/"><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/html5forms-datalist.jpg" alt="" /></a></p>
<p><em>NB. When testing this code I found that including a Datalist input broke the page in Safari (5.0.2, Mac); it simply stopped rendering the page when it reached the list. It doesn’t seem to have this effect in any other browser.</em></p>
<h2>Forms by Numbers<em><br />
</em></h2>
<p><strong>Feature</strong>: Numbers<br />
<strong>Browsers</strong>: Opera, Chrome, iPhone</p>
<p>The final feature we’re going to take a look at are numbers. By simply specifying an input type as ‘number’ you have access to ‘Increase’ and ‘Decrease’ buttons which allow the user to input whole integers. You will probably see these input types referred to as ‘spinners’ or a ‘spinbox’.</p>
<p><a href="http://richardshepherd.com/tv/forms/"><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/html5forms-numbers.jpg" alt="" /></a></p>
<p>On the iPhone the touch keyboard changes to one that is number-friendly.</p>
<p>As ever the code is easy and very readable:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input id=&quot;computers&quot; name=&quot;computers&quot; type=&quot;number&quot; min=&quot;0&quot; max =&quot;100&quot; step=&quot;1&quot; value=&quot;2&quot; /&gt;</pre></div></div>

<p>The <strong>min</strong>, <strong>max</strong>, <strong>step</strong> and <strong>value</strong> attributes are optional.</p>
<p>Again, I believe using this sort of control needs some thought as no-one  will want to click the button eighty-four times just to enter the number 84, they’ll simply type it in. However for smaller numbers and for altering numbers it’s a useful addition to the web designers arsenal.</p>
<h2>But wait, There’s More!</h2>
<p>In this article I’ve included what I think are the most interesting and practical additions to HTML forms, but there are others in the HTML5 spec which you might want to check out. They include:</p>
<ul>
<li><a href="http://dev.w3.org/html5/spec/association-of-controls-and-forms.html#autofocusing-a-form-control" target="_blank">Autofocus</a></li>
<li><a href="http://dev.w3.org/html5/spec/number-state.html#range-state" target="_blank">Range</a> (sliders)</li>
<li><a href="http://dev.w3.org/html5/spec/number-state.html#color-state" target="_blank">Color</a></li>
</ul>
<p>Go ahead and check them out &#8211; you might find one of them useful.</p>
<h2>Falling Back</h2>
<p>The great thing about many of these HTML5 form enhancements is that they will degrade gracefully to standard input boxes. However some don’t play nice at all, even with so-called ‘modern’ browsers.</p>
<p>The date picker, for example, can behave in odd and unusual ways. In Opera it works just fine but in Chrome there is simply an option to increase and decrease a date by one day &#8211; starting in 1582!!</p>
<p><a href="http://richardshepherd.com/tv/forms/"><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/html5forms-dateerror.jpg" alt="" /></a></p>
<p>This is far from helpful and I’d argue a complete <a href="http://search.twitter.com/search?tag=usabilityfail">#usabilityfail.</a> So how can we serve Opera the date picker and Chrome something else? Our old friend JavaScript, that’s how.</p>
<p>Before we go on to look at the code it’s also worth remembering that not everyone has JavaScript installed. Unfortunately for those users the form may malfunction or even stop the rest of the page loading (see Datalists, above).</p>
<p>Only you can decide how far you want to push the envelope in these fledgling days for HTML5 forms. For personal projects I urge you to heed the words of Salt ‘N’ Peppa, and “push it, p-p-push it real good”.</p>
<h2>JavaScript to the Rescue!</h2>
<p>With JavaScript we can check to see if a browser supports a particular attribute like type=&#8221;number&#8221;, and take action based on the result.</p>
<p>I first saw this code suggested by <a href="http://adactio.com/" target="_blank">Jeremy Keith</a> in his excellent book ‘<a href="http://books.alistapart.com/" target="_blank">HTML5 For Web Designers</a>’ (which I encourage you all to buy):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> checkAttribute<span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> attribute<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 <span style="color: #003366; font-weight: bold;">var</span> test <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>attribute <span style="color: #000066; font-weight: bold;">in</span> test<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can then check to see if a browser supports a particular feature with something like:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>checkAttribute<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'placeholder'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// No support for placeholders, so add them with JS</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>A Word on Modernizr</h2>
<p>Another way to check if a browser supports a particular element is by using the funky <a href="http://www.modernizr.com/" target="_blank">Modernizr</a> script. This handy utility, when included in a page, adds a bunch of classes to the &lt;body&gt; tag that indicates which features are available. A wave of the JavaScript magic wand and you can decide exactly what can and can’t be used on the page.</p>
<p>A typical Modernizr script might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>Modernizr.<span style="color: #660066;">input</span>.<span style="color: #660066;">date</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// This browser supports date inputs. Hurrah!</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">// Oh dear. Time to include your own</span>
  <span style="color: #006600; font-style: italic;">// JavaScript date solution...</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><a href="http://www.modernizr.com/"><img src="http://thinkvitamin.com/wp-content/uploads/2010/10/html5forms-modernizr.jpg" alt="" /></a></p>
<h2>Wrapping it All Up</h2>
<p>So there you have it &#8211; a run through of some of the more useful and supported additions that HTML5 brings to our forms. I hope you can see the potential in some of them for your site and, as with all shiny new toys, I encourage you to play with them on personal projects but use sparingly on commercial outings. At least for now.</p>
<p>The additions to the HTML5 spec are direct responses to how we use the web in the real world, which is what makes them so effective. I’m not sure we’ll ever turn our backs on JavaScript when it comes to validating and controlling forms but usable, elegant form design is about to get a whole lot easier!</p>
<h2>Further Reading</h2>
<p>If you&#8217;re looking for more &#8220;input&#8221; that&#8217;s &#8220;on form&#8221; then &#8220;submit&#8221; yourself to these great articles:</p>
<ul>
<li><a href="http://www.w3.org/TR/html5/forms.html#the-form-element" target="_blank">http://www.w3.org/TR/html5/forms.html#the-form-element</a></li>
<li><a href="http://www.miketaylr.com/code/input-type-attr.html">http://www.miketaylr.com/code/input-type-attr.html</a></li>
<li><a href="http://en.wikipedia.org/wiki/Comparison_of_layout_engines_(HTML5)">http://en.wikipedia.org/wiki/Comparison_of_layout_engines_(HTML5</a>)</li>
<li><a href="http://www.opera.com/docs/specs/presto26/html5/" target="_blank">http://www.opera.com/docs/specs/presto26/html5/</a></li>
<li><a href="http://24ways.org/2009/have-a-field-day-with-html5-forms" target="_blank">http://24ways.org/2009/have-a-field-day-with-html5-forms</a></li>
<li><a href="http://net.tutsplus.com/tutorials/html-css-techniques/rethinking-forms-in-html5/" target="_blank">http://net.tutsplus.com/tutorials/html-css-techniques/rethinking-forms-in-html5/</a></li>
</ul>
<p>Have you tried out HTML5 forms yet? What do you think? We&#8217;d love to know!</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/code/fun-with-html5-forms/feed/</wfw:commentRss>
		<slash:comments>44</slash:comments>
		</item>
		<item>
		<title>Twitter @Anywhere Revisited</title>
		<link>http://thinkvitamin.com/code/twitter-anywhere-revisited/</link>
		<comments>http://thinkvitamin.com/code/twitter-anywhere-revisited/#comments</comments>
		<pubDate>Mon, 06 Sep 2010 14:00:01 +0000</pubDate>
		<dc:creator>Richard Shepherd</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://thinkvitamin.com/?p=7200</guid>
		<description><![CDATA[I must be honest, I was pretty unimpressed with Twitter @Anywhere when it first launched earlier this year. Of course I hadn’t bothered to check it out, but it seemed like ‘Integration Lite’ &#8211; just a very simple way for bloggers to include Twitter on their site. As Wired reported back in March Twitter’s Evan [...]]]></description>
			<content:encoded><![CDATA[<p>I must be honest, I was pretty unimpressed with <a href="http://dev.twitter.com/anywhere" target="_blank">Twitter @Anywhere</a> when it first launched earlier this year. Of course I hadn’t bothered to check it out, but it seemed like ‘Integration Lite’ &#8211; just a very simple way for bloggers to include Twitter on their site.</p>
<p>As <a href="http://www.wired.com/epicenter/2010/03/sxsw-twitter-ceo-evan-williams-launches-anywhere/" target="_blank">Wired reported back in March</a> Twitter’s Evan Williams promised <tt>@Anywhere</tt> would “reduce friction.”</p>
<blockquote><p>“You could be reading your favourite columnist [and] you can easily tweet from the column itself if […] you may just want to follow the columnist […] without going back to Twitter.”</p></blockquote>
<p>Amongst the launch partners for <tt>@Anywhere</tt> were <a href="http://digg.com/" target="_blank">Digg</a>, <a href="http://www.huffingtonpost.com/" target="_blank">Huffington Post</a> and <a href="http://www.nytimes.com/" target="_blank">The New York Times</a>. And one of the most high profile users the little known <a href="http://followfinder.googlelabs.com/" target="_blank">Follow Finder</a> from The Mighty Google.</p>
<p style="text-align: center;"><a href="http://www.followfinder.googlelabs.com/"><img class="size-medium wp-image-7201  aligncenter" src="http://thinkvitamin.com/wp-content/uploads/2010/09/followfinder.png" alt="" width="470" /></a></p>
<p>But alas, it was more a fizzle than a firework and like many at the time I was rather nonplussed.</p>
<p>I’m pleased to say I’ve been completely converted. Not only is playing with Twitter inherently a cool thing to do, it can add real power to your website and more importantly your web applications. Indeed, it’s the scope for easily including Twitter functionality in web applications that’s really exciting.</p>
<p>So let’s take a look at some of this functionality and how you can use it. You’ll be up and running in just minutes, and after briefly looking at some of the simpler methods we’ll dive straight in to integrating Twitter right into your web app.<span id="more-7200"></span></p>
<h3>Registering for @Anywhere</h3>
<p>The first thing you need to do is register for an Application ID. It’s ridiculously simply to get, but perhaps worth some explaining to the uninitiated. First head on over to <a href="http://dev.twitter.com/anywhere" target="_blank">http://dev.twitter.com/anywhere</a> and click on the &#8216;Start using it now&#8217; button.</p>
<p style="text-align: center;"><a href="http://dev.twitter.com/anywhere"><img class="size-medium wp-image-7205  aligncenter" src="http://thinkvitamin.com/wp-content/uploads/2010/09/anywherehomepage.png" alt="" width="470" /></a></p>
<p>You’ll be swiftly taken to a one-page form where you register your app. The first question, particularly to those new at this whole API game, is ‘what if I don’t have an app?’.</p>
<p>Let’s say you just want to use hovercards on your blog. Or let’s say you want your users to be able to tweet about your blog articles by providing them a tweet box at the footer of each page. Not really an ‘app’ is it? More of a feature, a widget perhaps.</p>
<p>Well, in the big wide world of APIs what you have is a web app my friend and you need to accept it! Even the humble ‘Linkify’ method (automatically turning all Twitter usernames into links to their profiles) makes your humble blog post a web application of sorts. Deal with this fuzzy distinction and move on. We need that app ID!!</p>
<p><em><strong>NB:</strong> For the rest of this article I’ll use the term app/application, but it equally applies to your site/blog</em></p>
<h3>Completing the Form</h3>
<p><strong>Application Name</strong></p>
<p>Name your application wisely because users will see this name when they are asked to authorise your app; although it’s worth noting that certain functionality, like linkifying and hovercards, doesn’t require users to see this name.</p>
<p><strong>Callback URL</strong></p>
<p>If you intend to use some of the <tt>@Anywhere</tt> functionality which requires users to authenticate your app then Twitter needs to know which page to send the API response to (which includes lots of handy data about the user that we can tap into using JavaScript!). The callback URL is most likely the very same page that has called the authentication.</p>
<p style="text-align: center;"><a href="http://dev.twitter.com/anywhere/apps/new"><img class="size-medium wp-image-7210  aligncenter" src="http://thinkvitamin.com/wp-content/uploads/2010/09/register.png" alt="" width="470"  /></a></p>
<p>If you’re only using simple <tt>@Anywhere</tt> methods like linkify you can just put your site’s homepage in here.</p>
<p><strong>Default Access Type</strong></p>
<p>This defaults to Read-only, but we want to use some advanced <tt>@Anywhere</tt> methods so make sure you change this to Read &amp; Write.</p>
<h3>Basic functions</h3>
<p>Okay, we’ve filled up this car with petrol/gas so it’s time to take it out for a spin! Open a new HTML document and in the <tt>&lt;head&gt;</tt> of the document include the following line of code:</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: #339933;">&lt;</span>script src<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;http://platform.twitter.com/anywhere.js?id=YOUR-APP-ID-GOES-HERE&amp;v=1&quot;</span><span style="color: #339933;">&gt;&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Don&#8217;t forget to add your app ID into the URL.</p>
<p>Before I go on I feel I must address those of you who now resemble Yosamite Sam, with steam coming out of their ears at the idea of ever including a JavaScript file at the top of a page. But including the <tt>anywhere.js</tt> file in the head makes a lot of sense for a number of reasons, outlined by Twitter themselves:</p>
<ul>
<li>It’s small. Less than 3kb to be exact, gzipped up into a tiny package.</li>
<li>All dependencies for <tt>@Anywhere</tt> features are loaded asynchronously, so you only get what you need when you need it.</li>
<li>Most importantly, when a user logs in and authenticates your app the Twitter login page redirects back to the callback URL. This happens in a pop-up window, so if anywhere.js is at the bottom of the page it’ll mean that the user sees your site in that pop-up before it disappears. Which looks bad and, as Twitter put it, ‘is a poor user experience’.</li>
</ul>
<p>So leave it in the <tt>&lt;head&gt;</tt> and let’s move on. Now we need to initialise the a global <tt>@Anywhere</tt> object which we do with:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
twttr.<span style="color: #660066;">anywhere</span><span style="color: #009900;">&#40;</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>For those who have tried to run that script, you’ll see that nothing happens. That’s because we need to give it a callback function of some sort. For example, to linkify every Twitter username on a page you simply need:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
twttr.<span style="color: #660066;">anywhere</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
T.<span style="color: #660066;">linkifyUsers</span><span style="color: #009900;">&#40;</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: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>What’s with the capital ‘T’? Well that’s just Twitter’s convention so we’ll stick with it, but you’re welcome to change it to a more descriptive name. An instance of the API client is passed into T for us to then call methods on, just as we’ve done above.</p>
<p>Run that puppy on a page which has a username in the markup and watch it magically turn into a link. It was roughly about now that I gave up on <tt>@Anywhere</tt> earlier this year, but stick with me here &#8211; it get’s better.</p>
<h3>Hovercards</h3>
<p>Another method that works in a similar way is Hovercards, which work in the same way.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
twttr.<span style="color: #660066;">anywhere</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
T.<span style="color: #660066;">hovercards</span><span style="color: #009900;">&#40;</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: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p><img class="aligncenter size-full wp-image-7225" src="http://thinkvitamin.com/wp-content/uploads/2010/08/hovercard2.png" alt="" width="313" height="271" /></p>
<p>With linkifying and hovercarding under our belts its worth looking at another couple of handy features.</p>
<h3>Scope</h3>
<p>You can limit the scope of a method by passing in a CSS selector. A lot of you will spot that this looks a lot like jQuery and that’s because it’s using the same <a href="http://sizzlejs.com/" target="_blank">Sizzle</a> selection engine.</p>
<p>For example, to linkify usernames only in the DIV with an ID of ‘sidebar’ you could use the following code</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
twttr.<span style="color: #660066;">anywhere</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
T<span style="color: #009900;">&#40;</span>‘#sidebar’<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">linkifyUsers</span><span style="color: #009900;">&#40;</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: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h3>Configuration Options</h3>
<p>There are a number of option that we can pass to these methods, which we do in an object literal. Again, if you&#8217;ve used jQuery before then this will be nothing new to you:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
twttr.<span style="color: #660066;">anywhere</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
T<span style="color: #009900;">&#40;</span>‘#sidebar’<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hovercards</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> expanded<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</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: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Adding <tt>{expanded: true}</tt> does exactly what it says &#8211; it renders the Hovercard in it&#8217;s expanded form from the off, rather than waiting for the user to click &#8216;more&#8217;.</p>
<h3>Moving Swiftly On</h3>
<p>I’m going to skip a bit here, which relates to some of the other configuration options you can use with the various &#8220;Anywhere methods. I highly recommend Twitter’s own documentation which you can find at <a href="http://dev.twitter.com/anywhere/begin" target="_blank">http://dev.twitter.com/anywhere/begin</a>.</p>
<h3>Getting more Advanced</h3>
<p>If you’ve spent much time surfing the interweb you’ve at some point seen that pop-up window asking you if you authorise an app or a site to play with your Twitter account. </p>
<p>If, like me, you blindly click yes you give that site direct access to lots of your personal (albeit public) information. Normally this isn’t a problem, but with great power comes great responsibility &#8211; as James Cunningham of <a href="http://twifficiency.com/" target="_blank">Twifficiency</a> fame (more at <a href="http://techcrunch.com/2010/08/17/twifficiency/" target="_blank">TechCrunch</a>).</p>
<p><img class="aligncenter size-medium wp-image-7211" src="http://thinkvitamin.com/wp-content/uploads/2010/09/authorise.png" alt="" width="470" height="" /></p>
<p>There are a couple of ways <tt>@Anywhere</tt> uses this connectivity. The first is with built in functions like the Tweet Box and the Follow button. Both need the user to login to Twitter and authorise your app, so both will cause the authorisation pop-up. We can call both like so:</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="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
twttr.<span style="color: #660066;">anywhere</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
T<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#follow-button'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">followButton</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;richardshepherd&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
T<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#tweet-box'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">tweetBox</span><span style="color: #009900;">&#40;</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: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>This places a follow button in the element with an ID of &#8216;follow-button&#8217; (and which is set to follow my Twitter account!), and a tweetbox in the element with an ID of &#8216;tweet-box&#8217;.</p>
<p><img class="aligncenter size-medium wp-image-7234" src="http://thinkvitamin.com/wp-content/uploads/2010/08/tweetbox-300x77.jpg" alt="" width="300" height="77" /></p>
<p>You’ll can find further details of the Tweet Box and Follow Buttons in the <a href="http://dev.twitter.com/anywhere/begin" target="_blank">official documentation</a>, but I think it get’s much more exciting to look at connecting your app with someone’s Twitter account and pulling information from their public profile. So let’s go ahead and crank things up! It’s surprisingly simple.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
twttr.<span style="color: #660066;">anywhere</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
T<span style="color: #009900;">&#40;</span>‘#connect’<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">connectButton</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> size<span style="color: #339933;">:</span> “medium” <span style="color: #009900;">&#125;</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: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>And that’s it. Try it out, and you’ll see the familiar pop-up asking for you to authorise your app (the name you gave it when you signed up). You can change the size be replacing ‘medium’ with ‘small’, ‘large’ or ‘xlarge’.</p>
<p style="text-align: center;"><img class="size-full wp-image-7214  aligncenter" src="http://thinkvitamin.com/wp-content/uploads/2010/08/connectwtwitter.png" alt="" width="256" height="50" /></p>
<p>Medium is the default, so if you’re happy with that you can remove it from the code. But how about your own custom connect button &#8211; how cool would that be? It turns out that the Twitter API has a <tt>signIn</tt> method which we can attach to any element: text, image or whatever you fancy.</p>
<p>Let’s say you have created your own CSS rollover button, applied to a span with and ID of twitterLogin. Here’s how you’d use JavaScript and the Twitter API to hook the signIn method to that span:</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="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
twttr.<span style="color: #660066;">anywhere</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span>‘twitterLogin’<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">onclick</span> <span style="color: #339933;">=</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>
T.<span style="color: #660066;">signIn</span><span style="color: #009900;">&#40;</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>We are now freed from the shackles of Twitter’s own graphics and buttons, which makes it possible to create a unique experience for your own app or site. If the user is logged in, the pop-up window will simply ask them to authorise your app. If they aren’t logged in to Twitter it will also ask them for their login details.</p>
<h3>I Can Haz your Details?</h3>
<p>So your web app now has a connect button, and let’s assume your user has clicked it and logged in/authorised your app. Sweet. What next?</p>
<p>You should have set the Twitter callback URL to the same page as your web app, which means the pop-up will disappear and your page will be sent a bunch of new data (think of it as an AJAX request and response). This means you can use Javascript to check if the user is connected with the rather handy <tt>isConnected</tt> method. And, assuming it’s all gone to plan, the <tt>currentUser</tt> property contains lots of handy information.</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="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script<span style="color: #339933;">&gt;</span>
twttr.<span style="color: #660066;">anywhere</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>T<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>T.<span style="color: #660066;">isConnected</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// This user is connected. Awesome!</span>
<span style="color: #003366; font-weight: bold;">var</span> user <span style="color: #339933;">=</span> T.<span style="color: #660066;">currentUser</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> screenName <span style="color: #339933;">=</span> user.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span>‘screen_name’<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>‘Hi there ‘ <span style="color: #339933;">+</span> screenName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
<span style="color: #006600; font-style: italic;">// the user isn’t connected, so probably best to show them</span>
<span style="color: #006600; font-style: italic;">// a connect button!</span>
T<span style="color: #009900;">&#40;</span>‘#twitterLogin’<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">connectButton</span><span style="color: #009900;">&#40;</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>Admittedly the use of a JavaScript alert here is almost criminal, but you get the idea. Include jQuery and you can start injecting the DOM with lots of Twitter goodness, personalising the user’s experience.</p>
<p>So what other properties can we access, apart from Screen Name? Well, pretty much all of the user’s public profile information is up for grabs. Here are some of the most useful:</p>
<ul>
<li>description</li>
<li> followers_count</li>
<li> following</li>
<li> profile_image_url</li>
<li> location</li>
<li> name</li>
<li> time_zone</li>
</ul>
<p>And you can check out a full like at <a href="http://dev.twitter.com/anywhere/begin#user-properties" target="_blank">http://dev.twitter.com/anywhere/begin#user-properties</a></p>
<p>So using our example above you could access the profile image with:</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: #003366; font-weight: bold;">var</span> imageHTML <span style="color: #339933;">=</span> ‘<span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span>”’ <span style="color: #339933;">+</span> user.<span style="color: #660066;">data</span><span style="color: #009900;">&#40;</span>‘profile_image_url’<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> ‘“ <span style="color: #339933;">/&gt;</span>’<span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>What Next?</h3>
<p>Of course <tt>@Anywhere</tt> is no substitute for server side integration with the Twitter API, but it’s a lean way of enhancing your web app/web sites experience for Twitter users.</p>
<p>Have you started to use <tt>@Anywhere</tt>? If so, tell us how and whether you think it’s enhanced your web application or site. And which features would you like to see in <tt>@Anywhere</tt> in future releases?</p>
<h3>Further reading</h3>
<p>Have you been bitten by the <tt>@Anywhere</tt> bug? If so, here are some great places to find out more…</p>
<p style="text-align: center;"><a href="http://code.google.com/p/twitter-api/issues/list"><img class="size-medium wp-image-7215  aligncenter" src="http://thinkvitamin.com/wp-content/uploads/2010/09/googlecode.png" alt="" width="470" /></a></p>
<ul>
<li><a href="http://www.slideshare.net/toddkloots/anywhere/">http://www.slideshare.net/toddkloots/anywhere/</a></li>
<li><a href="http://platform.twitter.com/js-api.html">http://platform.twitter.com/js-api.html</a></li>
<li></li>
</ul>
<p>I also really like this integration of Twitter Hovercards into a WordPress blog comments</p>
<ul>
<li><a href="http://www.binarymoon.co.uk/2010/05/integrate-twitters-wordpress-comments/">http://www.binarymoon.co.uk/2010/05/integrate-twitters-wordpress-comments/</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/code/twitter-anywhere-revisited/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Getting Started with HTML5 Video</title>
		<link>http://thinkvitamin.com/code/getting-started-with-html5-video/</link>
		<comments>http://thinkvitamin.com/code/getting-started-with-html5-video/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 10:30:51 +0000</pubDate>
		<dc:creator>Richard Shepherd</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[HTML5]]></category>

		<guid isPermaLink="false">http://thinkvitamin.com/?p=6984</guid>
		<description><![CDATA[HTML5 Video. It just sounds sexy. Even typing it stirs an excitement inside me usually reserved for theme park rides and new episodes of Family Guy. It’s one of the most lauded features within the new HTML spec, and also one of the most divisive. Perhaps not divisive in the web community, most designers and [...]]]></description>
			<content:encoded><![CDATA[<p>HTML5 Video. It just sounds sexy. Even typing it stirs an excitement inside me usually reserved for theme park rides and new episodes of Family Guy. It’s one of the most lauded features within the new HTML spec, and also one of the most divisive.</p>
<p>Perhaps not divisive in the web community, most designers and developers seem to love the idea of it, but certainly divisive in the real world of cross browser support, licensing and (dare we even mention it) Adobe Flash.</p>
<p style="text-align: center"><img class="size-full wp-image-7064 aligncenter" style="border: 1px solid #999" src="http://thinkvitamin.com/wp-content/uploads/2010/08/SafariNativeControls.png" alt="" width="470" height="352" /><br />
<em>HTML5 Video in Safari with native browser controls</em></p>
<p>Let’s start at the end. Internet Explorer 9 promises to support <a href="http://www.webmproject.org/">WebM video</a>, one of the three main contenders to the HTML5 video throne, but from IE8 backwards you’re out of luck. In short, you need to use Flash. The same is true of all but the most recent versions of other major browsers, which of course poses a very relevant question.</p>
<h3>Why Use HTML5 Video at All?</h3>
<p>It’s a great question and, as I’ve mentioned, a divisive one. There are myriad reasons why and we’ll go on and look at a few of them. But if your users follow the usual browsing trend the chances are they won’t be able to see the video and you’ll need a fallback to Flash.</p>
<p>Even when IE9 makes it’s debut to the masses there will still be a legion of IE8, IE7 and IE6 users, together with other older browsers, that will need you to keep dipping your toe in Adobe’s pool.<span id="more-6984"></span></p>
<p>I need to add a caveat to this article. I live in the real world and I work on a site with millions of real users. The vast majority of these users have browsers that don’t support HTML5 video and I expect that’s the same for many of your sites too. As much as we want everyone to use Chrome on OSX it’s not going to happen so you will need to create a Flash fallback for the foreseeable future. I’m talking years here, not months.</p>
<p>There are some other problems with HTML5. There are three major camps emerging each championing a different video codec (a way to compress and decompress video files). As of writing there are some content protection issues (digital rights management) and some features, like full-screen video and native keyboard support, have yet to be fully fleshed out.</p>
<h3>So Why Bother?</h3>
<p>I think any good web practitioner is a pioneer, always seeking out new technologies and techniques to bring colour and life to their site. If you’ve got this far then you’re one of those people.</p>
<p>HTML5 video promises a wonderful environment to create unique video experiences, styled with CSS and controlled with JavaScript. A HTML5 video is just another element in the DOM which we can read and write to, and this allows us far more customisation on-the-fly than Flash ever could. It’s accessible, it’s standards compliant, and above all it’s simple.</p>
<p><a href="http://www.youtube.com/html5">YouTube</a> are already delivering some HTML5 videos, and <a href="http://vimeo.com/blog:268">Vimeo</a> have hopped on the bandwagon too. More and more sites are offering up the format, experimenting with it, and creating new and exciting ways to use video on their site. Check out the links at the end of this article for more places to check it out &#8211; just not with IE ;)</p>
<p style="text-align: center"><img class="size-full wp-image-7061 " style="border: 1px solid #999" src="http://thinkvitamin.com/wp-content/uploads/2010/08/YouTubeHTML5Video.png" alt="" width="470" height="283" /><br />
<em>The YouTube HTML5 Video Player</em></p>
<h3>Need More Reasons?</h3>
<p><strong>DOM, Meet Video</strong></p>
<p>They’ve been flirting for some time now but finally the DOM and video can make it official and head out on a date. You can position, float and z-index video till dawn, indeed anything you can do to a <tt>div</tt> you can do to a <tt>video</tt> element. No more of those pesky plugins that make Flash elements disappear if you have lightboxes &#8211; HTML5 video will play nice with the rest of the kids and, more importantly, be under your complete control thanks to JavaScript.</p>
<p><strong>Video, Meet CSS</strong></p>
<p>It doesn’t end there. As a DOM element HTML5 video can be styled. And since the only browsers that support it also support CSS3, in some form or another, it means you can add (webkit) transitions, box shadows, hover effects and all kinds of stylesheet goodness to make your video blend seamlessly into your page.</p>
<p><strong>Keyboard, meet Video</strong></p>
<p>Although only currently supported by Opera, all browsers will soon support keyboard accessibility of videos without any additional code. As <a href="http://dev.opera.com/articles/view/introduction-html5-video/" target="_blank">Bruce Lawson has noted</a> it’s almost impossible to <tt>TAB</tt> in and out of a Flash video, which is a real problem for those users without assistive browsing technology.</p>
<p>Add these new features together, throw in a dizzy helping of JavaScript, and you just know that someone, somewhere will create a HTML5 video editor.</p>
<h3>Using HTML5 Video on your Site</h3>
<p>In days of old when Knights were bold the interweb got all confused about anything that wasn’t text. Netscape and Mosaic decreed that images should be tagged up as <tt>&lt;img&gt;</tt> which, to be fair, makes some sense. But the W3C didn&#8217;t approve and they introduced the <tt>&lt;object&gt;</tt> tag. As the saying goes &#8220;you snooze, you lose&#8221; &#8211; the web had already adopted the Netscape/Mosaic method.</p>
<p>Then things got really strange. When the forerunner to Flash hit the web the W3C again suggested the <tt>object</tt> element but Netscape went ahead and created the <tt>&lt;embed&gt;</tt> element, which the W3C refused to validate. The resulting confusion gave us the wonderful markup-soup that is Flash video.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; width=&quot;100&quot; height=&quot;100&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot;&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;src&quot; value=&quot;http://www.youtube.com/v/Sv5iEK-IEzw?fs=1&amp;amp;hl=en_GB&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; width=&quot;100&quot; height=&quot;100&quot; src=&quot;http://www.youtube.com/v/Sv5iEK-IEzw?fs=1&amp;amp;hl=en_GB&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;</pre></td></tr></table></div>

<p>Without whining about the aesthetics of that, wouldn’t it be nice if we could simplify it to just: <tt>&lt;video src=”myVideo.ogv”&gt;&lt;/video&gt;</tt></p>
<p>Surely it’s not that simple? Well sorry to disappoint you but it is. In fact, because it’s HTML5 the quotes are optional too so it’s two characters longer than it needs to be. I’m not even going to insult you by explaining that code.</p>
<p>But how about we give this baby some shizzle?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;video src=myvideo.ogv width=640 height=480 controls poster=myImage.jpg&gt;&lt;/video&gt;</pre></td></tr></table></div>

<p>I think the only two things I need to explain here are controls, a boolean which displays the browsers native controls, and poster which is an image to display whilst the video is downloading. If you only specify one size attribute, the video will automatically resize the other maintaining the video’s aspect ratio.</p>
<p>There are other attributes we can throw in but as yet there is limited cross browser support for them. They include loop, autoplay, and autobuffer.</p>
<p>Need a fallback for those browsers that don’t support HTML5 video? That’s easy too:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;video src=myvideo.ogv width=640 height=480 controls poster=myImage.jpg&gt;
&lt;a href=myvideo.ogv&gt;Download this video.&lt;/a&gt;
&lt;/video&gt;</pre></td></tr></table></div>

<h3>Codecs and Acronyms</h3>
<p>Of course this does pose the question, what on earth is an <tt>.ogv</tt> file? Well until fairly recently it was a fairly obscure video file. It gets worse it’s not the only one and it won’t work in all browsers.</p>
<p>Yep, cross-browser compatibility rears it’s ugly head once more. There are (currently) three codecs we need to worry about the main reason seems to be browser vendor’s personal preference and their aversion to using patented technology. Take our old friend the .mp4 file, which is familiar to anyone with an Apple device. MP4 files are created with the H.264 (MPEG-4 AVC) codec and there are licensing issues a-plenty surrounding it, which is probably why only Apple (Safari) and Google (Chrome) are supporting it.</p>
<p>Partly because of this we are seeing the rise of two comparative new-comers to the video field &#8211; Theora and VP8. I’ll try to keep this brief &#8211; Theora is royalty-free and therefore can be used in any form without a problem. You’ll most often see .ogv files for videos encoded with this codec. It’s best friend is Vorbis audio (.ogg files), similarly royalty free. Browsers that support this type of video include Firefox, Chrome and Opera.</p>
<p>VP8 (stay with me, we’re nearly done with this) came from On2 (the same company behind VP3 which then became Theora). On2 was bought by Google earlier this year who then waved a legal magic wand and made the technology royalty-free. It’s more complicated than that, but I’m not a lawyer, suffice it to say anyone can use it. The VP8 codec creates .webm video files.</p>
<h3>That Last Bit Bored Me</h3>
<p>Me too, but it’s important and I encourage you to read a little more on these codecs and the implications of using them.</p>
<p>Here’s a summary of which browsers support what:</p>
<ul>
<li><strong>Theora</strong> (.ogv files) &#8211; Firefox 3.5+, Chrome 5.0+, Opera 10.5+</li>
<li><strong>H.264</strong> (.mp4 files) &#8211; Safari 3.0+, Chrome 5.0+, iPhone 3.0+, Android 2.0+</li>
<li><strong>WebM</strong> (.webm files) &#8211; Internet Explorer 9 (yay!), Firefox 4.0+, Chrome 6.0+, Opera 11.0+</li>
</ul>
<p><em>NB. The file extensions I’ve noted here are the most often used but you can encode videos of different types with different extensions &#8211; most of us needn’t worry too much about that though.</em></p>
<p>For an in-depth look at the dizzy world of video and audio codecs as they relate to HTML5, check out <a href="http://diveintohtml5.org/video.html" target="_blank">Mark Pilgrim&#8217;s fantastic article</a>.</p>
<h3>A Real World Example &#8211; It’s Easier Than it Seems!</h3>
<p>Let’s return to the real world of digital camcorders and basic video editors like iMovie and Windows Movie Maker. Go ahead and record, edit and output your movie as normal. Now, head on over to <a href="http://www.mirovideoconverter.com/">http://www.mirovideoconverter.com/</a> and download the Miro Video Converter.</p>
<p style="text-align: center"><a rel="attachment wp-att-6987" href="http://thinkvitamin.com/code/getting-started-with-html5-video/attachment/screen-shot-2010-08-15-at-19-06-36/"><img class="size-medium wp-image-6987 aligncenter" style="border: 1px solid #999" src="http://thinkvitamin.com/wp-content/uploads/2010/08/Screen-shot-2010-08-15-at-19.06.36-300x37.png" alt="" width="300" height="37" /></a></p>
<p>The Miro converter will encode <tt>.ogv</tt>, .<tt>mp4</tt> and <tt>.webm</tt> in it’s sleep, so create your three versions. Three versions seems like overkill but for now it’s the only way we can support those browsers that support HTML5 video. Strictly speaking you could forego the <tt>.webm</tt> file until IE9 is released.</p>
<p style="text-align: center"><a rel="attachment wp-att-6988" href="http://thinkvitamin.com/code/getting-started-with-html5-video/attachment/screen-shot-2010-08-15-at-19-06-51/"><img class="size-medium wp-image-6988 aligncenter" style="border: 1px solid #999" src="http://thinkvitamin.com/wp-content/uploads/2010/08/Screen-shot-2010-08-15-at-19.06.51-260x300.png" alt="" width="260" height="300" /></a></p>
<p>And finally, here’s the code we’ll use:</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="html" style="font-family:monospace;">&lt;video width=640 height=480 controls poster=myImage.jpg id=videocontent&gt;
&lt;source src=myvideo.ogv type=video/ogg&gt;
&lt;source src=myvideo.mp4 type=video/mp4&gt;
&lt;source src=myvideo.webm type=video/webm&gt;
&lt;!-- You can even put your flash video code here as a fallback for internet explorer users --&gt;
&lt;a href=myvideo.mp4&gt;Download this video.&lt;/a&gt;
&lt;/video&gt;</pre></td></tr></table></div>

<p>And there you have it, all three major formats supported. Head over into your stylesheet and apply some lovely CSS3 effects to <tt>#videocontent</tt>. Of course the video element can have any ID or class name you like.</p>
<h3>Further reading</h3>
<p>As you can probably tell HTML5 video is a huge topic and at the same time relatively easy to implement. You can keep it as simple as the code above so you can delve deeper into the world of codecs and licensing. In case you feel the need to find out more, here are some of the sites I used to put together this article.</p>
<ul>
<li><a href="http://www.webmproject.org/">http://www.webmproject.org/</a></li>
<li><a href="http://www.vorbis.com/">http://www.vorbis.com/</a></li>
<li><a href="http://mpeg.chiariglione.org/">http://mpeg.chiariglione.org/</a></li>
<li><a href="http://www.getmiro.com/">http://www.getmiro.com/</a></li>
<li><a href="http://dev.opera.com/articles/view/introduction-html5-video/">http://dev.opera.com/articles/view/introduction-html5-video/</a></li>
<li><a href="http://diveintohtml5.org/video.html">http://diveintohtml5.org/video.html</a></li>
<li><a href="http://www.youtube.com/html5">http://www.youtube.com/html5</a></li>
<li><a href="http://vimeo.com/blog:268">http://vimeo.com/blog:268</a></li>
<li><a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#video">http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#video</a></li>
<li><a href="http://my.opera.com/core/blog/2010/03/03/everything-you-need-to-know-about-html5-video-and-audio-2">http://my.opera.com/core/blog/2010/03/03/everything-you-need-to-know-about-html5-video-and-audio-2</a></li>
</ul>
<p>So what do you think? Will you be using HTML5 video on your site soon or will you stick with good ole Flash?</p>
<p>We look forward to hearing your comments and seeing how you implement HTML5 videos on your sites with JavaScript and CSS fun and trickery!</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/code/getting-started-with-html5-video/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Getting Started with WordPress Custom Menus</title>
		<link>http://thinkvitamin.com/code/getting-started-with-wordpress-custom-menus/</link>
		<comments>http://thinkvitamin.com/code/getting-started-with-wordpress-custom-menus/#comments</comments>
		<pubDate>Thu, 15 Jul 2010 11:00:39 +0000</pubDate>
		<dc:creator>Richard Shepherd</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://carsonified.com/?p=6437</guid>
		<description><![CDATA[Perhaps one of the biggest holes in WordPress prior to version 3 was the lack of custom menu support. Menus often had to be hand coded into header.php and, whilst they could be &#8216;smart&#8217;, it often let the client needing a PHP savvy developer to make alterations to the setup. That was then. Now, at [...]]]></description>
			<content:encoded><![CDATA[<p>Perhaps one of the biggest holes in WordPress prior to version 3 was the lack of custom menu support. Menus often had to be hand coded into <tt>header.php</tt> and, whilst they could be &#8216;smart&#8217;, it often let the client needing a PHP savvy developer to make alterations to the setup.</p>
<p>That was then. Now, at (long) last, we are able to give some of this control back to the client. Or, if you&#8217;re using WordPress for your own site, you&#8217;re able to take control of your menu system with a wonderful new drag-and-drop GUI rather than diving into the code.<span id="more-6437"></span></p>
<h3>New Menu System</h3>
<p>The menu system is based on <a href="http://www.woothemes.com/2010/01/the-awesome-custom-woo-navigation/">WooNavigation</a>, a custom menu system used by the ubiquitous <a href="http://www.woothemes.com/">WooThemes</a> in the latest versions of their custom framework. Although much of the code was rewritten for WordPress 3, the similarities are still clear.</p>
<p>Adii Pienaar from WooThemes recently told me:</p>
<blockquote><p>&#8220;The new menu system that WooThemes contributed is firstly long overdue and is a massive benefit to every single WordPress user. In essence, this is such a basic feature of any CMS, but due to it being a benefit to all WP users, I think this is a significant new addition.&#8221;</p></blockquote>
<p>Chris Coyier from CSS Tricks agrees:</p>
<blockquote><p>&#8220;Personally I think this is a big step forward because, together with Custom Post Types, it is migrating WordPress towards a fully fledged CMS. The more power we can give the end user, the easier it is for developers and designers with an understanding of PHP to use WordPress as a content management solution.&#8221;</p></blockquote>
<p>So, without further ado, let&#8217;s take a look at how it all works. Grab yourself a WordPress 3 install with a bunch of pages and a refreshing beverage. It&#8217;s thirsty work.</p>
<h3>Activating a Custom Menu</h3>
<p>If you&#8217;re using WordPress 3 with the (brand new) default Theme TwentyTen installed this next bit won&#8217;t apply, because it&#8217;s already set up to use custom menus, so let&#8217;s assume you&#8217;re working with an older theme or perhaps even building your own. When you log into WordPress, go to <tt>Appearance &gt; Menus</tt>.</p>
<p>If your current theme doesn&#8217;t support custom menus you&#8217;ll see this error message:</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Picture-1.png" alt="" width="297" height="132" /></p>
<p>This means we need to head over to <tt>functions.php</tt> and get our hands a little dirty with some code. Open up <tt>functions.php</tt> and add in the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'register_custom_menu'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> register_custom_menu<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
register_nav_menu<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'custom_menu'</span><span style="color: #339933;">,</span> __<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Custom Menu'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><tt>register_nav_menu</tt> has two parameters here, the first is the slug we&#8217;ll use in our code and the second is the name our menu will have in the WordPress admin area.</p>
<p>Now before we head back over to the menu screen, let&#8217;s stay in the source code and tell WordPress wear to put this menu. The most likely place will be in <tt>header.php</tt>, but of course there is no reason you can&#8217;t put a menu anywhere on your page. To keep things simple for our first menu let&#8217;s open <tt>header.php</tt> and, at whatever point makes sense in your theme, add the following line:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> wp_nav_menu<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'menu'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'custom_menu'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>With just the default &#8216;About&#8217; page in our theme, if we look at our site we&#8217;ll see the following in the source code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;</span>li<span style="color: #339933;">&gt;&lt;</span>a href<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;the-link-in-here&quot;</span> title<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;About&quot;</span><span style="color: #339933;">&gt;</span>About<span style="color: #339933;">&lt;/</span>a<span style="color: #339933;">&gt;&lt;/</span>li<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>ul<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span></pre></div></div>

<p>And that&#8217;s all there is to it! Of course, we can add in a bunch of options into that array. You can check out the details over at the <a href="http://codex.wordpress.org/Function_Reference/wp_nav_menu">WordPress codex</a> and here are a few of the more important ones explained:</p>
<ul>
<li><tt>menu</tt> &#8211; which menu to call. In our example we only have one but you can set up as many menus as you like</li>
<li><tt>container</tt> &#8211; what element is wrapped around the menu. The default is a standard <tt>&lt;div&gt;</tt> but if you&#8217;re playing with the new kids over at HTML5 you could always change this to <tt>&lt;nav&gt;</tt></li>
<li><tt>container_class</tt> &#8211; the css class given to this container, helpful for custom styling</li>
<li><tt>menu_class</tt> &#8211; the css class given to the <tt>&lt;ul&gt;</tt> that is rendered. The default is &#8216;menu&#8217;, and again this is helpful for custom styling.</li>
<li><tt>depth</tt> &#8211; How many levels should the menu display. The default is 0&#8242; which means all levels, but you can set this to perhaps 1&#8242; or 2&#8242; to limit how far a drop-down menu will render. Handy stuff!</li>
</ul>
<p>That&#8217;s probably enough for our purposes, but it&#8217;s definitely worth checking out the WordPress codex because there are many options you can set, like adding text before and after menu items and links.</p>
<h3>Using the GUI</h3>
<p>So let&#8217;s get cracking with the user interface, where much of the magic happens.</p>
<p>The first thing we need to do is create our first menu (remember, we can have multiple menus) so I&#8217;ll imaginatively create one called &#8216;Our First Menu&#8217;. Type that in to the Menu Name field and hit the Create Menu Button.</p>
<p>Did you spot the check-box &#8216;Automatically add new top-level pages&#8217;? Well, do you?</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-13.png" alt="" width="470" /></p>
<p>Normally I don&#8217;t, because I figure if we&#8217;re going to the trouble of making a custom menu we probably don&#8217;t want it doing anything automatically. After all, we want complete control over our menu, so I don&#8217;t want WordPress dropping in a new page every time we create one. Maybe you do, and that&#8217;s okay with me too :)</p>
<p>Excellent! Our first menu is empty but we&#8217;re well on our way. Over on the left we&#8217;re told that our theme currently only supports one menu so we best make sure we&#8217;re using &#8216;Our First Menu&#8217;. Select it in the drop-down, and click Save.</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-11.png" alt="" width="291" height="188" /></p>
<h3>Adding Pages to our Menu</h3>
<p>So we&#8217;ve created a place in our theme for the menu, and now we&#8217;ve created the menu itself. Trouble is, there&#8217;s probably nothing in it. WordPress gives us three easy ways to add content to the menu, and the great news is that it doesn&#8217;t have to be content that sits in the WordPress site. For example, the first box on the left you&#8217;ll see is for Custom Links.</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-9.png" alt="" width="290" height="159" /></p>
<p>You could perhaps link to your twitter account like I have in the screen-shot above, and just click &#8216;Add to Menu&#8217;. It might be your portfolio, which you&#8217;ve hosted elsewhere, or links to live client sites. It might even be links to your favourite news sites. Whatever you choose, it&#8217;s great to know your menu can take your visitors anywhere.</p>
<p>The second (and probably most often used) way to add content to the menu is in the Pages&#8217; box, under &#8216;Custom Links&#8217;.</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-8.png" alt="" width="293" height="171" /></p>
<p>As you can see, I&#8217;m working on a fresh install of WordPress 3 which gives me just two pages &#8211; Home, and About. Now &#8216;Home&#8217; strictly speaking isn&#8217;t a page in the default configuration (although you can change that). For me, &#8216;Home&#8217; is whatever resides at and in <tt>index.php</tt>. </p>
<p>Notice the tabs at the top of the &#8216;Pages&#8217; box, which allow you to hunt around through content rich sites for exactly the pages you want. If you&#8217;re setting up your menu for the first time you might want to look in the &#8216;View All&#8217; tab, or if you&#8217;re just adding in a page you might want to &#8216;Search&#8217; for it.</p>
<p>I&#8217;m going to select both Home and About so I&#8217;ve got something to play with. If I hit &#8216;Add to Menu&#8217; you can see that my menu is really starting to shape up.</p>
<p>Finally we have the &#8216;Categories&#8217; box, which acts in much the same way as the &#8216;Tabs&#8217; box. Again, as this is the default install of WordPress I only have one category, the much maligned &#8216;Uncategorized&#8217;.</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-6.png" alt="" width="288" height="147" /></p>
<p>I&#8217;m going to select it (most of my posts often accidentally end up in uncategorized!) and &#8216;Add to Menu&#8217;. This is what we now have:</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-4.png" alt="" width="479" height="366" /></p>
<p>Now before we move on you need to &#8216;Save Menu&#8217;, so go ahead and click it.</p>
<p>To prove that this has worked we can load up my simple blog (which has the TwentyTen theme installed) and marvel at it&#8217;s glory.</p>
<p><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-3.png" alt="" width="445" height="430" /></p>
<h3>Menu Hierarchy</h3>
<p>That&#8217;s cool but it&#8217;s also only the beginning. The next thing to look at is ordering your menu hierarchy. So far the menu items have just appeared in the order we added them, but we probably want to change this. The good news is that this the whole system is drag-and-drop, so go ahead and move things around. </p>
<p>What you&#8217;ll also notice is that you can create a hierarchy by dragging items onto other items. I&#8217;ve added a few more pages and created something that looks like this:</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/menu-with-hierarchy.png" alt="" width="414" height="486" /></p>
<p>You can see I&#8217;ve created a hierarchy of pages as indicated by their indents, and I&#8217;ve also changed the name of &#8216;uncategorized&#8217; to &#8216;The Blog&#8217;. If we whizz on over to the actual site, we can also see that WordPress has created a funky drop down menu for me.</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/menu-with-hierarchy-live.png" alt="" width="426" height="329" /></p>
<p>Now it&#8217;s worth noting that TwentyTen is designed to style the menu as a drop-down, and that the actual HTML WordPress generates looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div&gt;
&lt;ul id=&quot;menu-our-first-menu&quot;&gt;&lt;li id=&quot;menu-item-5&quot;&gt;&lt;a title=&quot;The home page&quot; href=&quot;http://localhost/&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
&lt;li id=&quot;menu-item-6&quot;&gt;&lt;a href=&quot;http://localhost/?page_id=2&quot;&gt;About&lt;/a&gt;
&lt;ul&gt;
&lt;li id=&quot;menu-item-23&quot;&gt;&lt;a href=&quot;http://localhost/?page_id=9&quot;&gt;Our Company&lt;/a&gt;
&lt;ul&gt;
&lt;li id=&quot;menu-item-21&quot;&gt;&lt;a href=&quot;http://localhost/?page_id=13&quot;&gt;The Team&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li id=&quot;menu-item-22&quot;&gt;&lt;a href=&quot;http://localhost/?page_id=11&quot;&gt;Our Products&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li id=&quot;menu-item-20&quot;&gt;&lt;a href=&quot;http://localhost/?page_id=15&quot;&gt;Get in Touch&lt;/a&gt;
&lt;ul&gt;
&lt;li id=&quot;menu-item-19&quot;&gt;&lt;a href=&quot;http://localhost/?page_id=17&quot;&gt;Office Locations&lt;/a&gt;&lt;/li&gt;
&lt;li id=&quot;menu-item-4&quot;&gt;&lt;a href=&quot;http://twitter.com/richardshepherd&quot;&gt;My Twitter Stream&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li id=&quot;menu-item-8&quot;&gt;&lt;a href=&quot;http://localhost/?cat=1&quot;&gt;The Blog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;</pre></div></div>

<h3>A Few More Options</h3>
<p>You should also be able to see in the Menus admin that each menu has a drop-down arrow. Click on it, and you&#8217;ll see there are a few things you can tinker with.</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-19.png" alt="" width="417" height="197" /></p>
<ul>
<li><tt>URL </tt>- The url which the menu item links to</li>
<li><tt>Navigation Label </tt>- What it will actually say in the menu</li>
<li><tt>Title Attribute </tt>- Useful for making your menu accessible, and you could also use some jQuery to grab this and create a piece of &#8216;description&#8217; text</li>
<li><tt>Remove/Cancel </tt>- No prizes here</li>
</ul>
<h3>Menu Widgets</h3>
<p>There&#8217;s one more thing we&#8217;ll look at in this introductory tutorial, and that&#8217;s the menu widget. So far all we&#8217;ve done is actually create a menu in our header and stick &#8216;Our First Menu&#8217; into that hook. There is one other thing Custom Menu&#8217;s do out-of-the-box. In the WordPress admin head on over to <tt>Appearance &gt; Widgets</tt> and we can take a look at the Custom Menu Widget.</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-18.png" alt="" width="255" height="72" /></p>
<p>It should be sitting under Available Widgets, so drag it over to one of your widgetized areas and let&#8217;s take a look at the options:</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-17.png" alt="" width="292" height="265" /></p>
<p>As you can see, there aren&#8217;t many! You can give the widget a title (I&#8217;ve called mine &#8216;Our Menu Widget&#8217;) and then select which menu you have created to use. Now we only created one menu in this tutorial, but you could of course create a second and drop it in here. Click &#8216;Save&#8217;, head on over to your site and you should see something like this:</p>
<p style="text-align: center;"><img src="http://carsonified.com/wp-content/uploads/2010/07/Google-Chrome-16.png" alt="" width="367" height="311" /></p>
<p>And that&#8217;s it! Great for a sitemap-like feature, just remember that if your menu doesn&#8217;t automatically update when you add new pages neither will this widget.</p>
<h3>Just the Beginning</h3>
<p>As you can probably tell, there&#8217;s a lot going on with the Custom Menu feature. I&#8217;ve tried to stick to the basics here and focus on the GUI, but there is a lot more going on under the hood that is also worth looking into. If you feel confident with PHP then start playing &#8211; there&#8217;s so much more you can do.</p>
<p>If all you are looking for is a simple way to create and manage menus for you and your clients, we hope this overview gets you started. As always, we welcome your comments and look forward to seeing Custom Menus on your WordPress sites!</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/code/getting-started-with-wordpress-custom-menus/feed/</wfw:commentRss>
		<slash:comments>55</slash:comments>
		</item>
		<item>
		<title>Create your first WordPress Custom Post Type</title>
		<link>http://thinkvitamin.com/code/create-your-first-wordpress-custom-post-type/</link>
		<comments>http://thinkvitamin.com/code/create-your-first-wordpress-custom-post-type/#comments</comments>
		<pubDate>Wed, 30 Jun 2010 10:00:02 +0000</pubDate>
		<dc:creator>Richard Shepherd</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://carsonified.com/?p=6257</guid>
		<description><![CDATA[Let me get something off my chest. WordPress sometimes, just ever-so occasionally, makes things sound a little harder than they actually are. Custom post types sound quite scary, but really they aren&#8217;t. Jane Wells, WordPress UI guru, commented on their name in my last article for Think Vitamin: &#8220;They just got named poorly because they [...]]]></description>
			<content:encoded><![CDATA[<p>Let me get something off my chest. WordPress sometimes, just ever-so occasionally, makes things sound a little harder than they actually are. Custom post types sound quite scary, but really they aren&#8217;t. <a href="http://jane.wordpress.com/">Jane Wells</a>, WordPress UI guru, commented on their name in <a href="http://carsonified.com/blog/design/wordpress-3-0-for-web-designers/">my last article for Think Vitamin</a>:</p>
<blockquote><p>&#8220;They just got named poorly because they live in the Posts table in the database. Think of them more like custom objects/content types.&#8221;</p></blockquote>
<p>Cool &#8211; it&#8217;s just custom content. What&#8217;s the point of that then? Can&#8217;t we just categorise blog posts and order our pages in nice hierarchies? Can&#8217;t we use conditionals to spit out different CSS stylesheets? Well yes, we <em>could</em>. But admit it – it&#8217;s not fun.</p>
<p>And hands up if you&#8217;ve ever tried to explain to a client what a custom field is. Hurts, doesn&#8217;t it? So thank goodness for wonderful people like Jane who have given us a solution.<span id="more-6257"></span></p>
<h3>What is it and What it isn&#8217;t</h3>
<p>Everyone&#8217;s talking about them. I recently asked Chris Coyier from <a href="http://css-tricks.com/">CSS-Tricks</a> what excited him about WP3 and he replied custom post types:</p>
<blockquote><p>&#8220;Think of how <a href="http://tumblr.com/">Tumblr</a> works – how you can publish photos, quotes, links and whatever else. You could create those same types now with WordPress, and build themes to support and have special styles for them. This is fantastic stuff for building custom sites!&#8221;</p></blockquote>
<p>Make sense? Well according to Jane, Chris misinterpreted their goal:</p>
<blockquote><p>&#8220;Custom post types aren&#8217;t really meant for that use […] Custom post types are great for things that are more or less catalogued: products (in an e-commerce site), listings for a real estate site, etc. For regular content creation as described [by Chris], you can already do [that] by using custom taxonomies and/or stylesheets to make post templates.&#8221;</p></blockquote>
<p>Now I&#8217;m going to go out on a limb here and say that Chris made a interesting suggestion. I say this for two reasons: firstly because I think the great thing about WordPress is we can use it in the way we feel it <em>should</em> be used. There might be a different or better way, but there is no <em>wrong </em>way to do something (unless it doesn&#8217;t work!). Secondly I refuse to believe that many designers would read up on something as scarily titled as ‘<a href="http://codex.wordpress.org/WordPress_Taxonomy">custom taxonomies</a>&#8216;.</p>
<p>Indeed I expect to see WordPress themes cropping up all over the place that mimic the way Tumblr themes look and work. <a href="http://www.woothemes.com/">WooThemes</a> have already released three.</p>
<h3>Get your Hands Dirty</h3>
<p>Now before we dig in I want to say this – custom post types make things a lot easy for non-technical people to use the WordPress admin to enter content. However the designer/developer still needs to have a reasonable grasp of PHP and needs to be prepared to get their hands dirty (unless they opt for a plugin that does the job for you like <a href="http://wordpress.org/extend/plugins/custom-post-template/">http://wordpress.org/extend/plugins/custom-post-template/</a> or <a href="http://wordpress.org/extend/plugins/custom-post-type-ui/">http://wordpress.org/extend/plugins/custom-post-type-ui/</a>)</p>
<p>For our tutorial we&#8217;re going to be editing <tt>functions.php </tt>which is your theme directory. We&#8217;re simply adding to this file, so feel free to start at the top or at the bottom – just don&#8217;t change any of the code that&#8217;s already there. For the more confident of you out there you could adapt this code into a plugin.</p>
<p>With a few edits we are going to create a custom post type for our portfolio, and a template which will use this information. We&#8217;ll end up with a new panel in the admin menu which looks like this:</p>
<p><a href="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-AdminPanel.jpg"><img style="border: 1px solid #999" src="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-AdminPanel.jpg" alt="A custom post type admin panel" /></a></p>
<p>A new overview page called &#8220;My Portfolio&#8221;:</p>
<p><a href="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Columns.jpg"><img style="border: 1px solid #999" src="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Columns.jpg" alt="Custom Post Type admin page" width="470" /></a></p>
<p>And a new place to enter our content:</p>
<p><a href="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Edit.jpg"><img style="border: 1px solid #999" src="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Edit.jpg" alt="Custom post type edit screen" width="470" /></a></p>
<h3>Step 1</h3>
<p>Here&#8217;s the first bit of code we need to add to <tt>functions.php</tt>, which I&#8217;ll review below.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'init'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'portfolio_register'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> portfolio_register<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<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> _x<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'My Portfolio'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'post type general name'</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> _x<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Portfolio Item'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'post type singular name'</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> _x<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Add New'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'portfolio item'</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 Portfolio 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 Portfolio 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 Portfolio 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 Portfolio 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 Portfolio'</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>
&nbsp;
	<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;">'menu_icon'</span> <span style="color: #339933;">=&gt;</span> get_stylesheet_directory_uri<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/article16.png'</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;">'thumbnail'</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
	register_post_type<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'portfolio'</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></pre></td></tr></table></div>

<p>Anyone who&#8217;s worked with WordPress before will recognise the structure here. We&#8217;re adding an action when the WP Admin initialises to call the function <tt>portfolio_register()</tt>. In that function we create two arrays, <tt>$labels</tt> and <tt>$args</tt>, and then use <tt><a href="http://codex.wordpress.org/Function_Reference/register_post_type">register_post_type</a></tt> to pull it all together. In doing so we name the new custom post type ‘portfolio&#8217; and tell it to use the arguments from <tt>$args</tt>.</p>
<p>The devil is in the detail, so let&#8217;s run over some of those arguments. A full list can be found at <a href="http://codex.wordpress.org/Function_Reference/register_post_type">http://codex.wordpress.org/Function_Reference/register_post_type</a>. First let&#8217;s look at <tt>$labels</tt>:</p>
<ul>
<li><strong>name </strong>– <em>this is the (probably plural) name for our new post type</em></li>
<li><strong>singular_name </strong>– <em>how you&#8217;d refer to this in the singular  (such as ‘Add new ****&#8217;) </em></li>
</ul>
<p>You can probably work out the rest of <em>$labels</em> for yourself, as they simply refer to different circumstances in which the name of your custom post type would be used.</p>
<p>And now <tt>$args</tt>:</p>
<ul>
<li><strong>public </strong>– <em>should they be shown in the admin UI </em></li>
<li><strong>show_ui </strong>– <em>should we display an admin panel for this custom post type </em></li>
<li><strong>menu_icon </strong>– <em>a custom icon for the admin panel</em></li>
<li><strong>capability_type </strong>- <em>WordPress will treat this as a ‘post&#8217; for read, edit, and delete capabilities </em></li>
<li><strong>hierarchical </strong>– <em>is it hierarchical, like pages </em></li>
<li><strong>rewrite </strong>– <em>rewrites permalinks using the slug ‘portfolio&#8217;</em></li>
<li><strong>supports </strong>– <em>which items do we want to display on the add/edit post page</em></li>
</ul>
<p>That&#8217;s the first simple step, and it should be enough to see your new custom post time in the WordPress admin. Save <tt>functions.php</tt> and take a look!</p>
<h3>Step 2</h3>
<p>The next thing we need to do is <a href="http://codex.wordpress.org/Function_Reference/register_taxonomy">register a taxonomy</a>. Or, in English, create categories for this new content type.</p>
<p>For example, in our portfolio we want to include the names of technologies and software used to create our work. I&#8217;m going to call this taxonomy ‘Skills&#8217;, and populate it with things like HTML, CSS and jQuery.</p>
<p>It&#8217;s just one line of code:</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<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Skills&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;portfolio&quot;</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;">&quot;hierarchical&quot;</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;">&quot;label&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Skills&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;singular_label&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Skill&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;rewrite&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</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 first item here is the taxonomy name, ‘Skills&#8217;. The second is the name of the object type we&#8217;re applying it to, in our case the custom post type ‘portfolio&#8217; (which is an array). Finally our arguments; you can find a full list at <a href="http://codex.wordpress.org/Function_Reference/register_taxonomy">http://codex.wordpress.org/Function_Reference/register_taxonomy</a>, and here we&#8217;re using just three which have the same meaning as described in Step 1.</p>
<p>Add that line of code in and you should now see:</p>
<p><a href="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-AdminPanel.jpg"><img style="border: 1px solid #999" src="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-AdminPanel.jpg" alt="A custom post type admin panel" /></a></p>
<p>You can then enter new ‘skills&#8217; just like you&#8217;d enter categories for blog posts. It looks like this:</p>
<p><a href="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Skills.jpg"><img style="border: 1px solid #999" src="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Skills.jpg" alt="Custom post types - add skills" width="470" /></a></p>
<h3>Step 3</h3>
<p>The third step is to add custom data fields to the add/edit post page.</p>
<p>For our portfolio we can add things like the year the piece was published and details on who designed, built and produced the site/design.</p>
<p>There&#8217;s a bit more code here, but read through it in order and it should make sense:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">add_action(&quot;admin_init&quot;, &quot;admin_init&quot;);
&nbsp;
function admin_init(){
  add_meta_box(&quot;year_completed-meta&quot;, &quot;Year Completed&quot;, &quot;year_completed&quot;, &quot;portfolio&quot;, &quot;side&quot;, &quot;low&quot;);
  add_meta_box(&quot;credits_meta&quot;, &quot;Design &amp; Build Credits&quot;, &quot;credits_meta&quot;, &quot;portfolio&quot;, &quot;normal&quot;, &quot;low&quot;);
}
&nbsp;
function year_completed(){
  global $post;
  $custom = get_post_custom($post-&gt;ID);
  $year_completed = $custom[&quot;year_completed&quot;][0];
  ?&gt;
  &lt;label&gt;Year:&lt;/label&gt;
  &lt;input name=&quot;year_completed&quot; value=&quot;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$year_completed</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&quot; /&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> credits_meta<span style="color: #009900;">&#40;</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;">$post</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$custom</span> <span style="color: #339933;">=</span> get_post_custom<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$designers</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$custom</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;designers&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$developers</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$custom</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;developers&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$producers</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$custom</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;producers&quot;</span><span style="color: #009900;">&#93;</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: #000000; font-weight: bold;">?&gt;</span>
  &lt;p&gt;&lt;label&gt;Designed By:&lt;/label&gt;&lt;br /&gt;
  &lt;textarea cols=&quot;50&quot; rows=&quot;5&quot; name=&quot;designers&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$designers</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/textarea&gt;&lt;/p&gt;
  &lt;p&gt;&lt;label&gt;Built By:&lt;/label&gt;&lt;br /&gt;
  &lt;textarea cols=&quot;50&quot; rows=&quot;5&quot; name=&quot;developers&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$developers</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/textarea&gt;&lt;/p&gt;
  &lt;p&gt;&lt;label&gt;Produced By:&lt;/label&gt;&lt;br /&gt;
  &lt;textarea cols=&quot;50&quot; rows=&quot;5&quot; name=&quot;producers&quot;&gt;<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$producers</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>&lt;/textarea&gt;&lt;/p&gt;
  <span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>First of all we call the add the admin_init function to the queue when the WordPress admin initialises, and within that function we <a href="http://codex.wordpress.org/Function_Reference/add_meta_box">add two meta boxes</a> – places to enter our data. The context for these two statements is</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span> add_meta_box<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$callback</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span><span style="color: #339933;">,</span> <span style="color: #000088;">$context</span><span style="color: #339933;">,</span> <span style="color: #000088;">$priority</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>The only difference between the two is where we place them on the screen. The ‘year completed&#8217; is placed in the sidebar using ‘side&#8217; whilst the ‘credits&#8217; are placed in the main flow of the page using ‘normal&#8217;.</p>
<p>Within the two functions there is some vanilla WordPress PHP code and HTML to help define our old friend custom fields. Make sure to include</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$post</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>…so that we can then query the current post using</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;">$custom</span> <span style="color: #339933;">=</span> get_post_custom<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Once the two new meta boxes have been added it looks like this:</p>
<p><a href="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Add.jpg"><img style="border: 1px solid #999" src="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Add.jpg" alt="Custom post types - add new post" width="470" /></a></p>
<p>The final thing to do in step 3 is to make sure we then save these values with this post. I do this with</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;">add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'save_post'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'save_details'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>and</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: #000000; font-weight: bold;">function</span> save_details<span style="color: #009900;">&#40;</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;">$post</span><span style="color: #339933;">;</span>
&nbsp;
  update_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;year_completed&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;year_completed&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  update_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;designers&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;designers&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  update_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;developers&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;developers&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  update_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;producers&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;producers&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>There&#8217;s nothing too tricky here. Again we&#8217;re adding an action, this time to the ‘save_post&#8217; event. It fires the function <tt>save_details()</tt> which uses update_post_meta (<a href="http://codex.wordpress.org/Function_Reference/update_post_meta">http://codex.wordpress.org/Function_Reference/update_post_meta</a>) to save the relevant data.</p>
<h3>Step 4</h3>
<p>A nice little touch is to rejig the layout of the My Portfolio page to display some of this information. If you&#8217;ve followed through the article so far this should all make sense to you:</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;">add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;manage_posts_custom_column&quot;</span><span style="color: #339933;">,</span>  <span style="color: #0000ff;">&quot;portfolio_custom_columns&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
add_filter<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;manage_edit-portfolio_columns&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;portfolio_edit_columns&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> portfolio_edit_columns<span style="color: #009900;">&#40;</span><span style="color: #000088;">$columns</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$columns</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">&quot;cb&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;&lt;input type=<span style="color: #000099; font-weight: bold;">\&quot;</span>checkbox<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;title&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Portfolio Title&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;description&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Description&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;year&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Year Completed&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">&quot;skills&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Skills&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$columns</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> portfolio_custom_columns<span style="color: #009900;">&#40;</span><span style="color: #000088;">$column</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;">$post</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$column</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #339933;">:</span>
      the_excerpt<span style="color: #009900;">&#40;</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;">&quot;year&quot;</span><span style="color: #339933;">:</span>
      <span style="color: #000088;">$custom</span> <span style="color: #339933;">=</span> get_post_custom<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$custom</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;year_completed&quot;</span><span style="color: #009900;">&#93;</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;">break</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;skills&quot;</span><span style="color: #339933;">:</span>
      <span style="color: #b1b100;">echo</span> get_the_term_list<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ID</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Skills'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">', '</span><span style="color: #339933;">,</span><span style="color: #0000ff;">''</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: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Here we&#8217;re adding two more functions to the WordPress Admin. The first, <tt>portfolio_edit_columns($columns)</tt>, simply defines the columns. The first two arguments &#8220;cb&#8221; and &#8220;title&#8221; are part of the core so don&#8217;t play with those too much (you can of course rename &#8220;Portfolio Title&#8221;). It&#8217;s the next three that come from our custom post type, &#8220;description&#8221;, &#8220;year&#8221; and &#8220;skills&#8221;.</p>
<p>We have one more function to tell WordPress where to get this data from &#8211; <tt>portfolio_custom_columns($column)</tt>. Using a simple switch/case we can define what data to actually show in the column layout. For the &#8220;description&#8221; we use <a href="http://codex.wordpress.org/Template_Tags/the_excerpt" target="_blank"><tt>the_excerpt()</tt></a>, for &#8220;year&#8221; we get the custom field data using <a href="http://codex.wordpress.org/Function_Reference/get_post_custom" target="_blank"><tt>get_post_custom()</tt></a>, and for &#8220;skills&#8221; we get a comma separated list of the terms/taxonomies/categories using <a href="http://codex.wordpress.org/Function_Reference/get_the_term_list" target="_blank"><tt>get_the_term_list()</tt></a>.</p>
<p>With that we have our customised columns:</p>
<p><a href="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Columns.jpg"><img style="border: 1px solid #999" src="http://carsonified.com/wp-content/uploads/2010/06/WP-CPT-Columns.jpg" alt="Custom Post Type admin page" width="470" /></a></p>
<h3>One final touch</h3>
<p>Be default custom post types will display using single.php, or index.php as a fallback. The great thing is we can create our own custom template using the filename <tt>single-xxxxxx.php</tt>. In our case this is single-portfolio.php. Just create your template file in your theme directory and the custom post type will use it. How you display the data is totally up to you.</p>
<p>Hey, did you also notice the &#8216;Featured Image&#8217; option on the add/edit page. That works out-of-the-box thanks to the  &#8216;thumbnail&#8217; part of:</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: #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;">'thumbnail'</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>You just need to make sure to include the following line of code in <tt>functions.php</tt>:</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;">add_theme_support<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'post-thumbnails'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>For more details check out this <a href="http://markjaquith.wordpress.com/2009/12/23/new-in-wordpress-2-9-post-thumbnail-images/" target="_blank">excellent post by Mark Jaquith</a>.</p>
<h3>Rewrite Problems?</h3>
<p>There are a few reported problems out there about the rewriting of custom URLs; in short, they sometimes don&#8217;t work. However, if you&#8217;re experiencing this problem there are a couple of easy fixes.</p>
<p>First, simply go to the Settings &gt; Permalinks page, which will flush the permalinks (assuming that your WordPress install can write to the .htaccess file). This should clear up most problems related to permalinks, custom post type related or not.</p>
<p>If that doesn&#8217;t work you can add a line of code <em>after </em>you register the post type:</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;">	register_post_type<span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'portfolio'</span> <span style="color: #339933;">,</span> <span style="color: #000088;">$args</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        flush_rewrite_rules<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This worked for me on a particularly problematic install.</p>
<h3>Conclusion</h3>
<p>And there we have it! That is a whistle-stop tour of custom post  types which I hope gives you the appetite to explore further.</p>
<p>The best thing is to experiment with the code, read through the  documentation in the <a href="http://codex.wordpress.org/Main_Page" target="_blank">WordPress  Codex</a>, and share your ideas with others. Like any new feature I&#8217;m  sure it will evolve as users create new and exciting ways to use it, and  we&#8217;d love to hear about how you&#8217;re using custom post types here at  Think Vitamin.</p>
<div style="width: 1px;height: 1px;overflow: hidden">
<p>function portfolio_register() {</p>
<p>$labels = array(<br />
&#8216;name&#8217; =&gt; _x(&#8216;My Portfolio&#8217;, &#8216;post type general name&#8217;),<br />
&#8216;singular_name&#8217; =&gt; _x(&#8216;Portfolio Item&#8217;, &#8216;post type singular name&#8217;),<br />
&#8216;add_new&#8217; =&gt; _x(&#8216;Add New&#8217;, &#8216;portfolio item&#8217;),<br />
&#8216;add_new_item&#8217; =&gt; __(&#8216;Add New Portfolio Item&#8217;),<br />
&#8216;edit_item&#8217; =&gt; __(&#8216;Edit Portfolio Item&#8217;),<br />
&#8216;new_item&#8217; =&gt; __(&#8216;New Portfolio Item&#8217;),<br />
&#8216;view_item&#8217; =&gt; __(&#8216;View Portfolio Item&#8217;),<br />
&#8216;search_items&#8217; =&gt; __(&#8216;Search Portfolio&#8217;),<br />
&#8216;not_found&#8217; =&gt;  __(&#8216;Nothing found&#8217;),<br />
&#8216;not_found_in_trash&#8217; =&gt; __(&#8216;Nothing found in Trash&#8217;),<br />
&#8216;parent_item_colon&#8217; =&gt; &#8221;<br />
);</p>
<p>$args = array(<br />
&#8216;labels&#8217; =&gt; $labels,<br />
&#8216;public&#8217; =&gt; true,<br />
&#8216;publicly_queryable&#8217; =&gt; true,<br />
&#8216;show_ui&#8217; =&gt; true,<br />
&#8216;query_var&#8217; =&gt; true,<br />
&#8216;menu_icon&#8217; =&gt; get_stylesheet_directory_uri() . &#8216;/article16.png&#8217;,<br />
&#8216;_builtin&#8217; =&gt; false,<br />
&#8216;rewrite&#8217; =&gt; true,<br />
&#8216;capability_type&#8217; =&gt; &#8216;post&#8217;,<br />
&#8216;hierarchical&#8217; =&gt; false,<br />
&#8216;menu_position&#8217; =&gt; null,<br />
&#8216;supports&#8217; =&gt; array(&#8216;title&#8217;,'editor&#8217;,'thumbnail&#8217;)<br />
);</p>
<p>register_post_type( &#8216;portfolio&#8217; , $args );<br />
}</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/code/create-your-first-wordpress-custom-post-type/feed/</wfw:commentRss>
		<slash:comments>191</slash:comments>
		</item>
		<item>
		<title>WordPress 3.0 for Web Designers</title>
		<link>http://thinkvitamin.com/code/wordpress-3-0-for-web-designers/</link>
		<comments>http://thinkvitamin.com/code/wordpress-3-0-for-web-designers/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 10:00:21 +0000</pubDate>
		<dc:creator>Richard Shepherd</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://carsonified.com/?p=6024</guid>
		<description><![CDATA[Editors Note: In his first article for Think Vitamin Richard Shepherd looks at what&#8217;s new in the upcoming 3.0 release of WordPress and quizzes a number of web designers on their thoughts on the new features. According to founder Matt Mullenweg, WordPress now runs an estimated 8.5% of all sites on the internet. This is [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>Editors Note:</strong> In his first article for Think Vitamin Richard Shepherd looks at what&#8217;s new in the upcoming 3.0 release of WordPress and quizzes a number of web designers on their thoughts on the new features.<br />
</em></p>
<p><a href="http://wordpress.org/development/2010/05/wordpress-3-0-release-candidate/"><img class="aligncenter size-full wp-image-6065" style="border: 1px solid #999" src="http://carsonified.com/wp-content/uploads/2010/06/wp30rc1.png" alt="" width="470" height="150" /></a></p>
<p>According to founder <a href="http://ma.tt/">Matt Mullenweg</a>, <a href="http://wordpress.org">WordPress</a> now runs an estimated 8.5% of all sites on the internet. This is a staggering amount &#8211; almost one in every ten sites is sitting on what was once just ‘blogging software’.</p>
<p>Put another way, there are 20 million eager WordPressers looking forward to the long awaited upgrade from 2.9.2 to WordPress 3.0.</p>
<p>The <a href="http://wordpress.org/development/2010/05/wordpress-3-0-release-candidate/">first release  candidate</a> is now available and most of the new features are well-known  and have been described in detail. Others, like custom post types, are leaving  some users scratching their heads. But what does the new version of WordPress  mean for the millions of web designers that use it every day?<span id="more-6024"></span></p>
<h3>A CMS by any Other Name</h3>
<p><em>&#8220;Version 3 is absolutely a step forward for designers&#8221;</em>, argues Chris Coyier from <a href="http://css-tricks.com/">CSSTricks</a>&#8221;</p>
<blockquote><p>&#8220;For one thing, I think it will finally squelch the &#8216;WordPress is a blogging platform, not a CMS&#8217; argument. With custom post types becoming a core feature, it is straight up CMS territory.&#8221;</p></blockquote>
<p>We&#8217;ll come back to custom post types later, but the point that Chris makes about WordPress becoming a true Content Management System (CMS) has lots of support.</p>
<p><a href="http://www.elliotjaystocks.com/">Elliot Jay Stocks</a> is a well-known designer who uses WordPress, and he&#8217;s also excited about this development towards a true CMS:</p>
<blockquote><p>&#8220;It&#8217;s definitely a step forward. It&#8217;s moving further and further away from its roots as a blogging engine and incorporating powerful tools that make it a viable CMS.&#8221;</p></blockquote>
<p><a href="http://perishablepress.com/">Jeff Starr</a>, who co-wrote ‘<a href="http://digwp.com/">Digging into WordPress</a>’, believes that:</p>
<blockquote><p>“WordPress 3 is custom everything, and it&#8217;s awesome. These features provide greater control and flexibility in terms of content organisation, presentation, and management. Custom content types take us beyond the days of posts and pages. Custom taxonomies take us beyond tags and categories.”</p></blockquote>
<p>Adii Pienaar from <a href="http://woothemes.com/">WooThemes</a> agrees:</p>
<blockquote><p>&#8220;I&#8217;m very excited about custom post types as this makes WordPress even more of a fully-fledged CMS”</p></blockquote>
<p>So let&#8217;s take a look at some of these features in more depth, and just how they will affect web professionals designing sites for the WordPress platform.</p>
<h3>Custom Post Types &#8211; Simpler than it Sounds</h3>
<p>Perhaps the feature that causes the most confusion, or at least the most questions, is custom post types. At its heart it&#8217;s a relatively simple concept &#8211; instead of just posts and pages you can now specify your own type of entry.</p>
<p style="text-align: center"><img class="aligncenter size-full wp-image-6054" style="border: 1px solid #999999;margin-bottom: -10px" src="http://carsonified.com/wp-content/uploads/2010/06/wp3_cpt.jpg" alt="" width="470" height="270" /></p>
<p style="text-align: center"><strong><em>Admin screen for a custom post type of &#8220;Video&#8221;</em></strong></p>
<p>Chris from CSSTricks explains the concept well:</p>
<blockquote><p>&#8220;Think of how <a href="http://tumblr.com">Tumblr</a> works – how you can publish photos, quotes, links and whatever else. You could create those same types now with WordPress, and build themes to support and have special styles for them. This is fantastic stuff for building custom sites!&#8221;</p></blockquote>
<p>Fantastic stuff indeed, and it&#8217;s interesting to hear Chris mention Tumblr. WordPress&#8217;s nimbler, younger cousin continues to grow at a phenomenal rate and has many fans out there. Indeed, some designers have ditched WordPress altogether for Tumblr.</p>
<p>Others, like Corbus Bester, have designed amazing WordPress sites that look and feel like <a href="http://fresh01.co.za/">Tumblogs</a>. Perhaps there is some mileage in this fusion of styles; it&#8217;s certainly now possible &#8211; and considerably easier &#8211; with custom post types.</p>
<p>Chris Spooner from <a href="http://www.spoongraphics.co.uk/">SpoonGraphics</a> also sees the benefit this new feature will bring:</p>
<blockquote><p>“As someone who uses WordPress for almost every website I build, I&#8217;d say it would be really useful to be able to create a new entry for a particular section of a website that&#8217;s different to the usual post and page layouts. A new portfolio entry springs to mind as a good use of this feature.”</p></blockquote>
<h3>The Community Gives Back</h3>
<p>One of the other highly publicised additions, thanks in no small part to the goodwill and marketing genius of those WooThemes folk, is the new Navigation System.</p>
<p style="text-align: center"><img class="aligncenter size-full wp-image-6057" style="border: 1px solid #999999;margin-bottom: -10px" src="http://carsonified.com/wp-content/uploads/2010/06/wp3_menus.jpg" alt="" width="470" height="263" /></p>
<p style="text-align: center"><em><strong>Admin screen for the new menu system</strong><br />
</em></p>
<p>It is, in short, a master-stoke. It also blurs the line between the open-source platform and its profit-making community. Adii Pienaar explains:</p>
<blockquote><p>&#8220;I&#8217;m happy to be a bit biased, but the new menu system that WooThemes contributed is long overdue and a massive benefit to every single WordPress user, In essence, this is such a basic feature of any CMS, but due to it being a benefit to all WordPress users I think this is a significant new addition&#8221; he adds.</p></blockquote>
<p>Chris Coyier believes:</p>
<blockquote><p>“The menu building tools are going to be huge for designers as well, and unquestioningly a step forward. Building dynamic menus in the past has involved using WordPress functions in the theme code itself with a bunch of parameters. Nerdy stuff, and not for the average site owner or even for many designers.”</p></blockquote>
<p>WooThemes designer Magnus Jepson agrees:</p>
<blockquote><p>“The navigation system will let designers concentrate on setting up the menu instead of hard coding it.”</p></blockquote>
<p>So how does it work? Chris explains:</p>
<blockquote><p>“The new menu builder puts a GUI on [menu building], putting menu control in the admin where it should be and where anybody can intuitively control it.”</p></blockquote>
<h3>Other Features</h3>
<p>Of the myriad other features on offer with version 3.0, the more noteworthy include:</p>
<ul>
<li>The long awaited integration of <a href="http://mu.wordpress.org/">WordPress MU</a> with the core WordPress code, a project that has taken the WordPress team over a year;</li>
<li>The new default theme <a href="http://2010dev.wordpress.com/">TwentyTen</a>, and the promise of a new theme every year;</li>
<li><a href="http://wpengineer.com/wordpress-3-0-specific-author-templates/">New author templates</a>, allowing the easier customisation of pages for individual authors; and</li>
<li>A new installation system, ditching the known security issue that is the ubiquitous ‘admin’ user.</li>
</ul>
<p style="text-align: center"><img class="aligncenter size-full wp-image-6053" style="border: 1px solid #999999;margin-bottom: -10px" src="http://carsonified.com/wp-content/uploads/2010/06/wp3_2010.jpg" alt="" width="470" height="260" /></p>
<p style="text-align: center"><em><strong>The new default theme &#8220;TwentyTen&#8221; home page</strong><br />
</em></p>
<h3>WordPress 3 for Designers</h3>
<p>So will these new features make a difference for designers? Adii argues:</p>
<blockquote><p><em>&#8220;Not necessarily for designers, </em><em>I think the new features are probably more geared to end-users, like the menu system, or developers, for example custom post types.&#8221;</em></p></blockquote>
<p>He has a point. The level of comfort and experience you have with PHP really does affect how much you can squeeze from the WordPress system; HTML and CSS are simply not enough. Elliot touches on this:</p>
<blockquote><p>&#8220;I hope it becomes easier to work with template files. Less PHP wizardry required and more functionality built in from the start.&#8221;</p></blockquote>
<p>Chris Spooner thinks that others will benefit from the upgrade:</p>
<blockquote><p>“I see the upgrades more suited to the everyday end user. The extra customisation options will also allow the non-tech user to add their own touch to their blog.”</p></blockquote>
<p>Others, like Jeff Starr, believe that WordPress 3 will bring benefits to designers as well<em>:</em></p>
<blockquote><p>“[It is] definitely a step forward. A huge step. In addition to better content management, its integration of WPMU makes it that much easier to create entire networks of sites with a single installation. WordPress 3 also features bulk updates for plug-ins and themes, saving users even more time.”</p></blockquote>
<p>And it’s that hacking that keeps WordPress, even version 3.0, out of the  reach of anyone without a basic grounding in PHP (and, arguably,  jQuery). Elliot argues:</p>
<blockquote><p>&#8220;It still doesn&#8217;t look as powerful as Expression Engine, But for ease-of-setup and ease-of-use, WordPress remains my favourite CMS. My only beef with WordPress is that it can take a lot of hacking to get what you want, and I&#8217;m hoping that WordPress 3 will solve this problem.&#8221;</p></blockquote>
<h3>The future for WordPress</h3>
<p>So just what does the future hold for WordPress? Is it too early to speculate what we&#8217;ll see in WordPress 4?<em></em></p>
<blockquote><p><em>&#8220;I&#8217;d actually vote for it not to develop too much,</em>&#8221; said Adii from WooThemes.<em> &#8220;WordPress is great because it has never been the most complex or complicated system to use. I am however in favour of adding functionality that will make WordPress a more complete CMS, even if that means having different ‘flavours’ of the platform i.e. blogging vs. CMS.&#8221;</em></p></blockquote>
<p>Perhaps <a href="http://wordpress.com">WordPress.com</a> will remain as the hosted solution for casual bloggers, and a ‘WPLite’ version will also become available.</p>
<p>We then might see an offshoot, the more powerful CMS version, which caters for the needs of its considerable and growing user-base of web designers looking for a back-end system.</p>
<p>It makes sense, and it could see an opportunity for <a href="http://automattic.com">Automattic</a> to further monetise part of its software-as-service stable. But who exactly would profit? And who would pay?</p>
<p>When asked about the future or WordPress, Jeff focuses on usability for designers:<em></em></p>
<blockquote><p><em>“Make it fast, make it secure, and make it easy on the server. Go for lean and mean, and deprecate the fluff that nobody uses.”</em></p></blockquote>
<p>This sentiment is echoed by Magnus:</p>
<blockquote><p><em>“I hope that WordPress will develop to be faster and more secure than it is today, and also that it adopts the most widely used functions into the core for all to use. Maybe wishful thinking to have both new functions and faster system, but everything is possible.”</em></p></blockquote>
<p>Chris Coyier also offers some thoughts about the future:<em></em></p>
<blockquote><p><em>&#8220;As far as the software itself, I hope it continues to have a big community and have a good, smart, quick core team. There are no particular ‘features’ that I&#8217;m pining for anymore, mostly just a wish that the team keeps my WordPress nice and secure as we move forward.&#8221;</em></p></blockquote>
<p>There&#8217;s a nice turn of phrase here from Chris, &#8220;My WordPress&#8221;. The wonderful thing about WordPress is that it’s us, the users, the designers and the developers that help create its ultimate direction. And it’s our creative and unique implementations of this powerful system that will guide its future development.</p>
<h3>Want More on WordPress 3.0?</h3>
<p>Over the coming weeks we will be taking a more code based look at some of the new features in WordPress 3.0. Watch out for our first article which will focus on &#8220;Custom Posts&#8221; in the next few weeks.</p>
]]></content:encoded>
			<wfw:commentRss>http://thinkvitamin.com/code/wordpress-3-0-for-web-designers/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 4.255 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2012-02-08 20:39:03 -->

