<?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>Apartment One Six &#187; Wordpress</title>
	<atom:link href="http://apartmentonesix.com/category/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://apartmentonesix.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 Dec 2009 22:59:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to Display Properly Formatted Content From a $post Object in WordPress</title>
		<link>http://apartmentonesix.com/2009/05/how-to-display-properly-formatted-content-from-a-post-object-in-wordpress/</link>
		<comments>http://apartmentonesix.com/2009/05/how-to-display-properly-formatted-content-from-a-post-object-in-wordpress/#comments</comments>
		<pubDate>Fri, 22 May 2009 02:49:05 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Writing Plugins]]></category>
		<category><![CDATA[$post]]></category>
		<category><![CDATA[the_content]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=185</guid>
		<description><![CDATA[Occasionally, I&#8217;ll run into a situation where I need to call a post specifically using the get_post() function.  get_post() returns a Post object, with a number of member variables &#8211; the one that stores the actual content of the post being post_content.
However, if you try just echoing $post-&#62;post_content, you&#8217;ll get completely unformatted text &#8211; which [...]]]></description>
			<content:encoded><![CDATA[<p>Occasionally, I&#8217;ll run into a situation where I need to call a post specifically using the <a title="Wordpress get_post() codex page" href="http://codex.wordpress.org/Function_Reference/get_post" target="_blank">get_post()</a> function.  get_post() returns a Post object, with a number of member variables &#8211; the one that stores the actual content of the post being post_content.</p>
<p>However, if you try just echoing $post-&gt;post_content, you&#8217;ll get completely unformatted text &#8211; which is frustrating, because WordPress uses a great visual text editor on the backend &#8211; it&#8217;s a shame to not take advantage of it.  Fortunately, WordPress provides a filter to display the text just as it would be displayed in the the_content() function inside the loop.  Here&#8217;s how it works:</p>
<p>To get the content formatted properly, we need to apply a filter (which is different from adding a filter &#8211; here, we&#8217;re registering it so that other filter functions can modify it before it&#8217;s displayed).</p>
<pre class="brush: php">

apply_filters(&#039;the_content&#039;, $post-&gt;post_content);
</pre>
<p>So, we&#8217;re just applying the the_content filter to the $post-&gt;post_content variable.  Doing so means that any plugin which adds a filter function to the_content like this:</p>
<pre class="brush: php">

add_filter(&#039;the_content&#039;, &#039;my_content_manipulator&#039;);

function my_content_manipulator($content){

//do stuff here

return $content;

}
</pre>
<p>will be applied to your variable.  Fortunately, it also means that the WordPress core functions which format the post content correctly can also work on it.</p>
]]></content:encoded>
			<wfw:commentRss>http://apartmentonesix.com/2009/05/how-to-display-properly-formatted-content-from-a-post-object-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Get the Current Admin Page Title in a Plugin</title>
		<link>http://apartmentonesix.com/2009/05/get-the-current-admin-page-title-in-a-plugin/</link>
		<comments>http://apartmentonesix.com/2009/05/get-the-current-admin-page-title-in-a-plugin/#comments</comments>
		<pubDate>Sat, 16 May 2009 20:41:03 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Admin]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Writing Plugins]]></category>
		<category><![CDATA[get_admin_page_title]]></category>
		<category><![CDATA[plugins]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=177</guid>
		<description><![CDATA[Here&#8217;s something that comes up for me pretty often:  depending on what I&#8217;m trying to do, I sometimes need plugin functions to run (or not run) simply based on which admin page the user is visiting.   Here&#8217;s the WordPress function that will give you the title of the currently loaded admin page:


get_admin_page_title()

So, if you only [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s something that comes up for me pretty often:  depending on what I&#8217;m trying to do, I sometimes need plugin functions to run (or not run) simply based on which admin page the user is visiting.   Here&#8217;s the WordPress function that will give you the title of the currently loaded admin page:</p>
<pre class="brush: php">

get_admin_page_title()
</pre>
<p>So, if you only want to run a bit of code on the &#8220;Edit Posts&#8221; page, you&#8217;d put together a conditional statement like this:</p>
<pre class="brush: php">

if(get_admin_page_title() == &#039;Edit Posts&#039;){
//do something fancy
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://apartmentonesix.com/2009/05/get-the-current-admin-page-title-in-a-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to Hide Certain Custom Fields From the Edit Post Page</title>
		<link>http://apartmentonesix.com/2009/05/how-to-hide-certain-custom-fields-from-the-edit-post-page/</link>
		<comments>http://apartmentonesix.com/2009/05/how-to-hide-certain-custom-fields-from-the-edit-post-page/#comments</comments>
		<pubDate>Sat, 16 May 2009 04:22:32 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Admin]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Writing Plugins]]></category>
		<category><![CDATA[Custom Fields]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[Post Meta]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=167</guid>
		<description><![CDATA[Post meta is easily one of the most useful features in WordPress from a developer&#8217;s perspective.  Adding custom content to a post provides the ability to accomplish countless goals quickly and easily.
Sometimes, it makes sense to give the user access to a post meta field &#8211; for example, if they need to add a thumbnail [...]]]></description>
			<content:encoded><![CDATA[<p>Post meta is easily one of the most useful features in WordPress from a developer&#8217;s perspective.  Adding custom content to a post provides the ability to accomplish countless goals quickly and easily.</p>
<p>Sometimes, it makes sense to give the user access to a post meta field &#8211; for example, if they need to add a thumbnail url to a post, they&#8217;re probably going to need to access to those post meta fields.  Other times, having the post meta field visible is only likely to confuse the visitor.  One (extremely popular) place that I&#8217;ve seen this is the All in One SEO Plugin.  All in One SEO is wildly popular, and does what it claims well.  It provides a few extra fields, and a nice, easy to use interface to get at them.  Here&#8217;s what it looks like when you get started:</p>
<p><a href="http://apartmentonesix.com/wp-content/uploads/2009/05/picture-31.png"><img class="aligncenter size-large wp-image-172" title="picture-31" src="http://apartmentonesix.com/wp-content/uploads/2009/05/picture-31-500x166.png" alt="picture-31" width="500" height="166" /></a>Fantastic.  It&#8217;s attractive, easy to use, and works well.  So we save our post, and then later on, we go back to edit the meta description.  Here&#8217;s what we get:</p>
<p><a href="http://apartmentonesix.com/wp-content/uploads/2009/05/picture-4.png"><img class="aligncenter size-large wp-image-173" title="picture-4" src="http://apartmentonesix.com/wp-content/uploads/2009/05/picture-4-500x494.png" alt="picture-4" width="500" height="494" /></a>At this point, its pretty clear what&#8217;s going on if you understand the inner workings of WordPress &#8211; AIOSEO has saved our values to custom fields, so now they&#8217;re showing up there.  However, most users dont understand, or even care about how AIOSEO works, or what custom fields are.  All they know is that strange stuff is showing up, and they&#8217;re not sure where to edit their meta keywords, because they&#8217;re showing up in 2 places.  Most people have the gumption to just change one and see what happens &#8211; but there will always be users who get scared and decide they want to email you about the issue, or worse, just uninstall the plugin and move on.</p>
<h3>Underscores to the Rescue</h3>
<p>The WordPress developers, fortunately, thought of this.  In fact, they store all kinds of stuff that they don&#8217;t want the user to see in custom fields &#8211; things like the last time the post was edited, who is currently editing it, and a few others.  A quick look at the database, reveals this:<a href="http://apartmentonesix.com/wp-content/uploads/2009/05/picture-5.png"><img class="aligncenter size-full wp-image-174" title="picture-5" src="http://apartmentonesix.com/wp-content/uploads/2009/05/picture-5.png" alt="picture-5" width="406" height="43" /></a>Notice a trend?  The mysterious custom field key values are prepended with an underscore.  Give it a try &#8211; enter a new custom field from the edit-post page, and enter a name that starts with an underscore &#8211; like _thumbnail, or _meta_keywords.  Hit &#8220;Add Custom Field&#8221;, and it disappears &#8211; but if you check the database, its right where it should be.</p>
<p>Now get out there and start hiding things from your users!</p>
]]></content:encoded>
			<wfw:commentRss>http://apartmentonesix.com/2009/05/how-to-hide-certain-custom-fields-from-the-edit-post-page/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using a Custom User Table Share Users Between Two WordPress Installs</title>
		<link>http://apartmentonesix.com/2009/04/using-a-custom-user-table-share-users-between-two-wordpress-installs/</link>
		<comments>http://apartmentonesix.com/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 on [...]]]></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://apartmentonesix.com/2009/04/using-a-custom-user-table-share-users-between-two-wordpress-installs/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How To:  Get an Overview of all Action and Filter Hooks</title>
		<link>http://apartmentonesix.com/2009/04/how-to-get-an-overview-of-all-action-and-filter-hooks/</link>
		<comments>http://apartmentonesix.com/2009/04/how-to-get-an-overview-of-all-action-and-filter-hooks/#comments</comments>
		<pubDate>Sat, 18 Apr 2009 03:17:46 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Writing Plugins]]></category>
		<category><![CDATA[$wp_filter]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=150</guid>
		<description><![CDATA[Here&#8217;s a very cool little tidbit I found today (strangely enough, I found it in the bbpress codebase, while working on a bbpress plugin..)  Wordpress keeps track of which function/filter combinations are registered, along with which action hooks have been called at any point.  If you&#8217;re having trouble trying to figure out which action hook [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a very cool little tidbit I found today (strangely enough, I found it in the bbpress codebase, while working on a bbpress plugin..)  Wordpress keeps track of which function/filter combinations are registered, along with which action hooks have been called at any point.  If you&#8217;re having trouble trying to figure out which action hook you should use to run some code for your plugin or theme, give this a try (on a test install, of course).</p>
<h3>$wp_filter()</h3>
<p>As far as I can tell, this holds every currently registered function/filter combination, and all the relevant information.  I can&#8217;t quite figure out exactly what determines which filters this grabs &#8211; I&#8217;ve tried calling it from a few places, and the results are slightly different, but I can&#8217;t seem to find a pattern.  I initially assumed that you wouldnt be able to see about admin side hooks from the front end, but this isn&#8217;t the case.  At any rate, you get lots of information from anywhere. Not getting the info you want?  Call it from somewhere else!  This is a great debugging tool if you&#8217;re having trouble with plugin incompatibility &#8211; it gives you a list of the filters and actions being called,the matching functions that are working on them, the priority specified for each hook call, the accepted args, and even the order in which same-priority-level hooks are called.  An example:</p>
<pre class="brush: php">
    [the_title] =&gt; Array
        (
            [10] =&gt; Array
                (
                    [wptexturize] =&gt; Array
                        (
                            [function] =&gt; wptexturize
                            [accepted_args] =&gt; 1
                        )

                    [convert_chars] =&gt; Array
                        (
                            [function] =&gt; convert_chars
                            [accepted_args] =&gt; 1
                        )

                    [trim] =&gt; Array
                        (
                            [function] =&gt; trim
                            [accepted_args] =&gt; 1
                        )

                )

        )
</pre>
<p>This is a small snippet I pulled after displaying the contents of this variable in the footer of a theme.  As you can see, the referenced hook is a filter, specifically &#8220;the_title&#8221;.  It has 3 functions attached to it: wptexturize, convert_chars, and trim.  Each one is at priority level 10 (which is the default), but they are called in the order listed.   Each accepts 1 argument.</p>
<h3>Try it Out</h3>
<p>If you want to have a look yourself, display the contents of wp_filter with this code snippet:</p>
<pre class="brush: php">
&lt;?php
global $wp_filter;
print_r($wp_filter);
?&gt;
</pre>
<p>This is going to dump the contents right to the screen, so you don&#8217;t want to do it on a live blog.  As with any use of the print_r() function, the results will be formatted nicely in the source code &#8211; but not in the rendered html.</p>
<p>Roundup:<br />
<a href="http://www.shankrila.com/tech-stuff/top-wordpress-plugins/" title="Top &#038; Best 65+ WordPress Plugins | ShanKri-la" target="_blank">Top wordpress plugins</a><br />
<a href="http://www.pingable.org/20-of-the-best-free-magazine-wordpress-themes/" title="wordpress magazine theme" target="_blank">wordpress magazine theme</a></p>
]]></content:encoded>
			<wfw:commentRss>http://apartmentonesix.com/2009/04/how-to-get-an-overview-of-all-action-and-filter-hooks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Wordpress Plugin:  Default Post Content</title>
		<link>http://apartmentonesix.com/2009/04/new-wordpress-plugin-default-post-content/</link>
		<comments>http://apartmentonesix.com/2009/04/new-wordpress-plugin-default-post-content/#comments</comments>
		<pubDate>Mon, 13 Apr 2009 16:28:06 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Admin]]></category>
		<category><![CDATA[Plugin Releases]]></category>
		<category><![CDATA[default post]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[post content]]></category>
		<category><![CDATA[Post Meta]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=144</guid>
		<description><![CDATA[Justin over at justintadlock.com made a post a few days ago about how to preset text in the WordPress post editor.  It&#8217;s a great post, with an interesting filter detailed.  In the comments, somebody mentioned that they&#8217;d like to be able to preset custom fields as well &#8211; something that seems like it shouldn&#8217;t work [...]]]></description>
			<content:encoded><![CDATA[<p>Justin over at <a title="Justin Tadlock" href="http://justintadlock.com" target="_blank">justintadlock.com</a> made a post a few days ago about how to <a href="http://justintadlock.com/archives/2009/04/05/how-to-preset-text-in-the-wordpress-post-editor" target="_blank">preset text in the WordPress post editor</a>.  It&#8217;s a great post, with an interesting filter detailed.  In the comments, somebody mentioned that they&#8217;d like to be able to preset custom fields as well &#8211; something that seems like it shouldn&#8217;t work (Custom fields need a post id to work on, and new posts dont have a post id).  Yesterday, the workaround hit me like a slap in the face while I was in the shower &#8211; so I decided to package up this, along with the original code that Justin published in a plugin.</p>
<p>It&#8217;s not the most elegant piece of code in the world, but it works on all the installs I&#8217;ve tried it on.  I&#8217;ll try to put up a post detailing how it works soon, but in the meantime, feel free to download the plugin and give it a try.</p>
<p><a title="Default Post Content Plugin" href="http://apartmentonesix.com/plugins/default-post-content/">Default Post Content Plugin</a></p>
]]></content:encoded>
			<wfw:commentRss>http://apartmentonesix.com/2009/04/new-wordpress-plugin-default-post-content/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Saving Wordpress Plugin Options &#8211; Admin Panels Done Right</title>
		<link>http://apartmentonesix.com/2009/04/saving-wordpress-plugin-options-admin-panels-done-right/</link>
		<comments>http://apartmentonesix.com/2009/04/saving-wordpress-plugin-options-admin-panels-done-right/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 02:54:53 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Admin]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Writing Plugins]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=120</guid>
		<description><![CDATA[This isn&#8217;t a terribly difficult task &#8211; it&#8217;s incredibly common, and it&#8217;s covered multiple places in the codex.  So why am I writing about it?  A few months ago, I was tasked with customizing a plugin &#8211; one that someone else wrote.  I need to add some options to it, and their [...]]]></description>
			<content:encoded><![CDATA[<p>This isn&#8217;t a terribly difficult task &#8211; it&#8217;s incredibly common, and it&#8217;s covered multiple places in the codex.  So why am I writing about it?  A few months ago, I was tasked with customizing a plugin &#8211; one that someone else wrote.  I need to add some options to it, and their code was baffling to me.  I could see that they were using custom options, but I couldnt understand how they were being set &#8211; it was like the author just made some form fields and hoped for the best.</p>
<h3>More Wordpress Magic</h3>
<p>What was happening is referenced here (Right in the codex &#8211; apparently the last place I thought to look).   My first instinct has always been to post forms on an admin page to itself, then handle checking for the $_POST values, and creating/updating the appropriate options.  As is often the case, WordPress makes it even easier than that:</p>
<h3>Set Up Your Form Properly</h3>
<p>I wont get into the boring details &#8211; you can look at the codex page for that &#8211; but pay special attention to the hidden fields you set up:</p>
<pre class="brush: html">

&lt;input name=&quot;action&quot; type=&quot;hidden&quot; value=&quot;update&quot; /&gt;
&lt;input name=&quot;page_options&quot; type=&quot;hidden&quot; value=&quot;new_option_name,some_other_option,option_etc&quot; /&gt;
</pre>
<p>Not too tough to understand &#8211; the first line tells the system that we&#8217;re going to be updating, and the second line lets WordPress know which fields we want to create/update.  These, of course, need to match the &#8220;name&#8221; attribute of the input elements up above.</p>
<h3>But What About Arrays?</h3>
<p>I posted a few days ago about the <a title="Maybe Serialize and the Magic of WordPress" href="http://apartmentonesix.com/2009/03/maybe_serialize-and-the-magic-of-wordpress/">very cool functionality of maybe_serialize()</a>, and its relation to WordPress options.  I often store arrays in options, because they help keep the database free of unnecessary clutter, and they make your plugin even easier to use (from a developers view).  I was a little worried that this &#8220;page options&#8221; trick would prevent me from saving arrays &#8211; but alas, WordPress saved the day again:</p>
<pre class="brush: php">

&lt;input type=&quot;text&quot; name=&quot;fruit_colors[&#039;apple&#039;]&quot; value=&quot;red&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;fruit_colors[&#039;carrot&#039;]&quot; value=&quot;orange&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;fruit_colors[&#039;banana&#039;]&quot; value=&quot;yellow&quot;&gt;
</pre>
<p>Just make a form field an array, like you would with a set of checkboxes (by adding [] after it).  Pass in the proper values to the page_option hidden field, like so:</p>
<pre class="brush: html">

&lt;input name=&quot;page_options&quot; type=&quot;hidden&quot; value=&quot;fruit_colors&quot; /&gt;
</pre>
<p>This can get pretty interesting &#8211; a few days ago I used to to loop through categories and associate a string with each one &#8211; you can do the same for posts, days of the week, whatever suits your plugin.</p>
<p>So there you go.  You&#8217;ve got yourself an option without any tinkering with $_POST variables, or checking if you need to update or create an option.  Wordpress even takes care of serializing it for you.  What a world&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://apartmentonesix.com/2009/04/saving-wordpress-plugin-options-admin-panels-done-right/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating User Friendly Custom Fields by Modifying the Post Page</title>
		<link>http://apartmentonesix.com/2009/03/creating-user-friendly-custom-fields-by-modifying-the-post-page/</link>
		<comments>http://apartmentonesix.com/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://apartmentonesix.com/wp-content/uploads/2009/03/picture-3.png"><img class="size-full wp-image-83 alignleft" title="picture-3" src="http://apartmentonesix.com/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://apartmentonesix.com/wp-content/uploads/2009/03/picture-22.png"><img class="alignright size-full wp-image-80" title="picture-22" src="http://apartmentonesix.com/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.apartmentonesix.com/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.apartmentonesix.com/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://apartmentonesix.com/wp-content/uploads/2009/03/picture-5.png"><img class="aligncenter size-full wp-image-93" title="picture-5" src="http://apartmentonesix.com/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://apartmentonesix.com/2009/03/creating-user-friendly-custom-fields-by-modifying-the-post-page/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Maybe_Serialize and the Magic of Wordpress</title>
		<link>http://apartmentonesix.com/2009/03/maybe_serialize-and-the-magic-of-wordpress/</link>
		<comments>http://apartmentonesix.com/2009/03/maybe_serialize-and-the-magic-of-wordpress/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 15:33:52 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Functions]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Array]]></category>
		<category><![CDATA[Object]]></category>
		<category><![CDATA[Options]]></category>
		<category><![CDATA[Post Meta]]></category>
		<category><![CDATA[Serialize]]></category>
		<category><![CDATA[User Meta]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=72</guid>
		<description><![CDATA[As you may have noticed in a post I made a few days ago about widetizing wordpress plugins,  I mentioned that we needed to serialize our variables before we created them as options in wordpress.  As it turns out, this isn&#8217;t the case &#8211; WordPress does it for you when you go to update or [...]]]></description>
			<content:encoded><![CDATA[<p>As you may have noticed in a post I made a few days ago about <a href="http://apartmentonesix.com/2009/03/widgetizing-a-wordpress-plugin-example-widget-code/">widetizing wordpress plugins</a>,  I mentioned that we needed to serialize our variables before we created them as options in wordpress.  As it turns out, this isn&#8217;t the case &#8211; WordPress does it for you when you go to update or create an option.  This morning, while reading a<a href="http://wpengineer.com/wordpress-working-with-options/" target="_blank"> post on wpengineer.com</a> about wordpress options, I noticed that in their sample code, they weren&#8217;t serializing arrays or objects before creating them as options.  <a href="http://wpengineer.com/about/" target="_blank">Michael</a>, the author, set me straight on why this is ok.</p>
<h3>Maybe Serialize?</h3>
<p>As it turns out, WordPress is one step ahead of me.  <a href="http://xref.apartmentonesix.com/2.7.1/wp-includes/functions.php.source.html#l795" target="_blank">Maybe_serialize()</a> is called a number of times throughout wordpress, relating to these items:</p>
<ul>
<li>Post Meta:  It&#8217;s called on <a href="http://xref.apartmentonesix.com/2.7.1/wp-includes/post.php.source.html#l497" target="_blank">add_post_meta()</a> and <a href="http://xref.apartmentonesix.com/2.7.1/wp-includes/post.php.source.html#l612" target="_blank">update_post_meta()</a>.</li>
<li>User Meta:  It&#8217;s called on <a href="http://xref.apartmentonesix.com/2.7.1/wp-includes/user.php.source.html#l323" target="_blank">update_usermeta()</a></li>
<li>Options:  And, of course, its called on <a href="http://xref.apartmentonesix.com/2.7.1/wp-includes/functions.php.source.html#l557" target="_blank">add_option()</a> and <a href="http://xref.apartmentonesix.com/2.7.1/wp-includes/functions.php.source.html#l487" target="_blank">update_option()</a></li>
</ul>
<p>So there you go.  Use all the arrays and objects you want in post meta, user meta, and options &#8211; throw caution to the wind, and put them straight in.  Wordpress will handle the boring stuff.</p>
]]></content:encoded>
			<wfw:commentRss>http://apartmentonesix.com/2009/03/maybe_serialize-and-the-magic-of-wordpress/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Running Wordpress Admin Functions from a Cron Job</title>
		<link>http://apartmentonesix.com/2009/03/running-wordpress-admin-functions-from-a-cron-job/</link>
		<comments>http://apartmentonesix.com/2009/03/running-wordpress-admin-functions-from-a-cron-job/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 05:12:09 +0000</pubDate>
		<dc:creator>Peter</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[Writing Plugins]]></category>
		<category><![CDATA[admin functions]]></category>
		<category><![CDATA[cron jobs]]></category>
		<category><![CDATA[wp-admin]]></category>

		<guid isPermaLink="false">http://apartmentonesix.com/?p=68</guid>
		<description><![CDATA[The quick version?  Include wp-load.php and wp-admin/admin-functions.php in a file in the plugin directory, and point your cron there.  For those of you with nothing important to do, a long winded explanation:
I ran into a project a few days ago where the client needed a simple plugin put together &#8211; read an xml file, pull [...]]]></description>
			<content:encoded><![CDATA[<p>The quick version?  Include wp-load.php and wp-admin/admin-functions.php in a file in the plugin directory, and point your cron there.  For those of you with nothing important to do, a long winded explanation:</p>
<p>I ran into a project a few days ago where the client needed a simple plugin put together &#8211; read an xml file, pull some information from it, and turn it into a post.  Initially, the client wanted to just write straight to the database, but considering we&#8217;ve got access to a number of functions perfectly suited for the job, I knew things would be much easier if we turned it into a simple plugin, and ran the built in functions from the admin side.</p>
<p>The plugin came together quite nicely &#8211; RSS read, categories created, post inserted, all like a charm.  It occurred to me at that point that the client wanted this to run automatically at set intervals &#8211; he wanted to schedule a cron job to handle it.  However, since the functions I needed to run exist on the admin side, I assumed I&#8217;d have to jump through some serious hoops to get a cron to run it.</p>
<p>I formulated a plan, and was all ready to implement it &#8211; the plan was to access wp-admin/options.php with the proper get parameters to run the function.  It turns out, using curl, you can set a cookie, and then use it to access another page &#8211; very cool (big thanks to <a href="http://www.russellheimlich.com/blog/how-to-set-up-postie-with-wordpress/" target="_blank">this post from Russell Heimlich</a> for that;  I&#8217;m sure more thorough documentation is out there, but his is the post I found).</p>
<p>I still wasnt crazy about the idea though &#8211; the extra input with the cron meant more work for the client, more possibility for things to go wrong, and more time spent on support for me.  So I decided to see if I could just include the right files from outside the admin section, and poof &#8211; it worked.  Here&#8217;s how it works:</p>
<p>You&#8217;ve got to include 2 files to get access to admin side functions:  First, wp-load.php.  wp-load.php gets everything set up, and fires up wordpress.  However, we&#8217;re calling this function from the plugin folder, inside the content directory (as opposed to the admin directory) &#8211; so when wp-load is called, we&#8217;re not going to be in the admin section, and we&#8217;re not going to get access to those functions.  On the bright side, we also don&#8217;t have to deal with WordPress forcing you to login.  Since we still need those admin functions, include wp-admin/admin-functions.php.  This loads up the admin side and gives us access to the admin functions &#8211; and we&#8217;re set to go.</p>
<p>So that&#8217;s it.  Include those  2 files, call wp_insert_post() (or whatever you need to call), and make the client happy.</p>
<p>Off topic &#8211; if you&#8217;re tired of expensive web hosts, I&#8217;ve got a friend who is starting to host for people inexpensively:  check out his post on <a href="http://www.untwistedvortex.com/2009/06/26/cheap-web-hosting-not-cheap-web-hosting/" title="Cheap Web Hosting which isn't Cheap Web Hosting" target="_blank">cheap web hosting</a> here.</p>
]]></content:encoded>
			<wfw:commentRss>http://apartmentonesix.com/2009/03/running-wordpress-admin-functions-from-a-cron-job/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
