<?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>Code Garage Blog &#187; Customization</title>
	<atom:link href="http://codegarage.com/blog/category/wordpress/customization/feed/" rel="self" type="application/rss+xml" />
	<link>http://codegarage.com/blog</link>
	<description></description>
	<lastBuildDate>Thu, 08 Mar 2012 23:26:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The WordPress $Post Object and You</title>
		<link>http://codegarage.com/blog/2011/09/the-wordpress-post-object-and-you/</link>
		<comments>http://codegarage.com/blog/2011/09/the-wordpress-post-object-and-you/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 21:59:17 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Customization]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[The Loop]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[get_post()]]></category>
		<category><![CDATA[post object]]></category>

		<guid isPermaLink="false">http://codegarage.com/blog/?p=384</guid>
		<description><![CDATA[Ah, fair $post object. Lets talk about how great you are. The What What? Post Object. WordPress stores all post data (think content, author, tags, etc) in a single object, accessible to themes and plugins on each page load. Basically, this is the place you&#8217;re going to go to if you want to get some ...]]></description>
			<content:encoded><![CDATA[<p>Ah, fair $post object.  Lets talk about how great you are.</p>
<h2>The What What?</h2>
<p>Post Object.  WordPress stores all post data (think content, author, tags, etc) in a single object, accessible to themes and plugins on each page load. Basically, this is the place you&#8217;re going to go to if you want to get some extra information about a post for whatever clever scheme you&#8217;ve got cooked up. Need to know how many &#8220;n&#8221;s are in this particular post?  Talk to the post object.  Not sure when this post was originally published?  Post object.  Looking for lawyers who ride?  Now we&#8217;re talking &#8211; but you want the <a href="http://www.lawtigers.com/">Law Tigers</a>, not the $post object.  Watch their commercials.  You won&#8217;t regret it.</p>
<h2>How do I get at this mystical post object?</h2>
<h3>Single Posts</h3>
<p>The post object is available nearly everywhere you&#8217;re likely to be looking at a post.  If you&#8217;re working with a single post page, or an actual page, you&#8217;ve got access to that post&#8217;s (or page&#8217;s) post object anywhere within your theme file.  It&#8217;s all held in a variable named $post.</p>
<h3>Multiple Posts</h3>
<p>If you&#8217;re on a page with multiple posts, things are a little trickier but not much.  If you&#8217;re inside the loop, you&#8217;ve got access to a $post object (named $post) referring to the <strong>current</strong> post in the loop.</p>
<h3>Elsewhere</h3>
<p>What if you want to get at a post that <strong>isn&#8217;t</strong> the one you&#8217;re looking at on a particular page &#8211; or what if you want to get at one from a plugin, or somewhere where there isn&#8217;t a current post explicitly defined? There&#8217;s a function for that:</p>
<pre class="brush: php">
$id = 41;
$mypost = get_post($id);
print_r($mypost);
</pre>
<p>get_post() to the rescue!  There are, however, a couple of gotchas with get_post. </p>
<ol>
<li><strong>You have to pass it a variable &#8211; not an integer</strong>.  See how in the example above, I set $id to 41, an then used $id as the argument to get_post(), instead of just saying get_post(41)?  That&#8217;s important.  If you try to pass an integer to get_post(), it will blow up.  No Joke.</li>
<li><strong>You probably shouldnt use the variable $post.</strong>  Why not?  Because WordPress uses $post all the time, and chances are you&#8217;ll either overwrite WordPress&#8217; version of $post, or they&#8217;ll overwrite yours &#8211; either way, somebody is going to be mad.  Choose something more clever than $post (like $mypost!).  I know you can do it.</li>
</ol>
<h3>What if I don&#8217;t like getting the data back as an object?  I&#8217;m an associative array kind of guy.</h3>
<p>get_post() gets you.  So do I.  There&#8217;s a second, optional argument to get_post that determines what it spits out at you.  It defaults to OBJECT, but you can also pass in ARRAY_A, or ARRAY_N to get an associative array or a numeric array version of the post data.</p>
<h2>Ok, I&#8217;ve got a post object &#8211; what kind of data is in there?</h2>
<p>Find out for yourself!  The snippet above uses the function <strong>print_r()</strong> &#8211; it will show you the structure AND data of a particular post object &#8211; very useful stuff.  Surround it in a &lt;pre&gt; tag, and you&#8217;ll be in even better shape &#8211; it will display nicely in your browser.</p>
<h3>I&#8217;m too lazy for that.  Didn&#8217;t I come here for <strong>you</strong> to teach <strong>me</strong> about this?</h3>
<p>Well played.  Here&#8217;s a quick rundown on the data the post object gives you:</p>
<table class="small">
<thead>
<tr>
<th>Property</th>
<th>Example</th>
<th>Explanation</th>
</tr>
</thead>
<tr>
<td>$post->post_title</td>
<td>The WordPress $Post Object and You</td>
<td>Title of post.</td>
</tr>
<tr>
<td>$post->post_excerpt</td>
<td>Learn about the WordPress $Post object!</td>
<td><strong>Manually created</strong> post excerpt.  If you didn&#8217;t purposefully create an excerpt on the add post page, you&#8217;re not getting anything here.</td>
</tr>
<tr>
<td>$post->post_status</td>
<td>
<ul>
<li>publish</li>
<li>pending</li>
<li>draft</li>
<li>auto-draft</li>
<li>future</li>
<li>private</li>
<li>inherit</li>
<li>trash</li>
</ul>
</td>
<td>Current status of the post.</td>
</tr>
<tr>
<td>$post->comment_status</td>
<td>
<ul>
<li>open</li>
<li>closed</li>
<li>registered_only</li>
</ul>
</td>
<td>Comment status for this particular post.</td>
</tr>
<tr>
<td>$post->ping_status</td>
<td>
<ul>
<li>open</li>
<li>closed</li>
</ul>
</td>
<td>Does the current post accept pingbacks and trackbacks?</td>
</tr>
<tr>
<td>$post->post_password</td>
<td>
123456
</td>
<td>The plaintext password for this post, if there is one.  Empty otherwise.</td>
</tr>
<tr>
<td>$post->post_name</td>
<td>
the-wordpress-post-object-and-you
</td>
<td>A normalized, sanitized version of the post title, used to generate pretty permalinks.</td>
</tr>
<tr>
<td>$post->to_ping</td>
<td>
http://technorati.com http://someothersitetoping.com
</td>
<td>Space separated list of sites to ping <strong>which have not been pinged yet</strong>.  Modified by &#8220;Send Trackbacks&#8221; field on add post page.</td>
</tr>
<tr>
<td>$post->pinged</td>
<td>
http://technorati.com http://someothersitetoping.com
</td>
<td>Space separated list of sites already pinged for this post.</td>
</tr>
<tr>
<td>$post->post_modified</td>
<td>
2011-09-15 21:21:59
</td>
<td>Time this post was last modified, based on local server time. MySQL timestamp format.</td>
</tr>
<tr>
<td>$post->post_modified_gmt</td>
<td>
2011-09-15 21:21:59
</td>
<td>Time this post was last modified, based on GMT timezone. MySQL timestamp format.</td>
</tr>
<tr>
<td>$post->post_content_filtered</td>
<td>
Post Content
</td>
<td>This field is designed to hold a version of the post for caching, in situations where filters are being run on the post that are &#8220;expensive&#8221; (slow), and undesireable to run every time.  Not used in WP core, may be used by plugins.</td>
</tr>
<tr>
<td>$post->post_parent</td>
<td>
0
</td>
<td>ID of the parent post.  Posts that have parents are generally revisions, or attachments.  If the post_parent is 0, this is a bona-fide, original post.</td>
</tr>
<tr>
<td>$post->guid</td>
<td>
<p>http://codegarage.com/blog/?p=41</p>
</td>
<td>Global Unique Identifier for the post.  According to the documentation page on wordpress.org, this can&#8217;t be relied on to actually work as a link to the post, but I&#8217;m not totally sure why.  Maybe in cases where the site url has changed?</td>
</tr>
<tr>
<td>$post->menu_order</td>
<td>
0
</td>
<td>Integer determining the order in a list of posts.  Generally used for pages in menus, but can be used by plugins for special ordering.</td>
</tr>
<tr>
<td>$post->post_type</td>
<td>
<ul>
<li>post</li>
<li>page</li>
<li>attachment</li>
</ul>
</td>
<td>The particular type of post this is.  Attachments are generally images, pdfs, etc.  Posts and pages are self explanatory.</td>
</tr>
<tr>
<td>$post->mime_type</td>
<td>
image/png
</td>
<td>Mime type for attachments.</td>
</tr>
<tr>
<td>$post->comment_count</td>
<td>
14
</td>
<td>Number of comments on this post currently.</td>
</tr>
<tr>
<td>$post->post_ancestors</td>
<td>
array
</td>
<td>Array of parent posts for this post.</td>
</tr>
</table>
<h2>Whew!</h2>
<p>I think that about covers it.  One last question you might have:<br />
<a href="http://codegarage.com/blog/2009/05/how-to-display-properly-formatted-content-from-a-post-object-in-wordpress/" title="How to Display Properly Formatted Content From a $post Object in WordPress">How do I get WordPress to spit out properly formatted post content?</a></p>
<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://codegarage.com/blog/2011/09/the-wordpress-post-object-and-you/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Customizing WordPress:  an Introduction</title>
		<link>http://codegarage.com/blog/2011/03/customizing-wordpress-an-introduction/</link>
		<comments>http://codegarage.com/blog/2011/03/customizing-wordpress-an-introduction/#comments</comments>
		<pubDate>Mon, 07 Mar 2011 16:00:42 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Customization]]></category>
		<category><![CDATA[Tweaking Your Theme]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Premium themes]]></category>
		<category><![CDATA[Themes]]></category>
		<category><![CDATA[Wordpress Customization]]></category>

		<guid isPermaLink="false">http://yourcodegarage.com/blog/?p=274</guid>
		<description><![CDATA[WordPress is an amazingly flexible, powerful content management system &#8211; it can do a lot of things for a lot of different people, and can can be contorted to match the requirements of impossibly different businesses. All for free. Right, but I just want to change my header Of course. Lets talk about how you ...]]></description>
			<content:encoded><![CDATA[<p>WordPress is an amazingly flexible, powerful content management system &#8211; it can do a lot of things for a lot of different people, and can can be contorted to match the requirements of impossibly different businesses.  All for free.</p>
<h3>Right, but I just want to change my header</h3>
<p>Of course.  Lets talk about how you can go about making small customizations to your WordPress based site. There are routes to take for people of all ability levels &#8211; so don&#8217;t be afraid if you&#8217;re still not exactly sure what &#8220;hosting&#8221; means.</p>
<p>I&#8217;m going to give 2 paths here, one for people who don&#8217;t know html and css from arugula, and one for those who do, even if it&#8217;s just barely.</p>
<h3>The easy way out: Choosing a flexible theme</h3>
<p>Over the past 12-18 months, the WordPress community has exploded with a new class of themes &#8211; heavily customizable and easy to use, even for those with little or no html and css ability.  I&#8217;m going to talk about 3 themes here &#8211; 2 paid, and one free.  Feel free to chime in in the comments with your personal favorite.</p>
<h3>Headway</h3>
<p><a href="http://headwaythemes.com"><img src="http://yourcodegarage.com/blog/wp-content/uploads/2011/03/Screen-shot-2011-03-06-at-3.08.20-PM.png" alt="" title="Screen shot 2011-03-06 at 3.08.20 PM" width="191" height="88" class="alignleft size-full wp-image-280" /></a>Headway is my favorite.  It&#8217;s not free, in fact, it&#8217;s not even cheap &#8211; but in my opinion, their visual editor is really fantastic &#8211; and just what a beginning WordPress user needs to get them up and running with a great looking, customized theme.  <a href="http://headwaythemes.com">Click here to visit Headway</a>.</p>
<h3>Thesis</h3>
<p><a href="http://diythemes.com"><img src="http://yourcodegarage.com/blog/wp-content/uploads/2011/03/Screen-shot-2011-03-06-at-3.08.31-PM.png" alt="" title="Screen shot 2011-03-06 at 3.08.31 PM" width="217" height="57" class="alignleft size-full wp-image-281" /></a>Thesis has been around since the beginning of paid themes.  It&#8217;s hugely popular and has a rabid following.  It&#8217;s not my favorite for a number of reasons, but how wrong can 35,000 people be?  <a href="http://diythemes.com">Click here to visit Thesis</a></p>
<h3>Atahualpa</h3>
<p><a href="http://wordpress.bytesforall.com/?page_id=40"><img src="http://yourcodegarage.com/blog/wp-content/uploads/2011/03/Screen-shot-2011-03-06-at-3.11.57-PM.png" alt="" title="Screen shot 2011-03-06 at 3.11.57 PM" width="206" height="72" class="alignleft size-full wp-image-282" /></a>Atahualpa is another tricky one.  It&#8217;s very popular, and refreshingly, it&#8217;s free!  However, there is a pretty steep learning curve that comes along with it.  If you&#8217;ve got the patience to power through the learning stage, you&#8217;ll no doubt be very excited with the end product. <a href="http://wordpress.bytesforall.com/?page_id=40">Click here to visit the Atahualpa homepage</a></p>
<h3>The Road Less Travelled &#8211; Modifying theme code yourself</h3>
<p>The other option is a little less glamourous, and a lot more migraine inducing, but the possibilities are limitless.  WordPress works on a templating system that is both easy to use, and robust enough to let you do anything you want it to.  The kicker is:  you need to learn how HTML, CSS, and even a little bit of PHP if you&#8217;re going to make this work.  You don&#8217;t exactly need to be an expert in any of them to make small changes to an existing theme though.  Here&#8217;s the quick overview, and some links to get you pointed in the right direction:</p>
<h3>WordPress Theme Files</h3>
<p>Nearly every WordPress theme consists of the same basic set of files, listed here:</p>
<ul>
<li><strong>index.php</strong> Index.php is your theme&#8217;s go-to guy.  Traditionally, it controls the homepage (although if your theme has a file called &#8220;home.php&#8221;, that&#8217;s what will control your homepage).   When all else fails, index.php is the file that WordPress looks to to generate your page.</li>
<li><strong>header.php</strong> This one isn&#8217;t too tough.  Header.php controls &#8211; wait for it &#8211; your header.  In most themes, every single page on your site will share a header &#8211; so if you&#8217;re looking to add an image into your header, this is a good place to start your search.</li>
<li><strong>footer.php</strong> Another no brainer.  Looking for that hard to find copyright text?  Start checking here.</li>
<li><strong>sidebar.php</strong> More easy stuff &#8211; here&#8217;s where your sidebar code traditionally goes.</li>
<li><strong>single.php</strong> This file is in charge of single post pages &#8211; like the one you&#8217;re looking at right now.</li>
<li><strong>page.php</strong> This file is in charge of &#8220;Page&#8221; pages.  </li>
<li><strong>archive.php</strong> This file controls the layout of your archive pages &#8211; for example, the pages displayed when you click the &#8220;March 2011&#8243; link on this blog, or one of the category links in the sidebar.  A warning &#8211; categories are sometimes controlled by another file (category.php).</li>
<li><strong>style.css</strong> Here&#8217;s where things get interesting.  The style.css file is usually the single source of all CSS (which controls the actual look and feel of your blog &#8211; things like colors, layout, fonts, etc) on your theme.  Get comfortable with this file, and the CSS that it uses.</li>
</ul>
<p>That is, unfortunately, not even close to a complete list.  Fortunately, there&#8217;s a <a href="http://codex.wordpress.org/Template_Hierarchy">handy diagram at this page</a> on wordpress.org.</p>
<p>Now, you at least know where to start looking &#8211; all that&#8217;s left is to figure out what to do once you get there. Here are a couple of great tutorials to get you on your way:<br />
<a href="http://www.w3schools.com/html/default.asp">HTML Tutorial</a><br />
<a href="http://www.csstutorial.net/">CSS Tutorial</h3>
]]></content:encoded>
			<wfw:commentRss>http://codegarage.com/blog/2011/03/customizing-wordpress-an-introduction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using a Custom User Table Share Users Between Two WordPress Installs</title>
		<link>http://codegarage.com/blog/2009/04/using-a-custom-user-table-share-users-between-two-wordpress-installs/</link>
		<comments>http://codegarage.com/blog/2009/04/using-a-custom-user-table-share-users-between-two-wordpress-installs/#comments</comments>
		<pubDate>Wed, 29 Apr 2009 02:11:26 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Admin]]></category>
		<category><![CDATA[Customization]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[custom_user_meta_table]]></category>
		<category><![CDATA[custom_user_table]]></category>
		<category><![CDATA[usermeta]]></category>
		<category><![CDATA[wp_usermeta]]></category>
		<category><![CDATA[wp_users]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=162</guid>
		<description><![CDATA[In one of many, many strokes of genius, the WordPress core developers threw in a bit of code to allow users (ok, other developers) to define a custom user table, and a custom usermeta table for a WordPress install. In it&#8217;s simplest form, you can point your WordPress user table at that of another blog ...]]></description>
			<content:encoded><![CDATA[<p>In one of many, many strokes of genius, the WordPress core developers threw in a bit of code to allow users (ok, other developers) to define a custom user table, and a custom usermeta table for a WordPress install.</p>
<p>In it&#8217;s simplest form, you can point your WordPress user table at that of another blog on your server.  Doing so means both blogs (and why stop at 2?) share user information:  passwords, usernames, author bios, etc etc.  If you&#8217;ve got a site that requires numerous different, separate WordPress installs (think something along the lines of <a href="http://www.wired.com/blogs/">wired.com</a>), but the same authors often write on many or all of them, this is a great, and easy solution.</p>
<h3>Enough Already, Give Me the Code</h3>
<p>Yes Sir.  Just slap this code in your wp-config file:</p>
<pre class="brush: php">

define(&#039;CUSTOM_USER_TABLE&#039;,&#039;new_user_table&#039;);
define(&#039;CUSTOM_USER_META_TABLE&#039;, &#039;new_usermeta_table&#039;);
</pre>
<p>It&#8217;s pretty self explanatory from there &#8211; just define a custom user table, and a custom usermeta table, and you&#8217;re good to go.  Want your users to share login info across different blogs, but be able to have a different bio for each one?  Define a custom user table, but leave the default user_meta table.  Everything stored in the users table (ID, login, password, nicename, email, url, and display name, among other things) will stay the same across all blogs.  Everything else (nickname, user level, First Name, Last Name, and many other things), will be blog specific.</p>
]]></content:encoded>
			<wfw:commentRss>http://codegarage.com/blog/2009/04/using-a-custom-user-table-share-users-between-two-wordpress-installs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating User Friendly Custom Fields by Modifying the Post Page</title>
		<link>http://codegarage.com/blog/2009/03/creating-user-friendly-custom-fields-by-modifying-the-post-page/</link>
		<comments>http://codegarage.com/blog/2009/03/creating-user-friendly-custom-fields-by-modifying-the-post-page/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 03:57:12 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Customization]]></category>
		<category><![CDATA[Functions]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Writing Plugins]]></category>
		<category><![CDATA[add_meta_box()]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[Edit Post]]></category>
		<category><![CDATA[Post Page]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=78</guid>
		<description><![CDATA[Ralph over at ForTheLose.org wrote up a great post on how to use custom fields in wordpress a few days ago, and I thought I&#8217;d expand on it with another feature that I use for clients quite often.  Custom fields are a great way to store extra post information, and they can really make for ...]]></description>
			<content:encoded><![CDATA[<p>Ralph over at <a href="http://forthelose.org" target="_blank">ForTheLose.org</a> wrote up a great post on <a href="http://forthelose.org/how-to-use-custom-fields-in-wordpress" target="_blank">how to use custom fields in wordpress</a> a few days ago, and I thought I&#8217;d expand on it with another feature that I use for clients quite often.  Custom fields are a great way to store extra post information, and they can really make for some more interesting layouts.</p>
<p>Sometimes, however, they can be a little much for a non-tech savvy client or user to handle &#8211; they&#8217;ve got to choose the right custom field from the dropdown, or type it in properly, or it won&#8217;t work &#8211; and what if you&#8217;ve got a few specific option values you&#8217;d like them to choose from for the custom field?  Sometimes it&#8217;s just too risky to expect the user to be able to handle it.</p>
<p>With this in mind, I started looking into how to modify the Add Post and Edit Post pages (ok, they&#8217;re really the same page).  As it turns out, its not too terribly difficult to add in a few options of your own on the post page, giving the user attractive, easy to use access to more advanced features of your theme or plugin.  A few examples:</p>
<h3>Allowing the user to choose his own sidebars</h3>
<p><a href="http://yourcodegarage.com/blog/wp-content/uploads/2009/03/picture-3.png"><img class="size-full wp-image-83 alignleft" title="picture-3" src="http://yourcodegarage.com/blog/wp-content/uploads/2009/03/picture-3.png" alt="picture-3" width="292" height="308" /></a>This one was a fun project for me &#8211; I had a client who wanted to  have a different sidebar on his &#8220;testimonials&#8221; page (he was a consultant).  Now, I started to just set up a new post template for him, but I realized that he really wasnt going to want to call me every time he wanted to add a new testimonial &#8211; that would be ridiculous.  I had really helped sell him on the idea of using wordpress so he wouldn&#8217;t need to call me a few times a month to make small changes to his website &#8211; so forcing him to do so to change his testimonials wouldnt be acceptable.</p>
<p>After giving that some thought, I decided to make another widgetized sidebar for use with the testimonials page template.  This way, he could add text widgets to just that sidebar.  I got to work, and got it set up, and decided that wasn&#8217;t quite right either &#8211; what if down the road he decided he wanted to show those testimonials on a sales page, or some other page I hadn&#8217;t thought of?  He&#8217;d be out of luck.  I decided what I really wanted to do was set up a number of sidebars (I ended up with 6), and allow him to choose the sidebars (it was a 3 column theme) for each page.  Custom Fields were the perfect fit for the job, but that meant he&#8217;d have to remember the name of each of the sidebars when he wanted to change them.  A custom post options box seemed just the right fit.  And it did &#8211; he was really excited about the functionality, and I was excited to have learned something new.</p>
<h3>Required Post Info for a Theme Site</h3>
<p><a href="http://yourcodegarage.com/blog/wp-content/uploads/2009/03/picture-22.png"><img class="alignright size-full wp-image-80" title="picture-22" src="http://yourcodegarage.com/blog/wp-content/uploads/2009/03/picture-22.png" alt="picture-22" width="299" height="524" /></a>Another project tasked me with creating a wordpress theme site for a client.  He wanted each theme post to have a number of fields relating to its layout and structure &#8211; things like number of columns, if it was widgetized, etc.  I decided that the easy way would be to just put the info into the post body, maybe in some custom tags or something &#8211; but wouldn&#8217;t it be cool if users could search by this info?  I got to work.  What I ended up with was a custom post options box with all the relevant theme info &#8211; dropdowns and checkboxes, which removed any possibility of error from user input.  The client was happy, and as always, I had a good time doing it.</p>
<h3>Implementation</h3>
<p>Before we get to how to actually implement this, a word of warning:  with great power comes great responsibility.  In this case, you&#8217;ve got great power to annoy your users &#8211; if you&#8217;re releasing a plugin with minor functionality that probably doesnt need a decision made on a per-post basis, it&#8217;s probably best to leave it off of the post page.  There is no sense in cluttering and slowing down the post page if it isn&#8217;t completely necessary.</p>
<p>The actual implementation of this isn&#8217;t too hairy &#8211; we&#8217;ve basically got 4 parts:</p>
<ol>
<li>Tell WordPress what we want</li>
<li>Display the options box</li>
<li>Retrieve the options</li>
<li>Insert them into custom fields</li>
</ol>
<h3>Finding Room on the Post Page with add_meta_box()</h3>
<p>First things first &#8211; we&#8217;ve got to reserve a spot on the oh-so-exclusive edit post page.  Here&#8217;s how we do it:</p>
<pre class="brush: php">
add_action(&#039;admin_menu&#039;, &#039;my_post_options_box&#039;);

function my_post_options_box() {
add_meta_box(&#039;post_info&#039;, &#039;Post Information&#039;, &#039;custom_post_info&#039;, &#039;post&#039;, &#039;side&#039;, &#039;high&#039;);
}
</pre>
<p>First things first &#8211; we hook into the admin_menu action, with our callback function my_post_options_box().  With this callback function, we initiate the actual box &#8211; with the add_meta_box() call.  Add meta box works like this:</p>
<p>add_meta_box(&#8216;id&#8217;, &#8216;title&#8217;, &#8216;callback&#8217;, &#8216;page&#8217;, &#8216;context&#8217;, &#8216;priority&#8217;)</p>
<ul>
<li>id &#8211; This is the identifier for your box.  Choose wisely.</li>
<li>title &#8211; This holds the string that will display to the user, so make it informative, and properly formatted.</li>
<li>callback &#8211; This references the function that is actually going to display your  option box.</li>
<li>page &#8211; This determines where your post box shows up and your options are post, page, or link.    As far as I know, its not possible to call more than a single option here &#8211; so if you want your box on posts and pages, you&#8217;ll need to make the call twice.</li>
<li>context &#8211; Where your post box will end up &#8211; your options are normal, advanced, and side.  Side shows up on the right side (only available since 2.7), obviously, and, as far as I can tell, both normal and advanced show up below the post box.</li>
<li>priority &#8211; Where (vertically) your box will show up.  Your options are high and low, low putting your option at the bottom of the other boxes, and high putting it at the top.  Both screenshots shown above are set to side and high priority.</li>
</ul>
<h3>Displaying Your Options Box</h3>
<p>Now we&#8217;re going to actually display the options on the page:</p>
<pre class="brush: php">
function custom_post_info() {
global $post;
?&gt;
&lt;fieldset id=&quot;mycustom-div&quot;&gt;
&lt;div&gt;
&lt;p&gt;
&lt;label for=&quot;cpi_dropdown_options&quot; &gt;Dropdown Options:&lt;/label&gt;&lt;br /&gt;
&lt;select name=&quot;cpi_dropdown_options&quot; id=&quot;cpi_dropdown_options&quot;&gt;
&lt;option&lt;?php selected( get_post_meta($post-&gt;ID, &#039;cpi_dropdown_options&#039;, true), &#039;Option 1&#039; ); ?&gt;&gt;Option 1&lt;/option&gt;
&lt;option&lt;?php selected( get_post_meta($post-&gt;ID, &#039;cpi_dropdown_options&#039;, true), &#039;Option 2&#039; ); ?&gt;&gt;Option 2&lt;/option&gt;
&lt;option&lt;?php selected( get_post_meta($post-&gt;ID, &#039;cpi_dropdown_options&#039;, true), &#039;Option 3&#039; ); ?&gt;&gt;Option 3&lt;/option&gt;
&lt;/select&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;label for=&quot;cpi_text_option&quot;&gt;Text Option:&lt;/label&gt;&lt;br /&gt;
&lt;input type=&quot;text&quot; name=&quot;cpi_text_option&quot; id=&quot;cpi_text_option&quot; value=&quot;&lt;?php echo get_post_meta($post-&gt;ID, &#039;cpi_text_option&#039;, true); ?&gt;&quot;&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/fieldset&gt;
&lt;?php
}
</pre>
<p>Nothing too fancy going on here &#8211; but a couple of important points &#8211; we grab the $post global at the top &#8211; we do this so we can check against already stored values in the database (for editing posts instead of creating them).   The entire section is wrapped in a fieldset, a div, and a paragraph &#8211; this ensures that the display matchces the rest of the boxes.  Other than that, its a pretty standard form, we&#8217;ve got a dropdown menu and a text box waiting to be populated.</p>
<h3>Retrieving the Options and Inserting Them Into Custom Fields</h3>
<p>We&#8217;ve had the user set the options, all that is left is to retrieve them on &#8220;Save Draft&#8221; or &#8220;Publish&#8221;, and put them where they really belong &#8211; in custom fields.  This is a little trickier than I expected it to be (and maybe I&#8217;ve made it too difficult.  If somebody can think of a way to simplify this, please let me know).</p>
<pre class="brush: php">
add_action(&#039;save_post&#039;, &#039;custom_add_save&#039;);
function custom_add_save($postID){
// called after a post or page is saved
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}

if ($_POST[&#039;cpi_dropdown_options&#039;]) {
update_custom_meta($postID, $_POST[&#039;cpi_dropdown_options&#039;], &#039;cpi_dropdown_options&#039;);
}
if ($_POST[&#039;cpi_text_option&#039;]) {
update_custom_meta($postID, $_POST[&#039;cpi_text_option&#039;], &#039;cpi_text_option&#039;);
}
}
</pre>
<p>The first thing we do is hook into the save_post action.  This is called on both saving and publishing, so we should be covered.  Our custom_add_save takes a post id as input from the save_post action, and we need it to tell wordpress where to save our options.</p>
<p>Next, we need to figure out if the post id we got matches a real post, or just a revision.  Revisions are stored in the database right alongside posts, with their own id and all &#8211; so we use the <a href="http://xref.yourcodegarage.com/blog/2.7.1/wp-includes/post.php.source.html#l3400" target="_blank">wp_is_post_revision</a> function to determine if we&#8217;re working with a revision.  If we are,  <a href="http://xref.yourcodegarage.com/blog/2.7.1/wp-includes/post.php.source.html#l3400" target="_blank">wp_is_post_revision</a> conveniently hands off the id of the parent post for us to use.</p>
<p>Now we&#8217;ll check to see if our options were posted.    If we find a $_POST option matching one of our fields, we use that to update the custom field relating to it &#8211; but since we have to do this a few times, I&#8217;ve sent the actual work of updating the custom fields to another function &#8211; update_custom_meta().  Here, we check if the post_meta (custom field) is already set &#8211; if it is, we just update it.  If it isn&#8217;t, we add a new one.</p>
<h3>All Together Now</h3>
<pre class="brush: php">

// ===================
// = POST OPTION BOX =
// ===================

add_action(&#039;admin_menu&#039;, &#039;my_post_options_box&#039;);

function my_post_options_box() {
add_meta_box(&#039;post_info&#039;, &#039;Post Information&#039;, &#039;custom_post_info&#039;, &#039;post&#039;, &#039;side&#039;, &#039;high&#039;);
}

//Adds the actual option box
function custom_post_info() {
global $post;
?&gt;
&lt;fieldset id=&quot;mycustom-div&quot;&gt;
&lt;div&gt;
&lt;p&gt;
&lt;label for=&quot;cpi_dropdown_options&quot; &gt;Dropdown Options:&lt;/label&gt;&lt;br /&gt;
&lt;select name=&quot;cpi_dropdown_options&quot; id=&quot;cpi_dropdown_options&quot;&gt;
&lt;option&lt;?php selected( get_post_meta($post-&gt;ID, &#039;cpi_dropdown_options&#039;, true), &#039;Option 1&#039; ); ?&gt;&gt;Option 1&lt;/option&gt;
&lt;option&lt;?php selected( get_post_meta($post-&gt;ID, &#039;cpi_dropdown_options&#039;, true), &#039;Option 2&#039; ); ?&gt;&gt;Option 2&lt;/option&gt;
&lt;option&lt;?php selected( get_post_meta($post-&gt;ID, &#039;cpi_dropdown_options&#039;, true), &#039;Option 3&#039; ); ?&gt;&gt;Option 3&lt;/option&gt;
&lt;/select&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;label for=&quot;cpi_text_option&quot;&gt;Text Option:&lt;/label&gt;&lt;br /&gt;
&lt;input type=&quot;text&quot; name=&quot;cpi_text_option&quot; id=&quot;cpi_text_option&quot; value=&quot;&lt;?php echo get_post_meta($post-&gt;ID, &#039;cpi_text_option&#039;, true); ?&gt;&quot;&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;/fieldset&gt;
&lt;?php
}

add_action(&#039;save_post&#039;, &#039;custom_add_save&#039;);
function custom_add_save($postID){
// called after a post or page is saved
if($parent_id = wp_is_post_revision($postID))
{
$postID = $parent_id;
}

if ($_POST[&#039;cpi_dropdown_options&#039;]) {
update_custom_meta($postID, $_POST[&#039;cpi_dropdown_options&#039;], &#039;cpi_dropdown_options&#039;);
}
if ($_POST[&#039;cpi_text_option&#039;]) {
update_custom_meta($postID, $_POST[&#039;cpi_text_option&#039;], &#039;cpi_text_option&#039;);
}
}

function update_custom_meta($postID, $newvalue, $field_name) {
// To create new meta
if(!get_post_meta($postID, $field_name)){
add_post_meta($postID, $field_name, $newvalue);
}else{
// or to update existing meta
update_post_meta($postID, $field_name, $newvalue);
}
}
?&amp;gt;
</pre>
<p style="text-align: center;"><a href="http://yourcodegarage.com/blog/wp-content/uploads/2009/03/picture-5.png"><img class="aligncenter size-full wp-image-93" title="picture-5" src="http://yourcodegarage.com/blog/wp-content/uploads/2009/03/picture-5.png" alt="picture-5" width="600" height="380" /></a></p>
<p>And there you have it.  Quick, go make something interesting!</p>
]]></content:encoded>
			<wfw:commentRss>http://codegarage.com/blog/2009/03/creating-user-friendly-custom-fields-by-modifying-the-post-page/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

