<?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>Richard Shepherd &#187; custom fields</title>
	<atom:link href="http://www.richardshepherd.com/tag/custom-fields/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardshepherd.com</link>
	<description>Making the web. Better looking.</description>
	<lastBuildDate>Fri, 04 Jun 2010 18:03:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>WordPress Custom Fields For Beginners</title>
		<link>http://www.richardshepherd.com/wordpress-custom-fields-for-beginners/</link>
		<comments>http://www.richardshepherd.com/wordpress-custom-fields-for-beginners/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 19:10:32 +0000</pubDate>
		<dc:creator>richardshepherd</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[custom fields]]></category>
		<category><![CDATA[Flickr]]></category>
		<category><![CDATA[get_post_meta]]></category>

		<guid isPermaLink="false">http://www.richardshepherd.com/?p=721</guid>
		<description><![CDATA[This is a tutorial for beginners, and we were all beginners once. To a beginner I remember custom fields looking quite pointless. I couldn&#8217;t quite grasp what they were for. Then, after building a few WordPress sites, I watched an excellent tutorial from Chris Coyier over at CSS Tricks and it really opened my eyes. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-726" title="fieldsofgold" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/fieldsofgold.jpg" alt="fieldsofgold" width="520" height="250" /></p>
<p>This is a tutorial for beginners, and we were all beginners once.</p>
<p>To a beginner I remember custom fields looking quite pointless. I couldn&#8217;t quite grasp what they were for.</p>
<p>Then, after building a few WordPress sites, I watched an excellent tutorial from Chris Coyier over at CSS Tricks and it really opened my eyes. There&#8217;s a link at the bottom of this post to Chris&#8217;s excellent video series. And that link was generated using a custom field.<br />
<span id="more-721"></span><br />
To whet your appetite, here&#8217;s an example of a search function I <a href="http://www.thet.org" target="_blank">built for THET</a> using custom fields. You can select from three options and then WordPress will search through all the posts custom fields until it finds a match.</p>
<p style="text-align: center;"><a href="http://www.thet.org" target="_blank"><img class="size-full wp-image-733 aligncenter" title="thet-search" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/thet-search.jpg" alt="thet-search" width="216" height="220" /></a></p>
<p style="text-align: left;">That&#8217;s a really powerful way to harness WordPress custom fields, but we&#8217;re going to start with something a little easier.</p>
<h3 style="text-align: left;">The project</h3>
<p style="text-align: left;">In this tutorial, we&#8217;re going to stick with something simple. All you&#8217;ll need is some basic PHP, together with feeling comfortable working with some WordPress files; specifically single.php which you&#8217;ll find in http://your-wordpress-url.com/wp-content/themes/your-theme/single.php.</p>
<p style="text-align: left;">To keep things both easy <em>and</em> hopefully useful, we&#8217;re going to create a couple of custom fields which display a credit and a link to a flickr image. I&#8217;m using it in this post! The image at the top of the field (custom <em>fields</em>, gettit?!) came thanks to a flickr user called innoxiuss. And if you scroll to the bottom of this page you&#8217;ll see a credit that looks like this:</p>
<p><img class="alignnone size-full wp-image-732" title="attribute" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/atrribute.jpg" alt="atrribute" width="218" height="27" /></p>
<p>Well that credit is generated thanks to two custom fields and a little bit of php code.</p>
<h3>What is a WordPress Custom Field?</h3>
<p>Okay. So think of a custom field as a bit of data. It can be anything at all, as long as it&#8217;s an alpha numeric string that can be stored in the WordPress database. So a custom field is not an image, it&#8217;s the URL <em>of</em> the image.</p>
<p>A custom field has two properties, a <strong>name</strong> and a <strong>value</strong>.</p>
<p>For example, the name could be <strong>image </strong>and the value could be <strong>www.yoursite.com/image.jpg</strong>.</p>
<p>But here&#8217;s the thing: <em>it doesn&#8217;t have to make that much sense</em>. The name could be <strong>fruit</strong> and the value could be <strong>apple</strong>. And as pointless as that looks, there are instances where that information could come in useful.</p>
<p>Let&#8217;s take the real world example of the flickr credit on the bottom of this page. There are actually two custom fields in there. The first is the flickr user and the second is the URL of the photo.</p>
<p>Name: <strong>flickr_attribute_url</strong><br />
Value: <strong>http://www.flickr.com/photos/innoxiuss/2665404220/</strong></p>
<p>Name: <strong>flickr_attribute_name</strong><br />
Value: <strong>innoxiuss</strong></p>
<h3>How to add a Custom Field</h3>
<p>Custom fields are added on the Edit Post page, and it looks like this&#8230;</p>
<p><img title="custom-fields-empty" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/custom-fields-empty.jpg" alt="custom-fields-empty" width="787" height="236" /></p>
<p>If you can&#8217;t see it, then scroll to the top and under &#8216;Screen Options&#8217; make sure that &#8216;Custom Fields&#8217; is ticked.</p>
<p>All you have to do is click on the &#8216;Enter new&#8217; link (in blue) and enter a name for your data under &#8216;Name&#8217;.</p>
<p>Next enter your data&#8217;s value under &#8216;Value&#8217;.</p>
<p>Finally, click on the &#8216;Add Custom Field&#8217; button and you&#8217;re done. This is what it might look like&#8230;</p>
<p><img class="alignnone size-full wp-image-723" title="adding-custom-field" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/adding-custom-field.jpg" alt="adding-custom-field" width="765" height="166" /></p>
<p>You can add as many custom fields as you like, and on some posts I have over 20. Here you can see I&#8217;ve added two, making sure that the names are <strong>unique</strong>.</p>
<p><img title="custom-fields-full" src="http://s85883.gridserver.com/wpblog/wp-content/uploads/2009/11/custom-fields-full.jpg" alt="custom-fields-full" width="766" height="186" /></p>
<p>And that&#8217;s it. You now have two custom fields each containing a snippet of data.</p>
<p>But without rolling up our selves and getting our hands dirty with some PHP, this isn&#8217;t going to do a thing.</p>
<h3>Adding the PHP</h3>
<p>Here&#8217;s the code we&#8217;ll be using. Have a read through and see if it makes sense&#8230;</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
&nbsp; <span style="color: #666666; font-style: italic;">// Check to see if this data (custom field) exists</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>get_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;">'flickr_attribute_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// It does, so let's start building our HTML!</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;flickr_attribute&quot;&gt;'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Grab the name of the flickr user from the custom field</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// called 'flickr_attribute_name'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$flickr_attribute_name</span> <span style="color: #339933;">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;get_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;">'flickr_attribute_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Grab the url of the flickr user from the custom field</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// called 'flickr_attribute_url'</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #000088;">$flickr_attribute_url</span> <span style="color: #339933;">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;get_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;">'flickr_attribute_url'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Continue with our HTML, using our new variables</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'photo courtesy of &lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flickr_attribute_url</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&quot; target=&quot;_blank&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flickr_attribute_name</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/a&gt; over at flickr'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Close of the DIV and we're done</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;&lt;!-- /.flickr attribute --&gt;'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// End of the get_post_meta if statement</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<p>Okay, let&#8217;s take a moment to go through the code.</p>
<p>First of all, make sure you have created the custom fields in your WordPress post, and that you have the names of the fields exactly right. After all, if there&#8217;s no custom field data there&#8217;s nothing to do!</p>
<p>We check for the data in the first line:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>get_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;">'flickr_attribute_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></div></td></tr></tbody></table></div>
<p>We use the WordPress function <a href="http://codex.wordpress.org/Function_Reference/get_post_meta">get_post_meta</a> to grab the value for &#8216;flickr_attribute_name&#8217;. If there&#8217;s no data there then this statement will be null and the if won&#8217;t run. If there is something in that custom field then the code will carry on executing.</p>
<p>But hold on a minute, what&#8217;s all that other stuff? Well, the get_post_meta function has the following parameters:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">get_post_meta<span style="color: #009900;">&#40;</span><span style="color: #000088;">$post_id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$key</span><span style="color: #339933;">,</span> <span style="color: #000088;">$single</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>We set our <strong>$post_id</strong> as the current post using $post->ID. Our <strong>$key</strong> is the name of the custom field which is &#8216;flickr_attribute_name&#8217;. And finally we set <strong>$single</strong> to true.</p>
<p>What is single? If it&#8217;s set to <strong>true</strong> it returns a single result as a string. If it&#8217;s set to <strong>false</strong> it returns multiple results as an array. We&#8217;ll get on to arrays in later tutorials, but for now we&#8217;re going to keep it simple. We only have one value for our flickr credit so we leave it as <strong>true</strong>.</p>
<p>So now we know we have some data, let&#8217;s carry on with the code. We put our content in a DIV so we can style it to our liking</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// It does, so let's start building our HTML!</span><br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;flickr_attribute&quot;&gt;'</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>And we also create two variables with our custom field data in. The <strong>get_post_meta</strong> function works exactly as we described it above.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">// Grab the name of the flickr user from the custom field</span><br />
<span style="color: #666666; font-style: italic;">// called 'flickr_attribute_name'</span><br />
<span style="color: #000088;">$flickr_attribute_name</span> <span style="color: #339933;">=</span><br />
<br />
&nbsp; get_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;">'flickr_attribute_name'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// Grab the url of the flickr user from the custom field</span><br />
<span style="color: #666666; font-style: italic;">// called 'flickr_attribute_url'</span><br />
<span style="color: #000088;">$flickr_attribute_url</span> <span style="color: #339933;">=</span><br />
&nbsp; get_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;">'flickr_attribute_url'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>Again, note that we set the <strong>$single</strong> parameter to <strong>true</strong>, because we know there is only one bit of data. How do we know this? Well, in this instance it&#8217;s because we&#8217;ve set the custom field up ourselves in the WordPress backend.</p>
<p>Finally, we throw together the rest of the HTML, and close off the <strong>if</strong> statement.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Continue with our HTML, using our new variables</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'photo courtesy of &lt;a href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flickr_attribute_url</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&quot; target=&quot;_blank&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$flickr_attribute_name</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/a&gt; over at flickr'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">// Close of the DIV and we're done</span><br />
&nbsp; &nbsp; &nbsp;<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;/div&gt;&lt;!-- /.flickr attribute --&gt;'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// End of the get_post_meta if statement</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
<h3>Bringing it together</h3>
<p>So we have our custom fields in WordPress and we&#8217;ve entered some data into them. We&#8217;ve also created the snippet of easy PHP which&#8217;ll read this data and display it on the page.</p>
<p>But where does it go?</p>
<p>Well, for this snippet I&#8217;ve chosen to put it on the bottom of the <strong>single.php</strong> page in the wp-content folder, as this is the page that displays single posts.</p>
<p>On my page it comes just before the <tags> section, which itself is just before the <strong>comments</strong> bit. But you could put it anywhere you want.</p>
<p>Just boot up your favourite PHP editor (I like Expresso on the Mac and, predictably, Dreamweaver on the PC) and type in the code.</p>
<p>Save your page, upload, and you should have a lovely flickr photo credit.</p>
<h3>What next?</h3>
<p>Don&#8217;t have flickr photos on your page? Well then this could be a bit pointless. But remember, this tutorial should just give you a basic idea of how custom fields work. Sure, you could have put this flickr information in the post itself but as we&#8217;ll see, we can do all kinds of funky things once we have the ability to search custom fields and display data based on their contents.</p>
<p>In the mean time there is some further reading below. Oh, and the further reading is displayed from data in custom fields!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardshepherd.com/wordpress-custom-fields-for-beginners/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
