<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>CogDogBlog &#187; wordpress</title>
	<atom:link href="http://cogdogblog.com/tag/wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://cogdogblog.com</link>
	<description>Alan Levine&#039;s space for barking about and playing with technology</description>
	<lastBuildDate>Sat, 26 May 2012 00:01:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
		<item>
		<title>When WordPress Has Memory Woes</title>
		<link>http://cogdogblog.com/2011/05/22/wordpressmemory-woes/</link>
		<comments>http://cogdogblog.com/2011/05/22/wordpressmemory-woes/#comments</comments>
		<pubDate>Mon, 23 May 2011 05:42:23 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=6841</guid>
		<description><![CDATA[cc licensed ( BY NC ND ) flickr photo shared by .m for matthijs I&#8217;m hoping to carve out some time soon to get back to the garage and return to tinkering with WordPress. To that end, I spent some time categorizing new and old posts, so I have a collection now of things blogged about using WordPress at http://cogdogblog.com/wp. Here at CogDogBlog, I&#8217;ve had some challenges with what I think is memory issues- lots of 404/not found on admin pages, and worse, strongs of deadly 500 Internal Server errors for visitors. Not good. No, not good. My web server error logs are full of the not very helpful &#8220;premature end of script headers&#8221; for index.php. You could spend years combing through the google results for this. What follows is not a definitive answer to this situation, just some guesses and results of my attempts to make it stop. To [...]]]></description>
			<content:encoded><![CDATA[<p><a title="I used to be a Spaceman" href="http://flickr.com/photos/matthijs/2296191441/"><img src="http://farm4.static.flickr.com/3281/2296191441_5ccf5925c9.jpg" /></a><br /><small><a title="I used to be a Spaceman" href="http://flickr.com/photos/matthijs/2296191441/">cc licensed ( BY NC ND )  flickr photo</a> shared by <a href="http://flickr.com/people/matthijs/">.m for matthijs</a></small></p>
<p>I&#8217;m hoping to carve out some time soon to get back to the garage and return to tinkering with WordPress. To that end, I spent some time categorizing new and old posts, so I have a collection now of things blogged about using WordPress at <a href="http://cogdogblog.com/wp">http://cogdogblog.com/wp</a>.</p>
<p>Here at CogDogBlog, I&#8217;ve had some challenges with what I think is memory issues- lots of 404/not found on admin pages, and worse, strongs of deadly 500 Internal Server errors for visitors. Not good. No, not good. My web server error logs are full of the not very helpful &#8220;premature end of script headers&#8221; for index.php. You could spend years <a href="http://www.google.com/search?q=wordpress+%22Premature+end+of+script+headers%22">combing through the google results for this</a>. </p>
<p>What follows is not a definitive answer to this situation, just some guesses and results of my attempts to make it stop. To fast forward, WordPress often needs more memory for PHP to do its work that the default settings allow. You can adjust it if you have a self-hosted site.</p>
<p>&#8220;Premature end of script headers&#8221; means (I think) that PHP died before it could finish returning information; often it did manage to complete the task at hand. Shared hosting sites usually rus a service that kills jobs that are eating too much memory. But the same error can occur from whacky or corrupt plugins or even templates.</p>
<p>After some reading and poking around, I got a sense my blog was dealing with running out of memory- I got some good ideas from a post on <a href="http://blog.openinworld.com/2011/03/wordpress-shared-hosting-performance/">WordPress Shared Hosting Performance</a> and a string of comments <a href="http://wordpress.org/support/topic/backend-errors-premature-end-of-script-headers">on a premature script headers part of the WordPress Codex</a>.</p>
<p>From these posts, I gave the <a href="http://wordpress.org/extend/plugins/tpc-memory-usage/faq/">TPC! Memory Usage plugin</a> a spin. At a first cut, it provides stats on your blog&#8217;s memory usage and settings (though this info is readily available elsewhere (simplest is a basic php script that uses the <a href="http://php.net/manual/en/function.phpinfo.php">phpinfo function</a> to output environment settings; best not to leave this on a server as it reveals path info that can be used by ne&#8217;er do wells).</p>
<p>But what the plugin does is alert you when your site is using more memory for PHP that it is allocated&#8211; and I got a flood of these, indicating that my site was regularly using a tad over the default 32 Mb of memory.</p>
<p>The key is to know how much memory your server can allocate to php &#8212; in my case, the max was 90 Mb. The fix is pretty easy, an edit to my wp-config.php file and add</p>
<p><pre><pre>
define(&#039;WP_MEMORY_LIMIT&#039;, &#039;76M&#039;);
</pre></pre></p>
<p>I changed the options in the TPC! Memory Usage plugin to message me only when the memory exceeded 64, but I still has getting emails when it went over 32 Mb (adding a specific email address seemed to clear this up), but once I had made the changes, I just de-activated the plugin. </p>
<p>Before that I was seeing than the typical usage on my site was about 35 Mb, some 15% of the total (90) that is technically available.</p>
<p>As a rule, its best to be sparing with plugins, they all add load to your site. </p>
<p>After this change I saw a drastic decrease in the &#8220;premature script headers&#8221; message in my error logs.</p>
<p>I am still having an occasional issue with the blog going under buried by 500 Internal server messages, and am trying to isolate what is happening. Oddly, the only way to clear it is to go in the database, and edit the wp_options entry for active plugins to use one less plugin, save, and then return it to the previous values (I know, that sound crazy, but it works- maybe it is some clash between server errors and W3 Total Cache??).</p>
<p>I have no idea if I am correct about any of this, but it does seem that 32Mb as the default setting for WordPress memory is borderline, or at least it is for my blog.</p>
<p>But its no fun when your memory fails! It HURTS!</p>
<p><a title="That's life ..." href="http://flickr.com/photos/frozenminds/348086684/"><img src="http://farm1.static.flickr.com/128/348086684_36f7ab37e0.jpg" /></a><br /><small><a title="That's life ..." href="http://flickr.com/photos/frozenminds/348086684/">cc licensed ( BY NC ND )  flickr photo</a> shared by <a href="http://flickr.com/people/frozenminds/">frozenminds</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2011/05/22/wordpressmemory-woes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Stuffing ds106 into an Archive</title>
		<link>http://cogdogblog.com/2011/04/26/stuffing-ds106/</link>
		<comments>http://cogdogblog.com/2011/04/26/stuffing-ds106/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 16:06:12 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Blog Pile]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[ds106]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=6694</guid>
		<description><![CDATA[Woah, it is ending? ds106 is almost over? Sigh. Well, sincr i have blog posts dribbled over the last few months, I thought about organizing it a bit better here in the doghouse- it&#8217;s not quite archiving, but it does feel like the suitcase needs help to get closed. cc licensed ( BY NC SD ) flickr photo shared by demandaj What I talk about below in likely un-reproducible code is what I did to generate http://cogdogblog.com/tag/ds106 Doing this has been part of some other WordPress tinkering around here. The current template here has 2 widgetized sidebars, and I shifted the rightmost one to be the &#8220;standard&#8221; one that appears on all pages. The left side one, by default, will have blog related things, but I also now use it on a few templates to create more relevant bits. To allow for a new widget, I edited my functions,php template [...]]]></description>
			<content:encoded><![CDATA[<p>Woah, it is ending? <a href="http://ds106.us/">ds106</a> is almost over? Sigh. Well, sincr i have blog posts dribbled over the last few months, I thought about organizing it a bit better here in the doghouse- it&#8217;s not quite archiving, but it does feel like the suitcase needs help to get closed.</p>
<p><a title="Stuffed" href="http://flickr.com/photos/demandaj/4965148290/"><img src="http://farm5.static.flickr.com/4125/4965148290_a11ae20914.jpg" /></a><br /><small><a title="Stuffed" href="http://flickr.com/photos/demandaj/4965148290/">cc licensed ( BY NC SD )  flickr photo</a> shared by <a href="http://flickr.com/people/demandaj/">demandaj</a></small></p>
<p>What I talk about below in likely un-reproducible code is what I did to generate <a href="http://cogdogblog.com/tag/ds106">http://cogdogblog.com/tag/ds106</a><br />
<div id="attachment_6695" class="wp-caption alignnone" style="width: 510px"><a href="http://cogdogblog.com/wp-content/uploads/2011/04/ds106-archive.jpg"><img src="http://cogdogblog.com/wp-content/uploads/2011/04/ds106-archive-500x320.jpg" alt="" title="ds106 archive" width="500" height="320" class="size-medium wp-image-6695" /></a><p class="wp-caption-text">(click for full size image)</p></div></p>
<p>Doing this has been part of some other WordPress tinkering around here. The current template here has 2 widgetized sidebars, and I shifted the rightmost one to be the &#8220;standard&#8221; one that appears on all pages. The left side one, by default, will have blog related things, but I also now use it on a few templates to create more relevant bits.</p>
<p>To allow for a new widget, I edited my functions,php template for the place where it generates 2 sidebars:</p>
<p><pre><pre>
if ( function_exists(&#039;register_sidebar&#039;) )
&nbsp;&nbsp;&nbsp;&nbsp;register_sidebars(2);
</pre></pre></p>
<p>This is how the template I use creates 2 sidebars (this is the simple way, they are called &#8220;sidebar 1&#8243; and &#8220;sidebar 2&#8243; in the widget editing screen; other themes may do this differently to name the widgets).</p>
<p>So I just bumped it to create 3 widget areas.</p>
<p><pre><pre>
if ( function_exists(&#039;register_sidebar&#039;) )
&nbsp;&nbsp;&nbsp;&nbsp;register_sidebars(3);
</pre></pre></p>
<p>All of the content is already organized via the ds106 tag used- but the basic template for tagged content is not all that special.</p>
<p>So what I want now is a template specific for my tag- so I make a copy of say <strong>category.php</strong> and named it <strong>tag-ds106.php</strong>, which tells wordpress to only use this template for tag archives of ds106.</p>
<p>For the top part of the template, I update the main header for the title I want, and use some conditionals to display an intro paragraph only if this is the first page someone is viewing:</p>
<p><pre><pre>
&lt;h1 class=&quot;page-title&quot;&gt;ds106: Digital Storytelling Tagged Stuff&lt;/h1&gt;
&nbsp;&nbsp;&nbsp;&nbsp;
&lt;?php if ( $paged &lt; 2 ):?&gt;
&nbsp;&nbsp;&nbsp;&nbsp;
&lt;div class=&quot;entry&quot;&gt;
&lt;p&gt;
What flows below are the bits, ideas, experiments, projects, 
assignments, and assorted weird ideas all associated with 
my participation in &lt;a href=&quot;http://ds106.us/&quot;&gt;ds106&lt;/a&gt;, 
the most innovative open course every, first launched in 
January 2011.
&lt;/p&gt;
&lt;/div&gt;
&lt;?php endif; ?&gt;
</pre></pre></p>
<p>All of the loop stuff I leave as is, it is the standard stuff a template does to spit out content.</p>
<p>To use my custom sidebars, I change:</p>
<p><pre><pre>
&lt;?php get_sidebar(); ?&gt;
</pre></pre></p>
<p>to read</p>
<p><pre><pre>
&lt;?php get_sidebar(&#039;ds106&#039;); ?&gt;
</pre></pre></p>
<p>This tells WordPress to not use the <strong>sidebars.php</strong> template, but instead to use <strong>sidebars-ds106.php</strong>, a custom sidebar template.</p>
<p>This is easy. I make a copy of the sidebars.php statement, and for the code in the first sidebar, I find the line that reads:</p>
<p><pre><pre>
&lt;?php if ( !function_exists(&#039;dynamic_sidebar&#039;) || !dynamic_sidebar(1) ) : 
</pre></pre></p>
<p>This tells wordpress to use Sidebar 1, but now I want it to use 3. One edit:</p>
<p><pre><pre>
&lt;?php if ( !function_exists(&#039;dynamic_sidebar&#039;) || !dynamic_sidebar(3) ) : 
</pre></pre></p>
<p>And now all I have to do is populate the stuff I want in Widget sidebar 3 to show what I want on my ds106 tag pages.</p>
<p>Your mileage will most likely vary with the structure of your templates, but as a rule, you can always override a category or tag archive by this method. For another case, I am using the category I created to document by summer road trip into <a href="http://cogdogblog.com/odyssey/">a custom archive for CogDog Odyssey</a>.</p>
<p>Ahhh, ds106, packed and organized.</p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2011/04/26/stuffing-ds106/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Taking Old WordPress Menus to School</title>
		<link>http://cogdogblog.com/2011/02/26/wordpress-menus/</link>
		<comments>http://cogdogblog.com/2011/02/26/wordpress-menus/#comments</comments>
		<pubDate>Sat, 26 Feb 2011 19:55:12 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=6384</guid>
		<description><![CDATA[cc licensed ( BY NC ND ) flickr photo shared by ailatan With a relaxing day of nothing definite to do, I decided to poke around the corners of the blog house and clean up some dust gathered in the corners of my theme. Themes that predate WordPress 3, like mine, do not take advantage of the newer menu editing options. Old school themes were typically set up to turn Pages into menus, for example mine is wedged inside the header.php template: &#60;ul class=&#34;menu&#34;&#62; &#60;li class=&#34;&#60;?php if ( is_home() or is_archive() or is_single() or is_paged() or is_search() or (function_exists(&#039;is_tag&#039;) and is_tag()) ) { ?&#62;current_page_item&#60;?php } else { ?&#62;page_item&#60;?php } ?&#62;&#34;&#62;&#60;a title=&#34;&#60;?php bloginfo(&#039;name&#039;); ?&#62;&#34; href=&#34;&#60;?php bloginfo(&#039;url&#039;); ?&#62;&#34;&#62;Da Blog&#60;/a&#62;&#60;/li&#62; &#60;?php wp_list_pages(&#039;depth=1&#38;title_li=&#38;include=1358,887,2929,1620&#039;); ?&#62; &#60;/ul&#62; where I have picked the pages listed by their id numbers to appear. This is rather tedious to edit and manage, and I also ran into issues because I [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Bubel" href="http://flickr.com/photos/osiatynska/2885936798/"><img src="http://farm4.static.flickr.com/3286/2885936798_743f7f65cb.jpg" /></a><br /><small><a title="Bubel" href="http://flickr.com/photos/osiatynska/2885936798/">cc licensed ( BY NC ND )  flickr photo</a> shared by <a href="http://flickr.com/people/osiatynska/">ailatan</a></small></p>
<p>With a relaxing day of nothing definite to do, I decided to poke around the corners of the blog house and clean up some dust gathered in the corners of my theme. Themes that predate WordPress 3, like mine, do not take advantage of the <a href="http://codex.wordpress.org/WordPress_Menu_User_Guide">newer menu editing options</a>.</p>
<p>Old school themes were typically set up to turn Pages into menus, for example mine is wedged inside the <strong>header.php</strong> template:</p>
<pre class="brush: php">

&lt;ul class=&quot;menu&quot;&gt;
	&lt;li class=&quot;&lt;?php if ( is_home() or is_archive() or is_single() or is_paged() or is_search() or (function_exists(&#039;is_tag&#039;) and is_tag()) ) { ?&gt;current_page_item&lt;?php } else { ?&gt;page_item&lt;?php } ?&gt;&quot;&gt;&lt;a title=&quot;&lt;?php bloginfo(&#039;name&#039;); ?&gt;&quot; href=&quot;&lt;?php bloginfo(&#039;url&#039;); ?&gt;&quot;&gt;Da Blog&lt;/a&gt;&lt;/li&gt;
	&lt;?php wp_list_pages(&#039;depth=1&amp;title_li=&amp;include=1358,887,2929,1620&#039;); ?&gt;
&lt;/ul&gt;
</pre>
<p>where I have picked the pages listed by their id numbers to appear. This is rather tedious to edit and manage, and I also ran into issues because I have a few custom php pages that draw in the wordpress theme, and thus I end up with about three variations of <strong>header.php</strong> that would need edits.</p>
<p>The first logical step is to pull out the navigation elements into its own php file (the bits above) that I call <strong>nav.php</strong>. This way, in any header file, I pull that in via a simple replacement of above with</p>
<p><pre><pre>
&lt;?php include &quot;nav.php&quot;?&gt;
</pre></pre></p>
<p>Now I use the WordPress Menus editor (under <strong>Appearance</strong> on the dashboard). The great thing abut the menu editor is you can have more than just pages- you can use categories, and you can also mix in custom (e.g. outside URLs) all in one menu. </p>
<p>I created one i called, for lack of better terms, &#8220;doggie&#8221;</p>
<div id="attachment_6385" class="wp-caption alignnone" style="width: 510px"><a href="http://cogdogblog.com/wp-content/uploads/2011/02/wp-menu-editor.jpg"><img src="http://cogdogblog.com/wp-content/uploads/2011/02/wp-menu-editor-500x414.jpg" alt="" title="wp menu editor" width="500" height="414" class="size-medium wp-image-6385" /></a><p class="wp-caption-text">(click for full size)</p></div>
<p>I have the blog main URL as a custom one, a few of my pages, and a few external sites. In the menu pane on the right, you can slide the items around to get the order you like. It is very &#8220;widgety&#8221; and most current themes likely use widgets to do this. </p>
<p>To use this in my theme, I followed the options for inserting a menu listed in the codex for <strong><a href="http://codex.wordpress.org/Function_Reference/wp_nav_menu">wp_nav_menu()</a></strong> function, so my nav.php file now reads:</p>
<p><pre><pre>
&lt;?php wp_nav_menu( array(&#039;menu&#039; =&gt; &#039;doggie&#039;, &#039;container&#039; =&gt; false)); ?&gt;
</pre></pre></p>
<p>which simply tells my theme to use the doggie menu. The value for container tells it not to wrap it in a dv as it is already in your theme. It even seems to take care of theming the menu as &#8220;selected&#8221; if you are on the page that matches the menu item (e.g. <a href="http://cogdogblog.com/last/">my custom page that lists my 100 last posts</a>)</p>
<p>Your own integration depends on how your themes menus are set up in the template. Being able to edit the menu in the wordpress dashboard is not only easier, but much more flexible than the old school pages approach.</p>
<p>This turned out to be a useful exercise for another project, where the <a href="http://codex.wordpress.org/Function_Reference/wp_nav_menu">codex page for the menu function</a> includes sample code that can have different menus for logged in and not logged in users:</p>
<p><pre><pre>
&lt;?php
if ( is_user_logged_in() ) {
&nbsp;&nbsp;&nbsp;&nbsp; wp_nav_menu( array( &#039;theme_location&#039; =&gt; &#039;logged-in-menu&#039; ) );
} else {
&nbsp;&nbsp;&nbsp;&nbsp; wp_nav_menu( array( &#039;theme_location&#039; =&gt; &#039;logged-out-menu&#039; ) );
}
?&gt;
</pre></pre></p>
<p>Loving the WP hacking&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2011/02/26/wordpress-menus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>NaNoWriMo-ing In the Open</title>
		<link>http://cogdogblog.com/2010/11/02/nanowrimo-open/</link>
		<comments>http://cogdogblog.com/2010/11/02/nanowrimo-open/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 07:05:54 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[nanowrimo]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=5889</guid>
		<description><![CDATA[cc licensed flickr photo shared by zenobia_joy Against every sane, rational, &#8220;I&#8217;m so over committed I&#8217;m gonna explode&#8221; thought, I have decided to plunge my hand again into National Novel Writing Month (NaNoWriMo), the challenge where aspiring, and maybe aspired, writers take on the goal of writing a 50,000 word novel over a one month time span. I have always liked writing, but had never done anything substantial or larger than a long blog post, so it was one of those items I listed long ago on 43 Things. I had heard of NaNoWriMo, and it seemed analogous to running a marathon &#8212; a goal you keep telling yourself you cannot do&#8211; until you try. Last year I made my 50,000 mark (I burned a lot of adjectives up in the process), finished, and got through about 1 1/2 rounds of later edits before hanging it out to dry (see [...]]]></description>
			<content:encoded><![CDATA[<p><a title="inspiration strikes" href="http://flickr.com/photos/zenobia_joy/5012210963/"><img src="http://farm5.static.flickr.com/4144/5012210963_81bd4c88b4.jpg" /></a><br /><small><a title="inspiration strikes" href="http://flickr.com/photos/zenobia_joy/5012210963/">cc licensed flickr photo</a> shared by <a href="http://flickr.com/people/zenobia_joy/">zenobia_joy</a></small></p>
<p>Against every sane, rational, &#8220;I&#8217;m so over committed I&#8217;m gonna explode&#8221; thought, I have decided to plunge my hand again into <a href="http://www.nanowrimo.org/">National Novel Writing Month (NaNoWriMo)</a>, the challenge where aspiring, and maybe aspired, writers take on the goal of writing a 50,000 word novel over a one month time span.</p>
<p>I have always liked writing, but had never done anything substantial or larger than a long blog post, so it was <a href="http://www.43things.com/things/view/686/write-a-novel">one of those items I listed long ago on 43 Things</a>. I had heard of NaNoWriMo, and it seemed <a href="http://dommy.com/ihaterunning/2008/01/13/did-it/">analogous to running a marathon</a> &#8212; a goal you keep telling yourself you cannot do&#8211; until you try.</p>
<p>Last year I made my 50,000 mark (I burned a lot of adjectives up in the process), finished, and got through about 1 1/2 rounds of later edits before hanging it out to dry (see <a href="http://cogdogblog.com/2010/04/25/done-pile/">reflections on 2009</a>). It never got discovered, I never got a book deal, and I still have a day job (none of these were primary aspirations, but heck, I&#8217;d not mind being surprised).</p>
<p>The typical way one does this is mostly solitary- you write and write and write in most commonly a word processor (I did), and you update your NaNoWriMo site with your word count. There&#8217;s really no vehicle (by design I think) where your novel goes; that one is up to you. Yes, they do offer a lot of tools to make friends and there are local discussion boards where there are options to meet up with other writers in your &#8216;hood. </p>
<p>I was fence sitting this year&#8211; if I follow my running my running analogy, I ran the marathon once, finished, and never will do THAT again. </p>
<p>But writing was a mental challenge that can be taken on, IMHO, easier than physical ones. IT&#8217;S IN YOUR HEAD.</p>
<p>So I decided to do it this year to play more with the How I write more than the Drivel I Do Write. I decided I would not use Word this year, but do the writing in a blog to see how it works as a composition platform. I got myself the idea I could use it as one more Opportunity to Monkey with WordPress, and add some code in to do word counts and progress. And since the <a href="http://anthologize.org/">Anthologize plugin</a> came out a few months ago, it would be a new experiment in publishing to ePub and/or HTML.</p>
<p>Even better, just last weak Patrick Murray-John offered up <a href="http://www.patrickgmj.net/project/anthologize-nanowrimo">his new extension of Anthologize</a> geared to publish NaNoWriMo content.</p>
<p>My writing site is now fluttering in the open breeze at <a href="http://nano.dommy.com/">http://nano.dommy.com/</a></p>
<p><a href="http://nano.dommy.com/"><img src="http://cogdogblog.com/wp-content/uploads/2010/11/nanowrimo-2010.jpg" alt="" title="nanowrimo 2010" width="500" height="355" class="alignnone size-full wp-image-5891" /></a></p>
<p>Like my running efforts, writing in the open becomes another impetus to finish as you don&#8217;t want to bee seen failing in public. Below I will outline some of the tinkering I have done in WordPress over the last 2 days to get myself set up. I&#8217;ll say my novel really is not novel and idea, <em>I am a Dog</em> is the working title and it purports to be the insider view to the Canine world fur Humans to get past their naive assumptions of the dog-verse. </p>
<p>I actually am so far hoping a plot emerges.</p>
<p>But this was less about trying to Write a Great Story but just to try and also run some technical experiments.</p>
<p>I did the automatic WordPress install on my Dreamhost-ed site, and pawed through the themes it provided, settling on the sparse <a href="http://lucianmarin.com/page/themes/">Journalist theme</a>.</p>
<p>The code bits I hacked first was finding a way to add a word count to each post, and a method of summing the total words written. It ought to be a few database queries. After consulting the GoogleVerse, I found that MySQL has no function to count words, but I did find <a href="http://www.marketingtechblog.com/wordpress/wordpress-mysql-word-count/">a blog that had a method for listing posts and word counts</a>, and as the author says:</p>
<blockquote><p>MySQL doesn’t have a built-in word count function for MySQL, but as with every other unanswered question, some smart guy on the blogosphere already answered <a href="http://www.mwasif.com/2008/12/count-number-of-words-in-a-mysql-column/">how to use MySQL to get a Word Count</a>.</p></blockquote>
<p>It&#8217;s a little bit of a hack- a query that first sums the character length of a text column&#8217;s contents and subtracts from it the character length of the same text with blank spaces replaced by null characters &#8212; e.g. a word is defined by blank space.</p>
<p><pre><pre>
SELECT SUM( LENGTH(name) - LENGTH(REPLACE(name, &#039; &#039;, &#039;&#039;))+1) FROM table
</pre></pre></p>
<p>First I would need a function that would return the word count for a single post, so I could add to the meta content for each post:</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/11/post-count.png" alt="" title="post count" width="258" height="40" class="aligncenter size-full wp-image-5892" /></p>
<p>In my <strong>functions.php</strong> template, I added my new function- it merely takes an id as a parameter and runs the word count query on the database using the WordPress function to run a raw query,</p>
<pre class="brush: php">
function nano_get_post_word_count($id) {

	global $wpdb; 

	// get word count for a single post
	$word_count = $wpdb-&gt;get_results(&quot;
			SELECT
			SUM( LENGTH(`post_content`) - LENGTH(REPLACE(`post_content`, &#039; &#039;, &#039;&#039;))+1) AS &#039;wordcount&#039;
			FROM $wpdb-&gt;posts
			where `post_type` = &#039;post&#039; and `ID`=&quot; . $id, ARRAY_A
	);
	return($word_count[0][&#039;wordcount&#039;]);
}
</pre>
<p>I then monkeyed the part of the templates that output the author and date/time of post publishing to be more on line with what I need (not referring to posts)&#8211; I lost the original part of template, but this is what produces the example above</p>
<pre class="brush: php">
printf(__(&quot;%d words written by %s on %s&quot;,&#039;journalist-13&#039;), nano_get_post_word_count(get_the_ID()), get_the_author(), get_the_time(&#039;F jS, Y g:i a&#039;));
</pre>
<p>I use my own custom function to generate the word count for an individual post.</p>
<p>The next part was to do something to create a running total of all words written across all published posts, put into my side bar. I could have done the calculation every time the sidebar loaded, but that seemed un-needed as the number would change only when a change was made, so I went for a cheap cache method.</p>
<p>Again, modding the <strong>functions.php</strong> template, I first add a hook that will be called when a post is  saved.  </p>
<pre class="brush: php">
// Use the save_post action to update word counts
add_action( &#039;save_post&#039;, &#039;nano_save_post&#039;, 5, 2 );
</pre>
<p>This will call my own private function <strong>nano_save_post</strong> whenever anything is published or updated. The database query is similar, but we run it across all published posts. The last part is a way to write to a text file on my server. I&#8217;ve obscured my own path, put it is a directory named YYYY inside <strong>wp-content</strong> that contains writable file named <strong>XXXXX</strong>. it would likely be more sensible to stuff it ins a meta field of the database, and maybe I will get around to that later.</p>
<pre class="brush: php">
function nano_save_post()
{

	global $wpdb; 

	// custom query to events whose end dates are greater than the current time stamp
	$total_word_count = $wpdb-&gt;get_results(&quot;
			SELECT
			SUM( LENGTH(`post_content`) - LENGTH(REPLACE(`post_content`, &#039; &#039;, &#039;&#039;))+1) AS &#039;Totalcount&#039;
			FROM $wpdb-&gt;posts
			where `post_type` = &#039;post&#039; and `post_status` = &#039;publish&#039;&quot;, ARRAY_A
	);

	// update the file with the total word count
	$DATAFILE = fopen(TEMPLATEPATH . &#039;/../../YYY/XXXXX&#039;,&quot;w+&quot;);
	fwrite($DATAFILE, $total_word_count[0][&#039;Totalcount&#039;]) or die(&quot; failed writing $DATAFILE&quot;);
	fclose($DATAFILE);
}
</pre>
<p>And the last part is some code for the sidebar to put a count and display a percentage for progress value. I came across this <a href="http://blog.benogle.com/2009/06/16/simple-css-shiny-progress-bar-technique/">Simple CSS shiny progress bar technique</a> to add a fancy progress bar that will change as my word count increases:</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/11/nano-sidebar.png" alt="" title="nano sidebar" width="213" height="106" class="aligncenter size-full wp-image-5893" /></p>
<p>The progress bar calls for some CSS to be added to the style sheet, but the working parts are in the <strong>sidebar.php</strong> template. We simply get the contents of our cached wordcount file ($wordcount) convert it to a percent ($wordpercent), and use the values in the output:</p>
<pre class="brush: php">
&lt;?php
// get the wordcount
$wordcount = file_get_contents(TEMPLATEPATH . &#039;/../../YYY/XXXX&#039;);
// make percentage
$wordpercent = sprintf(&#039;%d&#039;,$wordcount / 50000 * 100);
?&gt;
&lt;h3&gt;&lt;?php _e(&#039;The Count&#039;,&#039;journalist-13&#039; ); ?&gt;&lt;/h3&gt;
&lt;p&gt;&lt;?php echo $wordcount?&gt; out of 50,000 words written (&lt;?php echo $wordpercent?&gt;% done)&lt;/p&gt;

&lt;div class=&quot;meter-wrap&quot;&gt;
    &lt;div class=&quot;meter-value&quot; style=&quot;background-color: #0a0; width: &lt;?php echo $wordpercent?&gt;%;&quot;&gt;
        &lt;div class=&quot;meter-text&quot;&gt;
            progress
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
</pre>
<p>The other thing that occurred to me is that the blog format puts the most recently written content first, and rolls back in time, while the way to read the story is the reverse order. I deployed this by using the same (default) category for my &#8220;posts&#8221;- the category is called <strong>writing</strong>. I then copied my basic <strong>index.php</strong> template into a new one <strong>category-writing.php</strong> which is what is used to generate the output for this single category.</p>
<p>To make the posts go in the order they were written (as opposed to the default reverse chronology), I simply added a modified query above &#8220;The Loop&#8221;</p>
<pre class="brush: php">
&lt;?php
// for category we will make posts go in date order
global $query_string;
query_posts($query_string . &quot;&amp;order=ASC&quot;);
?&gt;

&lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
</pre>
<p>This trick just recasts the database call for the main loop to pull posts in the order they were written which you can access via the link for this category <a href="http://nano.dommy.com/category/writing">http://nano.dommy.com/category/writing</a>.</p>
<p>That&#8217;s where I site so far- I added some links to the sidebar widget to pount to a file that has the ePub version generated by Patrick&#8217;s Anthologize modification &#8212; <a href="http://nano.dommy.com/wp-content/pub/i_am_a_dog.epub">http://nano.dommy.com/wp-content/pub/i_am_a_dog.epub</a>, as long as I remember to redo the Anthologize export each time I write something.</p>
<p>And that&#8217;s mostly it (until I decided to tinker tomorrow). </p>
<p>Now, I better make sure I get my 1667words in tomorrow.</p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2010/11/02/nanowrimo-open/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Barking Dog Photo Blog</title>
		<link>http://cogdogblog.com/2010/10/09/barking-dog-photo-blog/</link>
		<comments>http://cogdogblog.com/2010/10/09/barking-dog-photo-blog/#comments</comments>
		<pubDate>Sun, 10 Oct 2010 03:32:53 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Blog Pile]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[photography]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=5738</guid>
		<description><![CDATA[Note: Updated December 7, 2010 for use of new theme&#8230;. Nothing like a little WordPress hacking to get back in action. A few months ago I registered the domain barkingdog.me as a place intended to set up a photo gallery, but decided today to finally get around to hanging something for real there. My goals were to have a clean site with ginormous images, and also something that would be little fuss to add. On my NMC work in creating the MIDEA site I had set up an account to have access to the Graph Paper Press themes &#8212; yes, these are paid for themes, but are very elegant and sophisticated. I did moderate hacking to the Modularity theme for the MIDEA site, but was liking their photo gallery themes, and landed on Widescreen Fullscreen. With a few hours tweaking and adding one moderate hunk of code, I have what [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Note: Updated December 7, 2010 for use of new theme&#8230;.</strong></p>
<p>Nothing like a little WordPress hacking to get back in action.</p>
<p>A few months ago I registered the domain barkingdog.me as a place intended to set up a photo gallery, but decided today to finally get around to hanging something for real there. My goals were to have a clean site with ginormous images, and also something that would be little fuss to add.</p>
<p>On my NMC work in creating the <a href="http://midea.nmc.org/">MIDEA site</a> I had set up an account to have access to the <a href="http://www.graphpaperpress.com/category/themes/">Graph Paper Press themes</a> &#8212; yes, these are paid for themes, but are very elegant and sophisticated. I did moderate hacking to the Modularity theme for the MIDEA site, but was liking their photo gallery themes, and landed on <del datetime="2010-12-08T15:49:50+00:00"><a href="http://graphpaperpress.com/2010/05/05/widescreen/">Widescreen</a></del> <a href="http://graphpaperpress.com/themes/fullscreen/">Fullscreen</a>.</p>
<p>With a few hours tweaking and adding one moderate hunk of code, I have what I aimed for at <a href="http://barkingdog.me/">barkingdog.me</a>:</p>
<p><em>Widescreen Theme version, not used anymore</em><br />
<img src="http://cogdogblog.com/wp-content/uploads/2010/10/barking-dog.jpg" alt="" title="barking dog" width="500" height="261" class="alignnone size-full wp-image-5739" /></p>
<p><em>Fullscreen Theme version, current</em><br />
<a href="http://barkingdog.me/"><img src="http://cogdogblog.com/wp-content/uploads/2010/10/barkingdog.jpg" alt="" title="barkingdog" width="500" height="291" class="alignnone size-full wp-image-6029" /></a></p>
<p><del datetime="2010-12-08T15:49:50+00:00">It&#8217;s totally unblog. There is no sense at all of &#8220;posts&#8221; or chronology. The front is a rotating slide show (they limit it to 5 images, I think I know how to expand, but that&#8217;s later). I&#8217;ve arranged them via a few categories. like <a href="http://barkingdog.me/photos/category/photos-3/people">People</a>, <a href="http://barkingdog.me/photos/category/photos-3/places">Places</a>, <a href="http://barkingdog.me/photos/category/photos-3/things">Things</a>.</del></p>
<p>The newer Fullscreen creates thumbnails for the recent upload, newer ones are large icons  across the top, plus 20 more following on the bottom. When you roll over an icon, it lightens up. Its all automatic, so as new photos are uploaded, they rotate in at the top. I set the theme options to NOT out sidebars on the pages, to the version I embed now is the full width (950 pixels wide).</p>
<p>But here is the key thing. Because of <a href="http://cogdogblog.com/2009/08/26/aperture-strategy/">the way I do my photo workflow in Aperture</a>, I only have to upload the image in the post, and I used some code to embed the meta data that comes out of Aperture export and WordPress drinks in on image upload. </p>
<p>As I edit my photos in Aperture, I enter titles and captions as these are used when the photos are exported to flickr (using the <a href="http://connectedflow.com/flickrexport/aperture/">Connected Flow plugin</a>). I do this in Aperture because it gets saved as meta data (which has saved me lots of re-do if the export fails for some reason).</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/10/aperture-meta-data.jpg" alt="" title="aperture-meta-data" width="500" height="318" class="alignnone size-full wp-image-5740" /></p>
<p>I&#8217;ve noticed when I import these images into wordpress that the metadata from aperture is carried <em>into</em> WordPress</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/10/wp-upload.jpg" alt="" title="wp-upload" width="500" height="308" class="alignnone size-full wp-image-5741" /></p>
<p>That means the Aperture metadata travels with the image, and also includes things like shutter speed and aperture of the photo. It would be nice to bring that into my site!</p>
<p>After a bit of googling I found <a href="http://www.kristarella.com/2008/12/geo-exif-data-in-wordpress">some code used to add and display geocode data to an image uploaded to WordPress</a> that gave me more than enough to do what I needed (with some modifications).</p>
<p>So what I did was to add this code to my functions.php template file:</p>
<pre class="brush: php">
function insert_exif($pid) {

// insert EXIF data for first image uploaded to a given post $pid
// modified from http://www.kristarella.com/2008/12/geo-exif-data-in-wordpress

	if (is_single() || is_attachment()) {

// set up query to get the first attachment to a POST
// yes, this assumes that there is one image per post, but thats what I designed!

		$args = array(
			&#039;post_type&#039; =&gt; &#039;attachment&#039;,
			&#039;numberposts&#039; =&gt; 1,
			&#039;post_status&#039; =&gt; null,
			&#039;post_parent&#039; =&gt; $pid
		);
		$attachments = get_posts($args);

		// I really dont have to loop, but go ahead, its one loop
		foreach ($attachments AS $attachment) {

			// get the image id
			$imgid = $attachment-&gt;ID;

			// load metadata associated with image
			$imgmeta = wp_get_attachment_metadata( $imgid );

			// use caption as body text
			if (!empty($imgmeta[&#039;image_meta&#039;][&#039;aperture&#039;])) echo &#039;&lt;p&gt;&#039; . nl2br($imgmeta[&#039;image_meta&#039;][&#039;caption&#039;]) . &#039;&lt;/p&gt;&#039;;

			// list for other meta data
			echo &quot;&lt;ul class=&#039;exif&#039;&gt;&quot;;

				// camera type
				if (!empty($imgmeta[&#039;image_meta&#039;][&#039;camera&#039;])) echo &quot;&lt;li&gt;&lt;strong&gt;Camera:&lt;/strong&gt; &quot; . $imgmeta[&#039;image_meta&#039;][&#039;camera&#039;].&quot;&lt;/li&gt;&quot;;

				// aperture
				if (!empty($imgmeta[&#039;image_meta&#039;][&#039;aperture&#039;])) echo &quot;&lt;li&gt;&lt;strong&gt;Aperture:&lt;/strong&gt; f/&quot; . $imgmeta[&#039;image_meta&#039;][&#039;aperture&#039;].&quot;&lt;/li&gt;&quot;;

				// ISO
				if (!empty($imgmeta[&#039;image_meta&#039;][&#039;iso&#039;])) echo &quot;&lt;li&gt;&lt;strong&gt;ISO:&lt;/strong&gt; &quot; . $imgmeta[&#039;image_meta&#039;][&#039;iso&#039;].&quot;&lt;/li&gt;&quot;;

				// format shutter speed
				if (!empty($imgmeta[&#039;image_meta&#039;][&#039;shutter_speed&#039;]))
				{
				echo &quot;&lt;li&gt;&lt;strong&gt;Shutter Speed:&lt;/strong&gt; &quot;;
					if ((1 / $imgmeta[&#039;image_meta&#039;][&#039;shutter_speed&#039;]) &gt; 1)
					{
					echo &quot;1/&quot;;
						if ((number_format((1 / $imgmeta[&#039;image_meta&#039;][&#039;shutter_speed&#039;]), 1)) == 1.3
						or number_format((1 / $imgmeta[&#039;image_meta&#039;][&#039;shutter_speed&#039;]), 1) == 1.5
						or number_format((1 / $imgmeta[&#039;image_meta&#039;][&#039;shutter_speed&#039;]), 1) == 1.6
						or number_format((1 / $imgmeta[&#039;image_meta&#039;][&#039;shutter_speed&#039;]), 1) == 2.5)
						{
						echo number_format((1 / $imgmeta[&#039;image_meta&#039;][&#039;shutter_speed&#039;]), 1, &#039;.&#039;, &#039;&#039;) . &quot; s&lt;/li&gt;&quot;;
						}
						else{
						echo number_format((1 / $imgmeta[&#039;image_meta&#039;][&#039;shutter_speed&#039;]), 0, &#039;.&#039;, &#039;&#039;) . &quot; s&lt;/li&gt;&quot;;
						}
					}
					else{
					echo $imgmeta[&#039;image_meta&#039;][&#039;shutter_speed&#039;].&quot; s&lt;/li&gt;&quot;;
					}
				}

				// focal length
				if (!empty($imgmeta[&#039;image_meta&#039;][&#039;focal_length&#039;])) echo &quot;&lt;li&gt;&lt;strong&gt;Focal Length:&lt;/strong&gt; &quot; . $imgmeta[&#039;image_meta&#039;][&#039;focal_length&#039;].&quot;mm&lt;/li&gt;&quot;;

				// license
				if (!empty($imgmeta[&#039;image_meta&#039;][&#039;copyright&#039;])) echo &quot;&lt;li&gt;&lt;strong&gt;Rights:&lt;/strong&gt; &quot; . $imgmeta[&#039;image_meta&#039;][&#039;copyright&#039;].&quot;&lt;/li&gt;&quot;;		

				echo &quot;&lt;/ul&gt;&quot;;
			}

	}
}
</pre>
<p>Essentially we pull the image id for the first attachment for a given post, then fetch its meta data, and walk through some steps to generate output. What I am doing is using all of the image meta data to compose the body of the post.</p>
<p>Then in my single.php template, I added somewhere below </p>
<pre class="brush: php">
&lt;?php the_content(); ?&gt;
</pre>
<p>this one line to call my function</p>
<pre class="brush: php">
&lt;!-- photo meta data --&gt;
&lt;?php insert_exif(get_the_id()) ?&gt;;
</pre>
<p>And thus for <a href="http://barkingdog.me/photos/89">this uploaded photo</a> all I did was upload the media in WordPress, add some tags and categories, and publish. The results use the image meta data to add the bits below the photo:</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/10/imsge-meta.jpg" alt="" title="imsge meta" width="500" height="296" class="alignnone size-full wp-image-5742" /><br />
<em>Note: This is a screen shot from my first effort, for some reason I am no longer getting the rights meta-data, need to make sure I am including this in Aperture&#8230;.</em></p>
<p><del datetime="2010-12-08T15:49:50+00:00">Another nice feature of this theme is that I can create a page (or post) to hold a gallery of images- like <a href="http://barkingdog.me/iceland-horses">this one of Icelandic Horses</a>. All I had to do was upload all the media to the page, and write my body text, and the published page is a new slide show.</del> The new theme does not have this; I have not tweaked all I want to do with the categories and tags yet.</p>
<p>I&#8217;ll likely keep tweaking this, but as far as adding new photos to my site, it takes about 2 minutes. If I can figure out how to export more metadata fields from Aperture, you will see them here soon (apparently you cannot!)</p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2010/10/09/barking-dog-photo-blog/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Rolling New Tweet Button into WordPress</title>
		<link>http://cogdogblog.com/2010/08/13/rolling-new-tweet-button-into-wordpress/</link>
		<comments>http://cogdogblog.com/2010/08/13/rolling-new-tweet-button-into-wordpress/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 19:04:34 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Blog Pile]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=5496</guid>
		<description><![CDATA[UPDATE Jan 6 2012: While the code should still work, I have disabled this approach on my own blog, instead using the Sharing tool built into the JetPack plugin. Twitter has created a new widget that makes it more friendly to provide a tweet this button from your own web sites, blogs, etc. The benefit is that it pops a window up with the twitter functionality, so you are not sending people from the web site. See the announcement from the twitter blog to learn more. The tweetbutton creation widget makes it easy to generate the code, and has a umber of options to choose from for the appearance of the button and what gets prefilled in the tweet (plus with their new url shortener t.co is one letter shorter than competitors!). The sample code includes a Javascript link to the library that provides the functionality- &#60;a href=&#34;http://twitter.com/share&#34; class=&#34;twitter-share-button&#34; data-count=&#34;none&#34; [...]]]></description>
			<content:encoded><![CDATA[<p><em><strong>UPDATE Jan 6 2012:</strong> While the code should still work, I have disabled this approach on my own blog, instead using the Sharing tool built into the <a href="http://wordpress.org/extend/plugins/jetpack">JetPack plugin</a>.</em></p>
<p>Twitter has created a new widget that makes it more friendly to provide a tweet this button from your own web sites, blogs, etc. The benefit is that it pops a window up with the twitter functionality, so you are not sending people from the web site.</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/08/tweet-this-500x288.jpg" alt="" title="tweet this" width="500" height="288" class="alignnone size-medium wp-image-5497" /></p>
<p>See <a href="http://blog.twitter.com/2010/08/pushing-our-tweet-button.html">the announcement from the twitter blog to learn more</a>. The <a href="http://twitter.com/goodies/tweetbutton">tweetbutton creation widget</a> makes it easy to generate the code, and has a umber of options to choose from for the appearance of the button and what gets prefilled in the tweet (plus with their new url shortener <strong>t.co</strong> is one letter shorter than competitors!).</p>
<p>The sample code includes a Javascript link to the library that provides the functionality- </p>
<pre class="brush: html">
&lt;a href=&quot;http://twitter.com/share&quot; class=&quot;twitter-share-button&quot; data-count=&quot;none&quot; data-via=&quot;cogdog&quot;&gt;Tweet&lt;/a&gt;&lt;script type=&quot;text/javascript&quot; src=&quot;http://platform.twitter.com/widgets.js&quot;&gt;&lt;/script&gt;
</pre>
<p>This works well on a WordPress single post template, where it pulls the url for the current page and provides a tweet button where-ever you like it. I put mine in the header tag that displays the blog post. </p>
<p><a href="http://cogdogblog.com/2010/08/12/one-thing/"><img src="http://cogdogblog.com/wp-content/uploads/2010/08/tweet-button.jpg" alt="" title="tweet button" width="484" height="217" class="alignnone size-full wp-image-5498" /></a></p>
<p>I also want on my main index page and archives where there are multiple posts. Here is how I set it up.</p>
<p>First I put the Javascript link in the header template, inside the &lt;head&gt;&#8230;&lt;/head&gt;</p>
<pre class="brush: html">
&lt;script type=&quot;text/javascript&quot; src=&quot;http://platform.twitter.com/widgets.js&quot;&gt;&lt;/script&gt;
</pre>
<p>This way we only load this library once, and now can use the code on any web page.</p>
<p>Now on my single post I can use the rest of the widget code to add my button (sans the javascript tags)</p>
<pre class="brush: html">
&lt;h1 class=&quot;entry-title&quot;&gt;&lt;?php the_title(); ?&gt; &lt;a href=&quot;http://twitter.com/share&quot; class=&quot;twitter-share-button&quot; data-count=&quot;none&quot; data-via=&quot;cogdog&quot;&gt;Tweet&lt;/a&gt;&lt;/h1&gt;
</pre>
<p>That was easy.</p>
<p>But I wanted more- I would like it on each post on the front of my blog.</p>
<p>That is also easy.</p>
<p>For the main page template, index.template, we cannot use the basic code because it posts the URL and title for the current page in view (just my blog) rather than for each post in the loop. This is easily rectified using the WordPress loop variables, so inside my loop where it is moving through posts, I have:</p>
<pre class="brush: html">
&lt;h2 class=&quot;entry-title&quot;&gt;&lt;a href=&quot;&lt;?php the_permalink(); ?&gt;&quot; rel=&quot;bookmark&quot; title=&quot;Permalink to &lt;?php the_title(); ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;
&lt;a href=&quot;http://twitter.com/share&quot; class=&quot;twitter-share-button&quot; data-url=&quot;&lt;?php the_permalink(); ?&gt;&quot; data-count=&quot;none&quot; data-text=&quot;&lt;?php the_title(); ?&gt;&quot; data-via=&quot;cogdog&quot;&gt;Tweet&lt;/a&gt;&lt;/h2&gt;
</pre>
<p>so I am populating the data-url and the data-text fields with the permalink and post title as WordPress moves through the loops.  Try it out on <a href="http://cogdogblog.com/">http://cogdogblog.com/</a> &#8211; each post title has its own tweet this button.</p>
<p>I can now add this to archives, search result templates etc.</p>
<p>I like the functionality and am looking at rolling it as well into our NMC fleet of web sites.</p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2010/08/13/rolling-new-tweet-button-into-wordpress/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Coding My Own WordPress Authors List</title>
		<link>http://cogdogblog.com/2010/08/05/wordpress-authors-list/</link>
		<comments>http://cogdogblog.com/2010/08/05/wordpress-authors-list/#comments</comments>
		<pubDate>Fri, 06 Aug 2010 06:47:21 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Blog Pile]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=5464</guid>
		<description><![CDATA[cc licensed flickr photo shared by bitzcelt Elwood: It&#8217;s 106 miles to get this sidebar coded, we&#8217;ve got a full tank of gas, half a pack of cigarettes, it&#8217;s dark and we&#8217;re wearing sunglasses. Jake: Hit it. I have no idea why I opened this way except for Jim Groom Inspiration. But to jive my code chops, there&#8217;s nothing more energizing than doing a little hack and chop coding in WordPress. Today I was again doing some sidebar fine tuning on the NMC MIDEA web site (previously covered in my almost done series on doing custom post types in WordPress 3.0). It was one of those &#8220;oh this might take 20 minute&#8221; deals that ended up going a bit longer. but like Jake and Elwood, when you gotta go to Chicago, you drop the sunglasses and hit it. And this is pretty simple, and most likely there is a plugin [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Too Cool / Blues for Sale" href="http://flickr.com/photos/bitzcelt/413275282/"><img src="http://farm1.static.flickr.com/126/413275282_8307909f17.jpg" /></a><br /><small><a title="Too Cool / Blues for Sale" href="http://flickr.com/photos/bitzcelt/413275282/">cc licensed flickr photo</a> shared by <a href="http://flickr.com/people/bitzcelt/">bitzcelt</a></small></p>
<p><strong>Elwood</strong>: It&#8217;s 106 miles to get this sidebar coded, we&#8217;ve got a full tank of gas, half a pack of cigarettes, it&#8217;s dark and we&#8217;re wearing sunglasses.<br />
<strong>Jake</strong>: Hit it.</p>
<p>I have no idea why I opened this way except for <a href="http://bavatuesdays.com/">Jim Groom Inspiration</a>.</p>
<p>But to jive my code chops, there&#8217;s nothing more energizing than doing a little hack and chop coding in WordPress.</p>
<p>Today I was again doing some sidebar fine tuning on the NMC <a href="http://midea.nmc.org/">MIDEA web site</a> (previously covered in my almost done <a href="http://cogdogblog.com/2010/07/23/roundup-wp3/">series on doing custom post types in WordPress 3.0</a>).</p>
<p>It was one of those &#8220;oh this might take 20 minute&#8221; deals that ended up going a bit longer. but like Jake and Elwood, when you gotta go to Chicago, you drop the sunglasses and hit it.</p>
<p>And this is pretty simple, and most likely there is a plugin that already does what I needed, but sometimes, it is just worth the effort to roll your own code. It&#8217;s the same joy of actually doing something in your car engine that actually does not result in flames or calling a tow truck.</p>
<p>What we have on this site is a number of accounts, me as admin, some staff as editors, and a few guest bloggers, with roles as authors. The category our gues authors blog under is called &#8220;<a href="http://midea.nmc.org/ideas">Ideas</a>&#8220;, and my idea was to add to the template sidebar, above the part where widgetized stuff happens, a blurb and a listing of our guest bloggers. I also liked the feature of not listing authors who have yet to post.</p>
<p>At first it seemed like the function <a href="http://codex.wordpress.org/Function_Reference/wp_list_authors">wp_list_authors</a> would do, but it has no way to list just authors of a certain role- it is meant for all authors (you can hide the administrator; I thought by making all our staff admins it would work, but that did not).</p>
<p>I scoured a few plugins, but most are widget oriented (its a long explanation, but because this only appears on category archives, doing a special set of widgets was overload) or or geared for listing author avatars.</p>
<p>Next I googled and found <a href="http://wpengineer.com/list-all-users-in-wordpress/">a custom function from WP Engineer</a> that at first looked good, but it was written for previous versions of WordPress- things have really changed from the early days of basing author capabilities on an integer user role number, though the info is still there in the database. I started editing their script to get it to work, going back and forth to phpMyAdmin to run test queries.</p>
<p>it ended up sort of working, but still was not what I sought. I wanted it (a) to not only list the authors, but link to their profiles; (b) list the number of posts the author has written like the wp_list_authors() function does; (c) skip authors that have not posted (again like  wp_list_authors() function does); and (d) list the authors in alphabetical order of their last name&#8230; which I did not find anywhere.</p>
<p>With some more elbow grease I more or less got it going, but really though the code was not quite as lean as it should be (this is the place where it may not pay to keep coding, but now Jake and Elwood were only 50 miles from the Windy City, and they could see the glow of light on the horizon).</p>
<p>There is actually quite a bit of fine grained tuning you can do for blog user accounts, down to the specific capabilities, and you can add/remove what accounts can do beyond the basic titles of &#8220;author&#8221; editor (<a href="http://codex.wordpress.org/Roles_and_Capabilities">see Roles and Capabilities)</a>. Most of this seems put to use for creators of plugins.</p>
<p>But with another round of Googling, I found <a href="http://sltaylor.co.uk/blog/get-wordpress-users-by-role/">the function I was looking for from Steve Taylor</a>- it would return a list of ids of users that had a given role, and it made my code a lot shorter.</p>
<p>Only 25 miles til Chi-Town, boys&#8230;.</p>
<p>I still ended up rolling some bits to do the parts I wanted&#8230; so here are the two functions I added to my <strong>functions.php</strong> template to generate a list of users with a given role as a &gt;ul&lt;&#8230;&gt;/ul&#038;lt list, in alphabetical order by last name (I made all the accounts, so I know they have these parts completed), skipping users with no posts&#8230;</p>
<p>It is pretty heavily commented, and ought to be semi-explanatory&#8230;</p>
<pre class="brush: php">
function getUsersByRole( $role ) {
	// find all users with given role
	// via http://sltaylor.co.uk/blog/get-wordpress-users-by-role/

	if ( class_exists( &#039;WP_User_Search&#039; ) ) {
		$wp_user_search = new WP_User_Search( &#039;&#039;, &#039;&#039;, $role );
		$userIDs = $wp_user_search-&gt;get_results();
	} else {
		global $wpdb;
		$userIDs = $wpdb-&gt;get_col(&#039;
			SELECT ID
			FROM &#039;.$wpdb-&gt;users.&#039; INNER JOIN &#039;.$wpdb-&gt;usermeta.&#039;
			ON &#039;.$wpdb-&gt;users.&#039;.ID = &#039;.$wpdb-&gt;usermeta.&#039;.user_id
			WHERE &#039;.$wpdb-&gt;usermeta.&#039;.meta_key = \&#039;&#039;.$wpdb-&gt;prefix.&#039;capabilities\&#039;
			AND &#039;.$wpdb-&gt;usermeta.&#039;.meta_value LIKE \&#039;%&quot;&#039;.$role.&#039;&quot;%\&#039;
		&#039;);
	}
	return $userIDs;
}

function midea_list_authors($user_role=&#039;author&#039;, $show_fullname = true) {
	// Generate a list of authors for a given role
	// default is to list authors and show full name

	global $wpdb;

	$blog_url = get_bloginfo(&#039;url&#039;); // store base URL of blog
	$holding_pen = array(); // this is cheap, a holder for author data

 	echo &#039;&lt;ul&gt;&#039;;

	// get array of all author ids for a role
	$authors = getUsersByRole( $user_role );

	foreach ( $authors as $item ) {

		// get number of posts by this author; custom query
		$post_count = $wpdb-&gt;get_results(&quot;SELECT COUNT( * ) as cnt
		FROM  $wpdb-&gt;posts
		WHERE  post_author =&quot; . $item . &quot;
		AND  post_type =  &#039;post&#039;
		AND  post_status =  &#039;publish&#039;&quot;);

		// only output authors with posts; ugly way to get to the result, but it works....

		if ($post_count[0]-&gt;cnt) {

			// load info on this user
			$author = get_userdata( $item);

			// store output in temp array; we use last names as an index in this array
			$holding_pen[$author-&gt;last_name] =  &#039;&lt;li&gt;&lt;a href=&quot;&#039; . $blog_url . &#039;/author/&#039;  . $author-&gt;user_login  . &#039;&quot;&gt; &#039; . $author-&gt;display_name . &#039; (&#039; . $post_count[0]-&gt;cnt . &#039;)&lt;/a&gt; &lt;/li&gt;&#039;;
		}

	}

	// now sort the array on the index to get alpha order
	ksort($holding_pen);

	// now we can spit the output out.
	foreach ($holding_pen as $key=&gt;$value) {
		echo $value;
	}
	echo &#039;&lt;/ul&gt;&#039;;
}
</pre>
<p>And this is simply called in my main sidebar.php template&#8230; I use a conditional to insert it where I want, so it only appears on the archives for the &#8220;ideas&#8221; category (I made the defaults of my function match my use case&#8230; sue me!)</p>
<pre class="brush: php">
&lt;?php if (is_category(&#039;ideas&#039;)) : ?&gt;
&lt;h3&gt;MIDEA Ideas&lt;/h3&gt;
&lt;p&gt;Our panel of guest bloggers, all of them experts in the museum and technology field, bring you food for thought. &lt;/p&gt;

&lt;?php midea_list_authors(); ?&gt; 

&lt;?php endif?&gt;
</pre>
<p>And with that, welcome to the big city&#8230; <a href="http://midea.nmc.org/ideas/">see it in action</a></p>
<p><a href="http://midea.nmc.org/ideas/"><img src="http://cogdogblog.com/wp-content/uploads/2010/08/midea-ideas.jpg" alt="" title="midea-ideas" width="500" height="315" class="alignnone size-full wp-image-5465" /></a></p>
<p>And the boys are now in town!</p>
<p><a title="Altar Of Blues" href="http://flickr.com/photos/51035610542@N01/160955737/"><img src="http://farm1.static.flickr.com/71/160955737_aec8393e03.jpg" /></a><br /><small><a title="Altar Of Blues" href="http://flickr.com/photos/51035610542@N01/160955737/">cc licensed flickr photo</a> shared by <a href="http://flickr.com/people/51035610542@N01/">cszar</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2010/08/05/wordpress-authors-list/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Pinning WordPress 3 Custom Content to the Map (b)</title>
		<link>http://cogdogblog.com/2010/07/22/wp3-custom-content-map/</link>
		<comments>http://cogdogblog.com/2010/07/22/wp3-custom-content-map/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 06:30:48 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Blog Pile]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp3]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=5310</guid>
		<description><![CDATA[cc licensed flickr photo shared by QualityFrog In a previous long winding post, Dressing up and Displaying WordPress 3 Custom Post Content (a), I tried to document the first part of my WordPress 3 site I manipulated the new Custom Content types. It seems it takes longer to explain that to build, and in the end, I&#8217;m wondering if it is even explained. Leading up to this, I began by overviewing the plans for a new web project, we set up the places to create the new content types, and introduced the benefits using child themes. Then we dove headfirst into the code to create the interface and data elements to add/edit the custom meta data for each of the three content (post) types. Getting your content onto your site is a matter of manipulating The Loop and then writing new side loops where needed to put things on the [...]]]></description>
			<content:encoded><![CDATA[<p><a title="" href="http://flickr.com/photos/qualityfrog/3623616823/"><img src="http://farm4.static.flickr.com/3626/3623616823_b95a70f872.jpg" /></a><br /><small><a title="" href="http://flickr.com/photos/qualityfrog/3623616823/">cc licensed flickr photo</a> shared by <a href="http://flickr.com/people/qualityfrog/">QualityFrog</a></small></p>
<p>In a previous long winding post, <a href="http://cogdogblog.com/2010/07/21/dressing-up-wp3-content-a/">Dressing up and Displaying WordPress 3 Custom Post Content (a)</a>, I tried to document the first part of my WordPress 3 site I manipulated the new Custom Content types. It seems it takes longer to explain that to build, and in the end, I&#8217;m wondering if it is even explained.</p>
<p>Leading up to this,  I began by <a href="http://cogdogblog.com/2010/05/28/wordpress-3-pt1/">overviewing the plans</a> for a new web project, we set up the <a href="http://cogdogblog.com/2010/05/28/setting-up-custom-contenttypes/">places to create the new content types</a>, and introduced the benefits <a href="http://cogdogblog.com/2010/05/29/those-darn-kids/">using child themes</a>.  Then we <a href="http://cogdogblog.com/2010/06/06/put-on-your-hazmat-suits/">dove headfirst into the code to create the interface and data elements to add/edit the custom meta data</a> for each of the three content (post) types.</p>
<p>Getting your content onto your site is a matter of manipulating The Loop and then writing new side loops where needed to put things on the sidebar. For this site, <a href="http://midea.nmc.org/">MIDEA</a>,  the dissected part from the last post was the construct for a gallery page of Project content types and then a display page for a single project.</p>
<p>In this post, I am going to describe what I did about the second Content type, Organizations, in which we track information like web site, Facebook, and twitter urls, street address&#8211; and what turned out to be neat, storing a latitude and longitude for each organization so we could generate a dynamic map showing the location of an organization. </p>
<p>I&#8217;m not going to detail as much on the template (org.php) that displays all the organizations &#8211; there&#8217;s not much that is different from the ground covered in the last post. You can explore this page at <a href="http://midea.nmc.org/org/">http://midea.nmc.org/org/</a></p>
<p><a href="http://midea.nmc.org/org/"><img src="http://cogdogblog.com/wp-content/uploads/2010/07/midea-orgs.jpg" alt="" title="midea-orgs" width="500" height="375" class="alignnone size-full wp-image-5311" /></a></p>
<p>(A) The main part of the page is an A-Z listing of all the organizations, using the same abbreviated format used elsewhere ion the site. Like we did do the A-Z project listing, we will use the custom mySQL call to the database so we can avoid the alphabetizing on organizations whose name starts with &#8220;The&#8221;.</p>
<p>(B) This block on the sidebar lists to other organization pages that will be described below- one generates a google map showing locations of all organizations, the other links provide a list of all the organizations that have Facebook or twitter presences.</p>
<p>(C) The rest of the sidebar is standard looping stuff for sidebars. One lists 3 blog posts from a category we call &#8220;Ideas&#8221;, the next is the 2 from &#8220;News&#8221; category, and the last is a random pul of three projects, using logic covered in the <a href="http://cogdogblog.com/2010/07/21/dressing-up-wp3-content-a/">previous post in this series</a>.</p>
<p>So generating the list of organizations follows directly from the work we did on projects.</p>
<pre class="brush: php">
&lt;?php
global $wpdb;
global $post;

// custom query to get posts of typer org order by trimming the leading &quot;The &quot; in a title
$orgposts = $wpdb-&gt;get_results(&quot;SELECT wposts.*
FROM $wpdb-&gt;posts as wposts
WHERE wposts.post_type = &#039;org&#039; and wposts.post_status=&#039;publish&#039;
ORDER BY TRIM(LEADING &#039;The &#039; FROM wposts.post_title)&quot;, OBJECT);
?&gt;

&lt;?php if ($orgposts): ?&gt;

		&lt;?php foreach ($orgposts as $post): ?&gt;
		&lt;?php setup_postdata($post); ?&gt;

&lt;h2&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot; rel=&quot;bookmark&quot; title=&quot;&lt;?php printf(__(&#039;Permanent Link to %s&#039;,&#039;gpp_i18n&#039;),the_title_attribute(&#039;echo=0&#039;)); ?&gt;&quot;&gt;&lt;?php the_title() ?&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;?php get_the_image(); ?&gt;
&lt;?php the_excerpt(); ?&gt;&lt;br /&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot; rel=&quot;bookmark&quot; title=&quot;&lt;?php printf(__(&#039;Permanent Link to %s&#039;,&#039;gpp_i18n&#039;),the_title_attribute(&#039;echo=0&#039;)); ?&gt;&quot;&gt;Learn more about &lt;?php the_title() ?&gt;...&lt;/a&gt;
&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;
 &lt;?php endforeach; ?&gt;
&lt;?php endif; ?&gt;
</pre>
<p>Lines (05-09) set up the database query to select all organization content, and do the trick to order it by the title after trimming any leading &#8220;The&#8221; in the title. We then walk through all the results, and lines (17-20) output the content. </p>
<p>Now we turn to making a template to display a single organization, using as an example, the <a href="http://midea.nmc.org/org/amon-carter-museum/">Amon Carter Museum</a> from Fort Worth, Texas.</p>
<p><a href="http://midea.nmc.org/org/amon-carter-museum/"><img src="http://cogdogblog.com/wp-content/uploads/2010/07/amon-carter-page.jpg" alt="" title="amon-carter-page" width="500" height="543" class="alignnone size-full wp-image-5313" /></a></p>
<p>(A) This is the main &#8220;post&#8221; for this content type, whatever we put in the blog editing box. It comes with the built in Loop, so we dont have to doctor it up at all.</p>
<p>(B) As an addition to the main content, we&#8217;ll do a second Loop to list all the projects from this museum.</p>
<p>(C) The top end of the sidebar displays the icon associated with the organization/post (using the built-in in WordPress &#8220;featured image&#8221; functionality). This is followed by links to the organization&#8217;s web site, Facebook page, and twitter account, if any of these data fields are present (they are optional, some of the organizations don&#8217;t have them all).</p>
<p>(D) Here is the special stuff! For each organization, we have meta data fields for latitude/longitude, and we can generate a google map. The details follow!</p>
<p>First, once we have output the descriptive info for this organization (A), we have some left over information we can use- essentially all the variables used in the last loop.</p>
<pre class="brush: php">
&lt;h3&gt;&lt;?php the_title() ?&gt; Projects&lt;/h3&gt;

&lt;?php $proj_query = new WP_Query(&quot;post_type=proj&amp;amp;posts_per_page=-1&amp;amp;orderby=title&amp;amp;order=ASC&amp;amp;meta_key=midea-proj-org&amp;amp;meta_value=&quot; . get_the_id()); ?&gt;

&lt;?php if ($proj_query-&gt;have_posts()) : while ($proj_query-&gt;have_posts()) : $proj_query-&gt;the_post(); ?&gt;

&lt;?php get_the_image(); ?&gt;
&lt;h6&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;&gt;&lt;?php the_title() ?&gt;&lt;/a&gt;&lt;/h6&gt;
&lt;?php the_excerpt(); ?&gt;&lt;br /&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;&gt;Learn more about &lt;?php the_title() ?&gt;...&lt;/a&gt;
&lt;div class=&quot;clear&quot;&gt;&lt;/div&gt;
&lt;hr /&gt;

&lt;?php endwhile; endif; wp_reset_query(); ?&gt;
</pre>
<p>So for example, in line (01) we can use the_title to create the header for projects that includes the organization&#8217;s name. It is in line (03) that we set up the logic to get the projects we want- all of the ones assocated with this organization. So to follow out the query string, we want posts that are of type <strong>proj</strong>, and we want to list all we find (posts_per_page= -1). They are listed in alphabetical order. And the selection is defined by the projects whose meta data field for the organization id matches the id used to generate the content in (A)&#8211; the post_id for this content is the same as the meta-date values we need to match.</p>
<p>We then just crank up our Loop structure to put the output on the screen.</p>
<p>Hey, that was easy, right?</p>
<p>Now let&#8217;s dig into the sidebar; which again we call in a custom sidebar via</p>
<pre class="brush: php">
&lt;?php get_sidebar(&#039;single-org&#039;) ?&gt;
</pre>
<p>meaning the template lives in <strong>sidebar-single-org.php</strong> For the organization info portion (C) we use:</p>
<pre class="brush: php">
global $post;

$my_fields = midea_org_fields(); // fields for this content type
foreach ($my_fields as $key =&gt; $value) {
	// load values into our array
	$my_fields[$key] = get_post_meta($post-&gt;ID, &#039;midea-org-&#039; . $key, true);
}
?&gt;

&lt;?php get_the_image( array( &#039;link_to_post&#039; =&gt; false, &#039;class&#039; =&gt; &#039;thumbnail&#039; ) ); ?&gt;

&lt;h4&gt;&lt;?php the_title() ?&gt; Online&lt;/h4&gt;
&lt;?php
if ($my_fields[&#039;web&#039;] != &#039;&#039;) echo &#039;&lt;p class=&quot;biglabel&quot;&gt;&lt;a href=&quot;&#039; . $my_fields[&#039;web&#039;] . &#039;&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;&#039; . get_bloginfo(&#039;url&#039;) .  &#039;/images/world.jpg&quot; width=&quot;40&quot; height=&quot;40&quot; align=&quot;middle&quot; alt=&quot; &quot; /&gt;&lt;/a&gt; &lt;a href=&quot;&#039; . $my_fields[&#039;web&#039;] . &#039;&quot; target=&quot;_blank&quot;&gt;web site&lt;/a&gt;&lt;/p&gt;&#039;;

if ($my_fields[&#039;facebook&#039;] != &#039;&#039;) echo &#039;&lt;p class=&quot;biglabel&quot;&gt;&lt;a href=&quot;&#039; . $my_fields[&#039;facebook&#039;] . &#039;&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;&#039; . get_bloginfo(&#039;url&#039;) .  &#039;/images/facebook.jpg&quot; width=&quot;40&quot; height=&quot;40&quot; align=&quot;middle&quot;  alt=&quot; &quot; /&gt;&lt;/a&gt; &lt;a href=&quot;&#039; . $my_fields[&#039;facebook&#039;] . &#039;&quot; target=&quot;_blank&quot;&gt;facebook&lt;/a&gt;&lt;/p&gt;&#039;;

if ($my_fields[&#039;twitter&#039;] != &#039;&#039;) echo &#039;&lt;p class=&quot;biglabel&quot;&gt;&lt;a href=&quot;http://twitter.com/&#039; . $my_fields[&#039;twitter&#039;] . &#039;&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;&#039; . get_bloginfo(&#039;url&#039;) .  &#039;/images/twitter.jpg&quot; width=&quot;40&quot; height=&quot;40&quot; align=&quot;middle&quot;  alt=&quot; &quot; /&gt;&lt;/a&gt; &lt;a href=&quot;http://twitter.com/&#039; . $my_fields[&#039;twitter&#039;] . &#039;&quot; target=&quot;_blank&quot;&gt;twitter&lt;/a&gt;&lt;/p&gt;&#039;;
?&gt;
</pre>
<p>Lines (03-07) calls one of our custom functions to set up an array to store the metadata for this organization, and we then use <strong>get_post_meta()</strong> to load the values from the database into the array we are using. Take a close look at the strategy. My meta data for organizations I refer to as &#8220;url&#8221;, &#8220;facebook&#8221;, &#8220;lat&#8221;, these are the keys to the array I am creating. Inside the meta-data structure of wordpress, they are named &#8220;midea-org-&#8221;url&#8221;, &#8220;midea-org-facebook&#8221;, &#8220;midea-org-lat&#8221;.</p>
<p>Lines 10 and 12 then output the icon and a title, again using the still available data items from the original Loop. Lines (14), (16), and (18) then output the organization URL, facebook URL, and twitter URL but only if there are values for the item. Note for facebook, we store a full URL in the database, but for twitter, we only need the username as the twitter URLs follow from a standard pattern.</p>
<p>Now it is map time. Our organization meta-date includes fields for a latitude and longitude. I looked at a number of map plugins, and found that the <a href="http://wordpress.org/extend/plugins/google-maps-advanced/">Inline Google Map one did the trick</a> even though it was last updated at the end of 2008.</p>
<p>To get  map to display, all our template has to produce is the data in the form of an html definition list &lt;dl&gt;&#8230;.&lt;/dl&gt; that includes not only the latitude.longitude to pin the center point of the map, with extra &lt;dt&gt;&lt;dd&gt; pairings, we can also add pins and labels to the map. And we can use our other metadata to populate the bubble:</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/07/amon-carter-map.jpg" alt="" title="amon-carter-map" width="337" height="352" class="alignnone size-full wp-image-5317" /></p>
<p>Time to see the code!</p>
<pre class="brush: php">
&lt;?php if ($my_fields[&#039;lat&#039;] !== &#039;&#039;) : ?&gt;
&lt;h4&gt;&lt;?php the_title() ?&gt; on the Map&lt;/h4&gt;

&lt;div id=&quot;orgmap&quot;&gt;
&lt;dl style=&quot;margin-bottom:20px;&quot; title=&quot;googlemap;w:100%;h:300&quot;&gt;
&lt;dt class=&quot;map&quot;&gt;&lt;a href=&quot;http://maps.google.com/?z=14&amp;ll=&lt;?php echo $my_fields[&#039;lat&#039;]?&gt;,&lt;?php echo  $my_fields[&#039;long&#039;]?&gt;&amp;om=1&quot;&gt;&lt;img src=&quot;/images/map-loader.gif&quot; alt=&quot;&quot; width=&quot;126&quot; height=&quot;22&quot; align=&quot;middle&quot;&gt;&lt;br /&gt;(map loading...)&lt;/a&gt;&lt;/dt&gt;
&lt;dt&gt;&lt;a href=&quot;http://maps.google.com/?ll=&lt;?php echo  $my_fields[&#039;lat&#039;]?&gt;,&lt;?php echo  $my_fields[&#039;long&#039;]?&gt;&quot; title=&quot;marker&quot;&gt;map&lt;/a&gt;&lt;/dt&gt;
&lt;dd&gt;&lt;h4&gt;&lt;?php the_title(); ?&gt;&lt;/h4&gt;&lt;br /&gt;&lt;?php get_the_image(array( &#039;height&#039; =&gt; 60, &#039;width&#039; =&gt; 60,))?&gt;&lt;?php echo nl2br($my_fields[&#039;address&#039;])?&gt;&lt;br /&gt;
&lt;a href=&quot;&lt;?php echo $my_fields[&#039;web&#039;]?&gt;&quot;&gt;web site&lt;/a&gt;&lt;/dd&gt;
&lt;/dl&gt;
&lt;/div&gt;
&lt;?php endif?&gt;

&lt;?php if ($my_fields[&#039;address&#039;] !=&#039;&#039;) echo &#039;&lt;p&gt;&lt;strong&gt;Address&lt;/strong&gt;&lt;br /&gt;&#039; . nl2br($my_fields[&#039;address&#039;]) . &#039;&lt;/p&gt;&#039;?&gt;
</pre>
<p>We test in line (01) for a value in out latitude variable to make sure we should eb drawing a map (no coordinates, no map for you!). The structure in line (05) is what the plugin expects to know this is a &lt;dl&gt; list for a map, as defined by the title attribute. I&#8217;ve set the map up here to fill 100% of the space it has (the sidebar) and to be 300 pixels high. You get to pick the dimensions of the map here.</p>
<p>The first &lt;dt&gt; with a class=map in line (06) is special to define the parameters for Google maps, meaning the zoom level (14) and the location of the center of the map ll as fed by the values of our latitude, longitude.</p>
<p>The embedded image and &#8220;map loading&#8221; text is a nice feature because this is what the page displays while the map takes a few seconds to be rendered. Instead of a blank box, this little bit of code will show a progress bar animation:</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/07/loading.png" alt="" title="loading" width="156" height="74" class="aligncenter size-full wp-image-5321" /></p>
<p>which disappears once the plugin places the map. Any other subsequent  &lt;dt&gt;  &lt;dd&gt; pairs define the map pins-  &lt;dt&gt; contains the coordinates for the pin with the google map notation, and &lt;dd&gt; defines what is displayed in the bubble for that pin (you could place many pins on a map&#8211; we will do that later) &#8211; see line (08). We put a small sized version of the organization&#8217;s icon, its physical address (we use nl2br because in the metadata this is a multi line entry with carriage returns this function makes sure we use &lt;br /&gt; tags instead on output).</p>
<p>Line (09) ends the map pin bubble with a hyperlink to the organization&#8217;s web site&#8211; I see here in my code I should have put an if statement there to make sure we dont do output if there is no URL.</p>
<p>After closing out the map data structure in line (10), we also append the physical address below the map in line (14).</p>
<p>I was rather stoked to get this map part working, because it is automatic. One a previous project, I was making google maps by hand and embedding the code into pages. Here it just happens as long as I feed it some coordinates.</p>
<p>But more bells went off as I saw the structure for setting up the map- I could create a dynamic map that put all the organizations on a map&#8211; like this <a href="http://midea.nmc.org/map/">http://midea.nmc.org/map/</a></p>
<p><a href="http://midea.nmc.org/map/"><img src="http://cogdogblog.com/wp-content/uploads/2010/07/midea-map.jpg" alt="" title="midea-map" width="500" height="366" class="aligncenter size-full wp-image-5322" /></a></p>
<p>Not only do I have a map, but using similar approaches to before, on the right, I have a sidebar with an A-Z listgin of all organizations, and since there was empty space below the map, I added some code that would output the info for one organization chosen at random each time the page loads.</p>
<p>Now I could have used the same approach on the big map as the single organization map listed above- to build the &lt;dl&gt; list by hitting the database for all organizations and looping through to find the latitude/longitude, but this seemed a lot of database processing on each page load. </p>
<p>I saw a clever end around- I could generate this content as an external text file, and then I just need to use a PHP include statement to insert the map data into the page for the plugin to process. </p>
<p>This is created as a WordPress Page content type, and I made a special template for it, making it one of the simplest looking Loops</p>
<pre class="brush: php">
&lt;?php if (have_posts()) : while (have_posts()) : the_post(); ?&gt;
  &lt;h2&gt;&lt;?php the_title(); ?&gt;&lt;/h2&gt;
  &lt;?php include TEMPLATEPATH . &#039;/data/mapdata.inc&#039;?&gt;
  &lt;?php the_content(); ?&gt;

&lt;?php endwhile; endif; ?&gt;
</pre>
<p>Actually the only &#8220;content&#8221; on the age is the one sentence that is below the map. The trick was realizing I have code in my <strong>functions.php</strong> file <strong>midea_save_post()</strong> that is triggered after any content is saved, namely for my custom content types so it can know to save any meta data added&#8211; we did that in the post n=on <a href="http://cogdogblog.com/2010/06/06/put-on-your-hazmat-suits/">adding the meta data field editing boxes</a>.</p>
<p>So I only have to update this map data file if there has been an edit to the org (organization) content type- menaning someone may have added or updated thee latitude longitude.</p>
<p>In that function, we just add a new temp variable, <strong>$midea_update_map_fla</strong>g,  to track of an org content type was updated:</p>
<pre class="brush: php">
$midea_update_map_flag = false;

if ($post-&gt;post_type == &quot;org&quot;) {

	$my_fields = midea_org_fields(); // fields for this content type
	$post_flag = &#039;org&#039;;
	$midea_update_map_flag = true;
   :
   :
   :
</pre>
<p>And after the code for updating all metadata, we trigger a call to a new function, but only if we need to change the map data. This had to come after the meta-date updates in case one of the updates for an org content type was the latitude/longitude.</p>
<pre class="brush: php">
// now update the cache file for the map data of all orgs
if ($midea_update_map_flag) {
	midea_update_map_data();
}
</pre>
<p>The function that updates the map data is going to run its own Loop, keep a running variable with the contents we need to save to our static file, and then write to the file.</p>
<p>It is almost identical to our code above, except we are going to loop through all the data for all organizations in the database.</p>
<pre class="brush: php">
function midea_update_map_data($zoom=3,$lat=30.2632,$long=-97.7406) {
	/* called on saving of an org post type tp create the map data to chart
	   all museum organization. Default settings are centered on Austin
	*/

	$my_query = &#039;post_type=org&amp;post_status=publish&amp;posts_per_page=-1&#039;;

	$getOrgs = new WP_Query();
	$getOrgs-&gt;query($my_query);

	$map_data = &#039;&lt;dl style=&quot;visibility: hidden;&quot; title=&quot;googlemap;w:590;h:400&quot;&gt;
        &lt;dt&gt;&lt;a href=&quot;http://maps.google.com/?z=&#039; . $zoom . &#039;&amp;ll=&#039; . $lat . &#039;,&#039; . $long . &#039;&amp;om=1&quot;&gt;&lt;img src=&quot;/images/map-loader.gif&quot; alt=&quot;&quot; width=&quot;126&quot; height=&quot;22&quot; align=&quot;middle&quot;&gt;&lt;br /&gt;(map loading...)&lt;/dt&gt;&#039; . &quot;\n&quot;;

	while ($getOrgs-&gt;have_posts()) : $getOrgs-&gt;the_post();
		$id = get_the_ID();

		$my_fields = midea_org_fields(); // fields for this content type

		foreach ($my_fields as $key =&gt; $value) {
			// load values into our array
			$my_fields[$key] = get_post_meta($id, &#039;midea-org-&#039; . $key, true);
		}	

		if ($my_fields[&#039;lat&#039;] != &#039;&#039;) {
			 $map_data .= &#039;&lt;dt&gt;&lt;a href=&quot;http://maps.google.com/?ll=&#039; .  $my_fields[&#039;lat&#039;] . &#039;,&#039; . $my_fields[&#039;long&#039;] . &#039;&quot; title=&quot;marker&quot;&gt;map&lt;/a&gt;&lt;/dt&gt;&#039; . &quot;\n&quot; . &#039;&lt;dd&gt;&lt;h4&gt;&#039; . get_the_title($id) . &#039;&lt;/h4&gt;&#039; . get_the_image(array( &#039;echo&#039; =&gt; false, &#039;height&#039; =&gt; 60, &#039;width&#039; =&gt; 60,)) .   nl2br($my_fields[&#039;address&#039;]) . &#039;&lt;br /&gt;&lt;a href=&quot;&#039; . $my_fields[&#039;web&#039;] . &#039;&quot;&gt;web site&lt;/a&gt;&lt;br /&gt;&lt;a href=&quot;&#039; . get_permalink($id) . &#039;&quot;&gt;MIDEA profile&lt;/a&gt;&lt;/dd&gt;&#039; . &quot;\n&quot;;
		 }
	endwhile;

	$map_data .= &#039;&lt;/dl&gt;&#039;;

	// update the file with the code for the map
	$DATAFILE = fopen(TEMPLATEPATH . &#039;/data/mapdata.inc&#039;,&quot;w+&quot;);
	fwrite($DATAFILE, $map_data) or die(&quot; failed writing $DATAFILE&quot;);
	fclose($DATAFILE);
}
</pre>
<p>Since the organizations are not going to be updated all that frequently, this ought to reduce some small amount of load on the database, since we only have to do this work when there has been a change in any of the location data.</p>
<p>So that is a taste of the ways I have used WordPress Content types for our organization data. Again, you can attach whatever kinds of data you want/need, and pull it as needed for output (or filter in it using the query structure).  I was able to easily make a page that lists <a href="http://midea.nmc.org/org-facebook">all the organizations that have twitter accounts</a> and <a href="http://midea.nmc.org/org-facebook">ones that have facebook pages</a> (and links to them). This pages run a loop to fetch all organizations, but on looping through them, it skips any that lack the data being outputted.</p>
<p>These content types open the skies on what is possible, but as you can see, it take some wrangling to sort out the queries and loops. It ain&#8217;t for the feint hearted, and I dont expect anyone to follow my logic or method&#8211; but this was really meant to show what is possible.</p>
<p>We can map anything to anything.</p>
<p><a title="How Do We Map Our Civilization?" href="http://flickr.com/photos/rafolio/3375223943/"><img src="http://farm4.static.flickr.com/3473/3375223943_961a4b124a.jpg" /></a><br /><small><a title="How Do We Map Our Civilization?" href="http://flickr.com/photos/rafolio/3375223943/">cc licensed flickr photo</a> shared by <a href="http://flickr.com/people/rafolio/">rafolio</a></small></p>
<p>The last post would be looking at our event content type, and see how we work with and around date and time. You can find all the posts in this series at <a href="http://cogdogblog.com/2010/07/23/roundup-wp3/">http://cogdogblog.com/2010/07/23/roundup-wp3/</a></p>
<p>Keep on pressing words!</p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2010/07/22/wp3-custom-content-map/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Dressing up and Displaying WordPress 3 Custom Post Content (a)</title>
		<link>http://cogdogblog.com/2010/07/21/dressing-up-wp3-content-a/</link>
		<comments>http://cogdogblog.com/2010/07/21/dressing-up-wp3-content-a/#comments</comments>
		<pubDate>Thu, 22 Jul 2010 05:57:24 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Blog Pile]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp3]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=5271</guid>
		<description><![CDATA[cc licensed flickr photo shared by Thomas Hawk Yikes, where did the time go? I&#8217;ve let slide the next piece in my now fractured series on using the custom post types added in WordPress 3.0. As a recap, what I am sharing is what I learned back in April (using the beta version of WP3) to create the NMC MIDEA web site. This site had a need to create, besides the usual page and blog post types, other content for projects, organizations, and events, each with its own extra custom meta-data. Before I go crazy with PHP here, I want to note that this will get technical and is written really for myself as a set of notes and others who have no hesitation to roll up their sleeves and muck around with templates and code. This is not the optimal way to use the new WordPress features, and it [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Carry Me Home" href="http://flickr.com/photos/thomashawk/362366934/"><img src="http://farm1.static.flickr.com/139/362366934_84eb90f281.jpg" /></a><br /><small><a title="Carry Me Home" href="http://flickr.com/photos/thomashawk/362366934/">cc licensed flickr photo</a> shared by <a href="http://flickr.com/people/thomashawk/">Thomas Hawk</a></small></p>
<p>Yikes, where did the time go? I&#8217;ve let slide the next piece in my now fractured series on using the custom post types added in WordPress 3.0. As a recap, what I am sharing is what I learned back in April (using the beta version of WP3) to create the NMC <a href="http://midea.nmc.org/">MIDEA web site</a>. This site had a need to create, besides the usual page and blog post types, other content for <a href="http://midea.nmc.org/proj">projects</a>, <a href="http://midea.nmc.org/org">organizations</a>, and <a href="http://midea.nmc.org/event">events</a>, each with its own extra custom meta-data.</p>
<p>Before I go crazy with PHP here, I want to note that this will get technical and is written really for myself as a set of notes and others who have no hesitation to roll up their sleeves and muck around with templates and code. This is not the optimal way to use the new WordPress features, and it is my expectation that what I am writing will be done more easily (and probably more efficiently) by plugins. I did it &#8220;my way&#8221; more to show what was possible.</p>
<p>And also, I am far from a deep WP engineering expert- I just keep tinkering til it works, and you can bet there are better ways to code than what I did.</p>
<p>In this series, I started with <a href="http://cogdogblog.com/2010/05/28/wordpress-3-pt1/">an overview of the plans</a> for the MIDEA project, we set up the <a href="http://cogdogblog.com/2010/05/28/setting-up-custom-contenttypes/">places to create the new content types</a>, and introduced the benefits <a href="http://cogdogblog.com/2010/05/29/those-darn-kids/">using child themes</a>.  Then we took it up a notch to <a href="http://cogdogblog.com/2010/06/06/put-on-your-hazmat-suits/">dive into the code to create the interface and data elements to add/edit the custom meta data</a> for each of the three content (post) types.</p>
<p>Now I turn to what it took to integrate and display stuff form my custom content into the MIDEA site. This requires a bit of understanding of what is called The Loop in WordPress.</p>
<p><a title="Loopy" href="http://flickr.com/photos/shnakepup/3492793246/"><img src="http://farm4.static.flickr.com/3561/3492793246_251eb3a4c3.jpg" /></a><br /><small><a title="Loopy" href="http://flickr.com/photos/shnakepup/3492793246/">cc licensed flickr photo</a> shared by <a href="http://flickr.com/people/shnakepup/">jayRaz</a></small></p>
<p>Understanding a bit of <strong>The Loop</strong> is a key bit of foundation for understanding templates (see the <a href="http://codex.wordpress.org/The_Loop_in_Action">Loop in Action</a> from the WordPress Codex). This is my take on it.</p>
<p>Just about everything WordPress does starts with it talking to the database, and saying, &#8220;get me this kind of stuff&#8221; (stuff might be recent blog posts, all posts with a certain tag, the results of a search, a single page, etc). When the database returns something, WordPress then says,  &#8220;Hmmm, for each one of this, pull these bits out and display them&#8221; This is a loop, sometimes The Loop.</p>
<p>Typically you get a built in Loop w/o needing to strictly ask for it- the front page is &#8220;give my the 10 most recent blog posts&#8221;, and other variations are set up by the context of whatever is being served (e.g. in an archive from December 2009, you get just those posts).</p>
<p>The most basic version of The Loop runs something like:</p>
<pre class="brush: php">
if (have_posts()) :
   while (have_posts()) :
      the_post();
      // do something for each post
   endwhile;
endif;
</pre>
<p>In our code &#8220;posts&#8221; is the placeholder for whatever we are fetching- blog posts, pages, custom content types. We first need to check that we have anything, and them if so, we cycle through them (while&#8230;)&#8211; <strong>the_post()</strong> grabs all the info about the item, (title, description, link, date posted, etc). Inside that are, we have a whole series of built  in functions we can use to display and wrap formatting around- <strong>the_title()</strong> , <strong>the_author()</strong>, <strong>the_permalink()</strong>&#8230;</p>
<p>In my MIDEA project, and maybe in all, I used four variations of looping.</p>
<p>(1) The <strong>default loop</strong> for whatever kind of content we are working with (front page, category page, search results). This uses the structure listed above</p>
<p>(2) <strong>Change the condition of the main loop</strong> with <a href="http://codex.wordpress.org/Template_Tags/query_posts">query_posts</a>. This basically makes a modification to whatever is the default behavior, so you can do things like display a different number of posts, or limit to a category.</p>
<pre class="brush: php">
//The Query
query_posts(&#039;posts_per_page=3&#039;);

//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
  // do stuff for each post
endwhile; else:
 // what to do if there is nothing found
endif;

//Reset Query
wp_reset_query();
</pre>
<p>In this case, <strong>query_posts</strong> is used to change the number things retrieved from its defaults to be exactly 3. Note the wise use of <strong>wp_reset_query()</strong> in case we want to use the default loop conditions elsewhere.</p>
<p>(3) <strong>Completely customize the parameters of the Loop</strong>, or make a second (or third, or fourth loop). In this case, we are not working with the default Loop for the current content, ut we are designing our own. This often happens in sidebars and/or widgets- Little Loops are done to get recent comments, recent posts, etc.</p>
<p>You will see a lot of these below, but the method uses the <a href="http://codex.wordpress.org/Function_Reference/WP_Query">WP_Query function</a> to set up the database request, and a slightly different variant of the default Loop structure.</p>
<p>An example of this is what I used on some sidebars to get the most recent blog post (setting showposts=1) from the news category:</p>
<pre class="brush: php">
$my_query = &quot;showposts=1&amp;amp;amp;category_name=news&quot;;
$showNews = new WP_Query();
$showNews-&gt;query($my_query);

while ($showNews-&gt;have_posts()) : $showNews-&gt;the_post(); ?&gt;

 // do stuff to display here
endwhile;
wp_reset_query();
</pre>
<p>The <a href="http://codex.wordpress.org/Template_Tags/query_posts">Query_posts()</a> reference page lists all the things you can use ti set up the query.</p>
<p>(4) The <strong>get down and dirty call the database query</strong> direct method. There are just some things you cannot get to via <strong>query_posts()</strong>. In the MIDEA site, I had to use this for anything that defined a loop based on the custom content meta data, or another area where I wanted to use some MySQL magic to make sure organization names that started with &#8220;The&#8221; were not alphabetized in the T&#8221;s.</p>
<p>Now I&#8217;m going to outline the places I used this and some of the loops used. I&#8217;m not going to detail what goes on inside the loops- this is really dictated by your template and your knowledge of how to pull and use the variables you can get for a loop item.</p>
<p>And this post is going to scroll for about 1000 feet.</p>
<h2>The Projects Content Type</h2>
<p>One of our custom content types represents projects, each with extra meta data of a URL, an id for the organization who it is affiliated with, and a rating of 1-5 (5 being highest). The general page for projects is a gallery, <a href="http://midea.nmc.org/proj">http://midea.nmc.org/proj</a> with the following parts that needed custom code. The right side is a sidebar- the main content is set out by template file <strong>proj.php</strong>. Our function<strong> midea_template_redirect()</strong> we added last time, sets up for each content type, proj for projects, org for organizations, event for events, the name for its template (proj.php,org.php,event.php) as well as the templates for a single item of the type- single-proj.php, single-org.php, and single.event.php.</p>
<p>So here is the skeleton of the proj,php display:</p>
<p><a href="http://midea.nmc.org/proj"><img src="http://cogdogblog.com/wp-content/uploads/2010/07/midea-proj.jpg" alt="" title="midea-proj" width="500" height="677" class="alignnone size-full wp-image-5294" /></a></p>
<p>(A) Each time the page loads we get 9 projects selected at random, and we want ones that are rated higher than 2 (we have some that are in progress and they get a 1 rating). The display in the squares was just used elsewhere in the Modularity template. But the idea is for a visual gallery puling projects at random.</p>
<p>(B) Each time we reload the page, we pull in the full information for one selected featured project. The way this is done is to select another random project, but only ones that are rated 4 or higher. In addition, we need to track the ones we used in (A) so we don&#8217;t repeat.</p>
<p>(C) The sidebar provides an alphabetical list of all projects in the site. This might be the easiest thing to do except for one wrinkle we discovered elsewhere on the project; the problem of projects (and organizations) that start with &#8220;The&#8221;, like &#8220;The Blue Dog&#8221; &#8212; we do not want to alphabetize on &#8220;T&#8221; but on &#8220;B:. This required a bit of custom mySQL in our loop.</p>
<p>Are you ready to loop?</p>
<p>For (A), the grid of 9 projects, we use the wordpress capability to modify the &#8220;stock&#8221; selection for the Loop. Even though this is a custom type, WordPress still provides a built in loop that will select projects in reverse chronological order. We don&#8217;t want that, so we use the method of modifying the main loop:</p>
<pre class="brush: php">
query_posts(&#039;post_type=proj&amp;amp;orderby=rand&amp;amp;meta_key=midea-proj-rating&amp;amp;meta_value&gt;2&amp;amp;posts_per_page=9&#039;);

$randy_projects = array(); // keep a rolling list of projects listed here
</pre>
<p>This php above the loop logic says, &#8220;instead of what you normally do, Loop, I want t make sure I get only content that are of type proj, they should be chosen at random rather than reverse chronologically, and I want you to look at the meta data of rating, and only get me ones with a value of 2 or greater. Oh, and I only want 9 of these&#8221;</p>
<p>I also set up a variable to keep track of the 9 that are chosen, and will store these in an array.</p>
<p>We then just run our basic wordpress loop, and use it to display the image for the project and link it. The only thing we add inside the loop is something to track the project ids, using the function <strong>get_the_ID()</strong>, which in a loop returns the id number for this item.</p>
<pre class="brush: php">
$randy_projects[] = get_the_ID();
</pre>
<p>That was easy, eh?</p>
<p>Now we move on to grabbing our featured project. We need to set up arguments to send to the wp_query() function:</p>
<pre class="brush: php">
$args = array(
	&#039;post_type&#039; =&gt; &#039;proj&#039;,
	&#039;posts_per_page&#039; =&gt; 1,
	&#039;orderby&#039;=&gt; &#039;rand&#039;,
	&#039;meta_key&#039; =&gt; &#039;midea-proj-rating&#039;,
	&#039;meta_value&#039; =&gt; &#039;4&#039;,
	&#039;meta_compare&#039; =&gt; &#039;&gt;=&#039;,
	&#039;post__not_in&#039; =&gt; $randy_projects,
);
$blog_query = new WP_Query($args);
</pre>
<p>It is similar but different to the first one we did. On this one, we only want to grab 1, its still random, but in this case we use the options to ask for ones with a rating of 4 or higher, and the trickier part, use <strong>post_not_in</strong> to make sure it is not among the ones in our 9 item array. Got it? the last line runs the query. From here we can run our loop.</p>
<p>Except&#8230; we need to look back at our functions.php file. We need a few utility functions to deal with our project data. The first one, <strong>midea_get_project_info($id)</strong> is used to fetch the data for a project (identified by $id). Now what our database stores for this content type is a url, an id number for the organization, and an integer rating. We need to fetch this info, but also, we need to look up the actual name of an organization.</p>
<pre class="brush: php">
function midea_get_project_info($id) {
	$my_proj_fields = midea_proj_fields(); // fields for this content type

	foreach ($my_proj_fields as $key =&gt; $value) {
		// load values into our array
		$my_proj_fields[$key] = get_post_meta($id, &#039;midea-proj-&#039; . $key, true);
	}

	$my_proj_fields[&#039;orgname&#039;] = ($my_proj_fields[&#039;org&#039;] &gt; 0) ? get_the_title((int)$my_proj_fields[&#039;org&#039;]) : &#039;MIDEA&#039;;
	$my_proj_fields[&#039;id&#039;] = $id;
	return ($my_proj_fields);
}
</pre>
<p>First we set up an empty array <strong>$my_proj_fields</strong> to store the data. We can then loop over our meta-data names to fill in their values using <strong>get_post_meta()</strong>. Next, since all we have is an id for the organization (stored as $my_proj_fields['org']) we can add a string name (&#8216;orgname&#8217;) to our data array by using the nifty wordpress function<strong> get_the_title()</strong> we just pass it the idea of a post or content type, and we get a string back, </p>
<p>Lastly, because we might need ti elsewhere, we add another data element to store the id number of this project.</p>
<p>Now we are back to our proj.php, where we have set up the query to get one featured project. We still run a loop, although it will go through once because we asked only for one item. Below IU will show this code, but it has the template formatting simplified to make it easier to follow</p>
<pre class="brush: php">
&lt;?php if ($blog_query-&gt;have_posts()) : while ($blog_query-&gt;have_posts()) :  $blog_query-&gt;the_post(); ?&gt;

  &lt;?php $pinfo = midea_get_project_info(get_the_ID());?&gt;

  &lt;p&gt;&lt;?php echo $pinfo[&#039;orgname&#039;]?&gt;&lt;/p&gt;

  &lt;h2&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot;&gt;&lt;?php the_title(); ?&gt;&lt;/a&gt;&lt;/h2&gt;
  &lt;div class=&quot;entry&quot;&gt;
  &lt;?php the_content(); ?&gt;

  &lt;?php if ($pinfo[&#039;url&#039;]):?&gt;
  &lt;p&gt;Project URL: &lt;a href=&quot;&lt;?php echo $pinfo[&#039;url&#039;]?&gt;&quot; target=&quot;_blank&quot;&gt;&lt;?php echo $pinfo[&#039;url&#039;]?&gt;&lt;/a&gt;&lt;/p&gt;
  &lt;?php endif; ?&gt;

  &lt;p&gt;&lt;a href=&quot;&lt;?php echo get_permalink($pinfo[&#039;org&#039;])?&gt;&quot;&gt;Learn more about &lt;?php echo $pinfo[&#039;orgname&#039;]?&gt;...&lt;/a&gt;&lt;/p&gt;

&lt;?php endwhile; endif; wp_reset_query(); ?&gt;
</pre>
<p>What we have in line (01) is a loop but using the variables defined in our query. In line (3) we get our project info, and in line (5) we echo the organization name as lead in to line (7) where we echo the project title. Line (09) just outputs whatever is written in the body for the project entry (like the body of a blog post).  Lines (11-13) echo the URL if there is one. Line 15 adds a link to the organization the project is associated with (note the use of the get_permalink() function, very handy of you know an item&#8217;s id).</p>
<p>Lastly, note in line (17) as we end our loops controls, we reset the main query (this is the default query WordPress generates for this content type, this is a good practice to do if other parts of your template need access to its data).</p>
<p>Now we move on to our sidebar. We are using a special sidebar template for the projects page, so we call it via:</p>
<pre class="brush: php">
&lt;?php get_sidebar(&#039;proj&#039;); ?&gt;
</pre>
<p>which tells WordPress instead fg using the template <strong>sidebar.php</strong> to use <strong>sidebar-proj.php</strong>.</p>
<p>This is the one where we want to list all projects in alpha order, but do the special ordering thing to skip alphabetizing on &#8220;The&#8221;. To do this we need to run the Type 4 variations of The Loop, where we throw it a full mySQL query. To do this you need to have a sense of how the WP database tables are set up. </p>
<pre class="brush: php">
&lt;ul&gt;
&lt;?php
global $wpdb;
global $post;

// custom query to get posts of type proj in alpha order by trimming the leading &quot;The &quot; in a title
$projposts = $wpdb-&gt;get_results(&quot;SELECT wposts.*
   FROM $wpdb-&gt;posts as wposts
   WHERE wposts.post_type = &#039;proj&#039; and wposts.post_status=&#039;publish&#039;
   ORDER BY TRIM(LEADING &#039;The &#039; FROM wposts.post_title)&quot;, OBJECT);
?&gt;
&lt;?php if ($projposts): ?&gt;

&lt;?php foreach ($projposts as $post): ?&gt;
&lt;?php setup_postdata($post); ?&gt;
	&lt;li&gt;&lt;a href=&quot;&lt;?php the_permalink();?&gt;&quot;&gt;&lt;?php the_title()?&gt;&lt;/a&gt;&lt;/li&gt;
 &lt;?php endforeach;  endif;?&gt;
&lt;/ul&gt;
</pre>
<p>I am not sure why, but I had to declare the global variables in lines (02-03).  Lines (07-10) are are raw mySQL query to get content of type proj, only ones that are published, and to list them in alpha order by title, but to trim any &#8220;The&#8221; from the ordering. I cant explain it, but found it used in some other project. Lines (12-15) set up the loop in the manner you have to do for using this method. Again, thats all I know except it works, and populates our loop with data we can use like any other loop.</p>
<p>In this case we are doing a simple list with a link on a title.</p>
<p>So next we have to code up the content for a single project. The parts of this are shown schematically:<br />
<img src="http://cogdogblog.com/wp-content/uploads/2010/07/midea-single-proj.jpg" alt="" title="midea-single-proj" width="500" height="362" class="alignnone size-full wp-image-5300" /></p>
<p>(A) the basic content for this project, we add a few extra bits like the organization name and the URL.</p>
<p>(B) On our sidebar, we list/link the organization this project is associated with, using the icon associated with the organization content type.</p>
<p>(C) We list other projects this organization has on our site.</p>
<p>This template again is <strong>single-proj.php</strong>.</p>
<p>The main part of it (A) is given by the basic loop, so we dont have to do anything fancy; and the format is pretty much in the same manner we did above for the featured project (B) on the projects gallery above.</p>
<p>We are going to request a custom sidebar template <strong>sidebar-single-proj.php</strong>. First we do (B) getting the organization information displayed.</p>
<pre class="brush: php">
&lt;?php
$pinfo = midea_get_project_info(get_the_ID());

// set up query to get org data
$my_query = &quot;post_type=org&amp;p=&quot; . $pinfo[&#039;org&#039;] . &quot;&amp;posts_per_page=1&quot;;
$showOrg = new WP_Query();
$showOrg-&gt;query($my_query);
while ($showOrg-&gt;have_posts()) : $showOrg-&gt;the_post();
	get_the_image( array( &#039;link_to_post&#039; =&gt; true, &#039;class&#039; =&gt; &#039;thumbnail&#039; ) );
endwhile;
?&gt;
&lt;p&gt;&lt;a href=&quot;&lt;?php the_permalink();?&gt;&quot;&gt;&lt;?php echo $pinfo[&#039;orgname&#039;]?&gt;&lt;/a&gt;&lt;/p&gt;
</pre>
<p>In line (02) we again use our function to get the project information, based on the current ID in the main loop. Next in lines (05-08) we run another loop to access the data for the organization associated with this project. This template has a built in function to get the icon image associated with a piece of content, which is what we output via <strong>get_the_image()</strong> function inside the one item loop. And in line (12) we echo a link to the organization.</p>
<p>Now we move on to (C) to get all other projects associated with this same organization.</p>
<pre class="brush: php">
&lt;ul&gt;
&lt;?php
$my_query = &#039;post_type=proj&amp;posts_per_page=-1&amp;orderby=title&amp;order=ASC&amp;meta_key=midea-proj-org&amp;meta_value=&#039; . $pinfo[&#039;org&#039;];

$showProj = new WP_Query();
$showProj-&gt;query($my_query);
?&gt;

&lt;?php if ($showProj-&gt;have_posts()) : while ($showProj-&gt;have_posts()) : $showProj-&gt;the_post(); ?&gt;

	&lt;?php if (get_the_ID() == $pinfo[&#039;id&#039;]) : ?&gt;
		&lt;li class=&quot;selected&quot;&gt;&amp;raquo; &lt;strong&gt;&lt;?php the_title();?&gt;&lt;/strong&gt;&lt;/li&gt;
	&lt;?php else: ?&gt;
		&lt;li&gt;&lt;a href=&quot;&lt;?php the_permalink();?&gt;&quot;&gt;&lt;?php the_title()?&gt;&lt;/a&gt;&lt;/li&gt;
	&lt;?php endif ?&gt;

&lt;?php endwhile; endif; ?&gt;
&lt;/ul&gt;
</pre>
<p>In lines (03-06) we set up a new wp_query() to find all projects (posts_per_page value of -1 means all of &#8216;em), and we will list them in alpha order by title (I&#8217;m lazy and not doing the fancy order by stripping &#8220;The&#8221;&#8230;).  The key here is the last bits where we tell the query to select just ones where the meta data for the project matches the organization ID of the main project being displayed. In lines (11-13) we are just doing the output, but if the project id matches the one this page represents, we dont provide a link, and add a CSS class so we can style it to indicate that its the one on this page.</p>
<p>This is just the basic code for one of the three content types I created. I f I write them all up, my poor blog (and body) will fall over. So I will have to come back to talk about the code used for the organizations (and the nifty plugin that inserts a google map based on latitude and longitude custom data) and for the event (where we have fun sorting by dates).</p>
<p>It&#8217;s a lot to explain! And this is not even to mention the various sidebars where I do things like list three random projects, but the first one is always one rated 5 and the others are all rated 3 and above. Maybe that&#8217;s an exercise for the student.</p>
<p>More to come. I promise/hope. </p>
<p>You can find all the posts in this series at <a href="http://cogdogblog.com/2010/07/23/roundup-wp3/">http://cogdogblog.com/2010/07/23/roundup-wp3/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2010/07/21/dressing-up-wp3-content-a/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
		<item>
		<title>Put on Your HazMat Suits- Setting Up Metadata For WordPress 3 Custom Content Types</title>
		<link>http://cogdogblog.com/2010/06/06/put-on-your-hazmat-suits/</link>
		<comments>http://cogdogblog.com/2010/06/06/put-on-your-hazmat-suits/#comments</comments>
		<pubDate>Sun, 06 Jun 2010 16:03:39 +0000</pubDate>
		<dc:creator>Alan Levine aka CogDog</dc:creator>
				<category><![CDATA[Blog Pile]]></category>
		<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wp3]]></category>

		<guid isPermaLink="false">http://cogdogblog.com/?p=5125</guid>
		<description><![CDATA[cc licensed flickr photo shared by StayRAW In the previous posts in this series of using the new WordPress 3 Custom Post Types (I keep calling them custom content types, same thing), I overviewed the plans for the MIDEA web site, we set up the places to create the new content types, and diverged into some set up magic using child themes. That was the easy stuff. Now I get into the parts I had to more or less invent on my own (well, with some good leg ups from others)- how to add all of the form field elements to my new content types so I could add extra information to them. This are fields for say, my Organization content types, to have a field to enter their web site address, latitude/longitude for doing some mapping, etc. Now I did this all in custom code, and as hopefully I [...]]]></description>
			<content:encoded><![CDATA[<p><a title="Hazmat Rapper" href="http://flickr.com/photos/hqas/3816355599/"><img src="http://farm4.static.flickr.com/3443/3816355599_e2dfab5958.jpg" /></a><br /><small><a title="Hazmat Rapper" href="http://flickr.com/photos/hqas/3816355599/">cc licensed flickr photo</a> shared by <a href="http://flickr.com/people/hqas/">StayRAW</a></small></p>
<p>In the previous posts in this series of using the new WordPress 3 Custom Post Types (I keep calling them custom content types, same thing), I <a href="http://cogdogblog.com/2010/05/28/wordpress-3-pt1/">overviewed the plans</a> for the <a href="http://midea.nmc.org/">MIDEA web site</a>, we set up the <a href="http://cogdogblog.com/2010/05/28/setting-up-custom-contenttypes/">places to create the new content types</a>, and diverged into some set up magic <a href="http://cogdogblog.com/2010/05/29/those-darn-kids/">using child themes</a>.</p>
<p>That was the easy stuff. Now I get into the parts I had to more or less invent on my own (well, with some good leg ups from others)- how to add all of the form field elements to my new content types so I could add extra information to them. This are fields for say, my Organization content types, to have a field to enter their web site address, latitude/longitude for doing some mapping, etc.</p>
<p>Now I did this all in custom code, and as hopefully I stated earlier, I have no expectation that this is the way it will be dobe going forward. I foresee a raft of new plugins coming in that will take away this manual code layer, and maybe, when WordPress 3.7 or 4.1 comes out, it might be built in. I have no idea. I did this to show the WP platform can do this.<br />
<span id="more-5125"></span><br />
What we are working towards is creating a place like this for my Organization content type when I create or edit new content:</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/06/midea-wp-edit3-org.jpg" alt="" title="midea-wp-edit3-org" width="500" height="352" class="alignnone size-full wp-image-5126" /></p>
<p>So returning to our <strong>functions.php</strong> editing, the first thing we need to do is to activate the bits to turn on the lights inside the WordPress dashboard; adding in the top sequence of our code:</p>
<pre class="brush: php">
// Admin interface init
add_action(&quot;admin_init&quot;, &#039;midea_admin_init&#039;);
</pre>
<p>which again, puts a &#8220;hook&#8221; in the wordpress flow to include the admin functions I want to add. This new function will in turn set up what is needed to add &#8220;metaboxes&#8221; to the wordpress editing pane- these are my own editing form elements. </p>
<pre class="brush: php">
function midea_admin_init() {
	// Custom meta boxes for the custom post types
	add_meta_box(&quot;org-meta&quot;, &quot;MIDEA Organization Details&quot;, midea_org_options, &#039;org&#039;, &#039;normal&#039;, &#039;low&#039;);
	add_meta_box(&quot;proj-meta&quot;, &quot;MIDEA Project Details&quot;, midea_proj_options, &#039;proj&#039;, &#039;normal&#039;, &#039;low&#039;);
	add_meta_box(&quot;event-meta&quot;, &quot;MIDEA Event Details&quot;, midea_event_options, &#039;event&#039;, &#039;normal&#039;, &#039;low&#039;);
}
</pre>
<p>Here we use the <a href="http://codex.wordpress.org/Function_Reference/add_meta_box">add_meta_box function</a> to do the work. The variables I am passing, for say the Organization types are:</p>
<ul>
<li><strong>org-meta</strong> a unique id just to identify the metabox, likely used to track in the database. I named mine in line with the content type slugs.</li>
<li><strong>MIDEA Organization Details</strong> the title that will appear in the top of the box</li>
<li><strong>midea_org_options</strong> the &#8220;callback function&#8221;, meaning my own function that will be used to do the set up work.</li>
<li><strong>org</strong> the internal name I set up previously for this content type.</li>
<li><strong>normal</strong> the placement of the metabox- normal is the main pane (below the post editing field). I could also put it on the right side if I use the value &#8216;side&#8217;</li>
<li><strong>low</strong> I&#8217;m not sure what this does, the codex says &#8220;The priority within the context where the boxes should show&#8221;; perhaps if it is set to &#8220;high&#8221; it might appear above the main post editing field?</li>
</ul>
<p>As something we will need quite a bit, I came up with this approach to define the meta data fields for my content types (as opposed to tossing around global variables). I call one of these when I need to create a code &#8220;container&#8221; for each content type&#8217;s meta data, and later we will use database calls to put in the actual values. But as a utility, this simply returns an array of the metadata, all set with initial empty string values.</p>
<pre class="brush: php">
function midea_org_fields() {
	// provide array of names for fields in organization content type;
	// init with empty valyes to fill later
	return array(&#039;web&#039; =&gt; &#039;&#039;, &#039;facebook&#039; =&gt; &#039;&#039;, &#039;twitter&#039; =&gt; &#039;&#039; , &#039;address&#039; =&gt; &#039;&#039;, &#039;lat&#039; =&gt; &#039;&#039;, &#039;long&#039; =&gt; &#039;&#039;, &#039;tagline&#039; =&gt; &#039;&#039;);
}

function midea_proj_fields() {
	// provide array of names for fields in project content type;
	// init with empty valyes to fill later
	return array(&#039;url&#039; =&gt; &#039;&#039;, &#039;org&#039; =&gt; &#039;&#039;, &#039;rating&#039; =&gt; &#039;&#039;);
}

function midea_event_fields() {
	// provide array of names for fields in event content type;
	// init with empty valyes to fill later
	return array(&#039;start&#039; =&gt; &#039;&#039;, &#039;end&#039; =&gt; &#039;&#039;, &#039;url&#039; =&gt; &#039;&#039;, &#039;location&#039; =&gt; &#039;&#039;);
}
</pre>
<p>Now we are ready to create some metaboxes! For our Organization type, this is the callback function that creates the pieces shown in the image above.</p>
<pre class="brush: php">
function midea_org_options() {
	// add meta data fields for a MIDEA organization content type
	global $post;

	// Use nonce for verification
	echo &#039;&lt;input type=&quot;hidden&quot; name=&quot;midea_noncename&quot; id=&quot;midea_noncename&quot; value=&quot;&#039; .
    wp_create_nonce( &#039;MyNonceCode&#039; ) . &#039;&quot; /&gt;&#039;;

	$my_fields = midea_org_fields(); // fields for this content type

	foreach ($my_fields as $key =&gt; $value) {
		// load values into our array
		$my_fields[$key] = get_post_meta($post-&gt;ID, &#039;midea-org-&#039; . $key, true);
	}	

	echo &#039;&lt;p&gt;&lt;strong&gt;About&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;label&gt;Tagline&lt;/label&gt; A one sentence blurb that describes what this organization is about:&lt;br /&gt;&lt;textarea name=&quot;midea-org-tagline&quot; rows=&quot;4&quot; cols=&quot;55&quot;&gt;&#039; . $my_fields[&#039;tagline&#039;] . &#039;&lt;/textarea&gt;&lt;/p&gt;&#039;;

	echo &#039;&lt;p&gt;&lt;strong&gt;Online Presence&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;label&gt;web site:&lt;/label&gt;:&lt;br /&gt; &lt;input name=&quot;midea-org-web&quot; size=&quot;60&quot; value=&quot;&#039; . $my_fields[&#039;web&#039;] . &#039;&quot; /&gt;&lt;/p&gt;&#039; . &quot;\n&quot;;
	echo &#039;&lt;p&gt;&lt;label&gt;facebook url&lt;/label&gt;:&lt;br /&gt; &lt;input name=&quot;midea-org-facebook&quot; size=&quot;60&quot;  value=&quot;&#039; . $my_fields[&#039;facebook&#039;] . &#039;&quot; /&gt;&lt;/p&gt;&#039; . &quot;\n&quot;;
	echo &#039;&lt;p&gt;&lt;label&gt;twitter username&lt;/label&gt;:&lt;br /&gt;&lt;input name=&quot;midea-org-twitter&quot; size=&quot;30&quot;  value=&quot;&#039; . $my_fields[&#039;twitter&#039;] . &#039;&quot; /&gt;&lt;/p&gt;&#039; . &quot;\n&quot;;

	echo &#039;&lt;p&gt;&lt;strong&gt;Location&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;label&gt;address&lt;/label&gt;:&lt;br /&gt;&lt;textarea name=&quot;midea-org-address&quot; rows=&quot;2&quot; cols=&quot;55&quot;&gt;&#039; . $my_fields[&#039;address&#039;] . &#039;&lt;/textarea&gt;&lt;/p&gt;&#039; . &quot;\n&quot; . &#039;&lt;p&gt;&lt;label&gt;latitude&lt;/label&gt;: &lt;input name=&quot;midea-org-lat&quot; size=&quot;15&quot;  value=&quot;&#039; . $my_fields[&#039;lat&#039;] . &#039;&quot; /&gt;&#039; . &quot;\n&quot;;
	echo &#039;&lt;label&gt;longitude&lt;/label&gt;: &lt;input name=&quot;midea-org-long&quot; size=&quot;15&quot;  value=&quot;&#039; . $my_fields[&#039;long&#039;] . &#039;&quot; /&gt;&#039; . &quot;\n&quot;;
}
</pre>
<p>So roughly, as a narrative, this is what I am doing. We always have to add this thing I dont fully understand, but is a security to make sure the form data is coming from my own code, a &#8220;nonce&#8221;, a hidden form element. The value you use &#8220;MyNonceCode&#8221; should be any unique string (don&#8217;t use that string, its just a placeholder). If you do not do this, any data you add will not &#8220;stay&#8221;in te database, as WordPress does its own internal cleanups. I think.</p>
<p>Next, I use the functions listed above to get my meta data container, and I cycle through them with the <a href="http://codex.wordpress.org/Function_Reference/get_post_meta">get_post_meta function</a> to grab any values that already exist. If you look carefully, I use the array keys to define the meta data, prefixed in this case with &#8220;midea-org-&#8221; to identify it in the database.</p>
<p>And lastly, I just echo what I need to build the form fields I want. It may be more code clean to use a function approach as described in <a href="http://blog.genuineinteractive.com/post/adding-custom-field-gui-to-custom-post-types-wordpress-3.aspx">Genuine Juice&#8217;s key guide to creating these elements</a> but I knew I would adding some very specific labels to my form elements, and I wanted full control. Hence the HazMat suit.</p>
<p>I&#8217;m not going to march through all the details of my other two content types, but they are included in my sample code below. The extra wrinkles here was for say, my Project content types, as I wanted one meta data element to be a drop down list of organizations I could associate a project with- so this involved writing a function to get all my Organization names and database ids, and build a menu out of that&#8211; isn;t this cool- I use one custom content type as an elemnt to create meta data for another.</p>
<p>And the Events content type had to do some extra work to deal with the date/time entry fields. Instead of creating a series  series of month, day, year, hour, minute fields or using some Javascript widget, I took a simpler approach of a single field and using PHP&#8217;s <strong>strtotime</strong> function to convert it to a unix time stamp, which is what we store in the database. There is a bit of gymnastics here for events- if it is a single day event, we look at the time values to generate start/end times. If it is a multi-day event, we just want to know the month/day/year values.</p>
<p>So for now, we can create the form fields for entering data, but we have to add more code to have WordPress save it all.</p>
<p>We have to add another &#8220;hook&#8221; at the top to tell WordPress do do our extra bits when a post/content type is saved.</p>
<pre class="brush: php">
// Use the save_post action to do something with the data entered
add_action( &#039;save_post&#039;, &#039;midea_save_post&#039;, 1, 2 );
</pre>
<p>The <strong>midea_save_post</strong> is the callback function used when we save anything- I honestly lost track of what the extra variables do (I did read up on it and just used values I saw used in the examples).</p>
<p>So here is the function I made to save my meta post data</p>
<pre class="brush: php">
// When a post is inserted or updated
function midea_save_post()
{
	global $post;

	$post_id = $post-&gt;ID;

	// verify this came from the our screen and with proper authorization,
	// because save_post can be triggered at other times

	if ( !wp_verify_nonce( $_POST[&#039;midea_noncename&#039;], &#039;MyNonceCode&#039;)) return $post_id;

	// verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
	// to do anything
	if ( defined(&#039;DOING_AUTOSAVE&#039;) &amp;amp;&amp;amp; DOING_AUTOSAVE ) return $post_id;

	// flag to set if we need to deal with custom post type
	// not exactly sure if we need this, but it cannot hurt

	$post_flag = &#039;x&#039;; 

	if ($post-&gt;post_type == &quot;org&quot;) {
		$my_fields = midea_org_fields(); // fields for this content type
		$post_flag = &#039;org&#039;;
	} elseif ($post-&gt;post_type == &quot;proj&quot;) {
		$my_fields = midea_proj_fields(); // fields for this content type
		$post_flag = &#039;proj&#039;;
	}  elseif ($post-&gt;post_type == &quot;event&quot;) {
		$my_fields = midea_event_fields(); // fields for this content type
		$post_flag = &#039;event&#039;;
	}

	if ($post_flag != &#039;x&#039;) {
		// we have MIDEA content to deal with

		// Loop through the MIDEA field data
		foreach ($my_fields as $k=&gt;$v)

		{
			$key = &#039;midea-&#039; . $post_flag . &#039;-&#039; . $k;

			$value = @$_POST[$key];
			if (empty($value))
			{
				delete_post_meta($post_id, $key);
				continue;
			}

			// special check for event time fields, convert as needed to unix time
			if ($key == &#039;midea-event-start&#039; OR $key == &#039;midea-event-end&#039; ) {
				$value = strtotime($value);
			}

			// If value is a string it should be unique
			if (!is_array($value))
			{
				// Update meta
				if (!update_post_meta($post_id, $key, $value))
				{
					// Or add the meta data
					add_post_meta($post_id, $key, $value);
				}
			}
			else
			{
				// If passed along is an array, we should remove all previous data
				delete_post_meta($post_id, $key);

				// Loop through the array adding new values to the post meta as different entries with the same name
				foreach ($value as $entry)
					add_post_meta($post_id, $key, $entry);
			}
		}

	}
}
</pre>
<p>This is a bit more complex, and again, I just modified it from examples I found form early HazMat suit wearers. It uses that same &#8220;nonce&#8217; thing (make sure your codes match). I am not 100% sure I needed that hack of using <strong>$post_flag</strong> I thought at one point I needed to make sure I did not run this function for saving any post type.</p>
<p>Basically, I am setting up my post meta data array, and then looking through the submitting form POST values to see if we have a value there. If we have this already in the database, we need to remove the old data first (<strong>delete_post_meta</strong>), and then add the new data (<strong>add_post_meta</strong>).</p>
<p>So now at this point, we can add this metadata, and if we have done it right, when we go to edit, the current values, if they exist, are pre-populated in the fields.</p>
<p>Whew. Anyone still breathing?</p>
<p>The last bit is some useful things you can do to customize the layout when you go to the Edit content types area- you can generate your own list of columns relevant to your content types. For example, when I go to edit my Organization content types, I get this interface:</p>
<p><img src="http://cogdogblog.com/wp-content/uploads/2010/06/wp3-org-edit-list.jpg" alt="" title="wp3-org-edit list" width="500" height="237" class="alignnone size-full wp-image-5127" /></p>
<p>So rather than a blog or page post title, my columns list the organization name, plus (from my own post meta data) the organization&#8217;s URL and their address. I can make any columns I want, and can even manipulate the output anyway relevent (like I made the URL a hyperlink).</p>
<p>To do this, we need to modify our content type creator function <strong>midea_custom_init()</strong> to include 2 new actions added for each content type, in the case of organizations:</p>
<pre class="brush: php">
	// set up dashboard editing view
	add_action(&quot;manage_posts_custom_column&quot;, &quot;org_custom_columns&quot;);
	add_filter(&quot;manage_edit-org_columns&quot;, &quot;org_columns&quot;);
</pre>
<p>This creates a callback function <strong>org_custom_columns</strong> used to generate the view above, and uses the <strong>org_columns</strong> function to define the columns. These functions look like:</p>
<pre class="brush: php">
function org_columns($columns)
{
	$columns = array(
		&quot;cb&quot; =&gt; &quot;&lt;input type=\&quot;checkbox\&quot; /&gt;&quot;,
		&quot;title&quot; =&gt; &quot;Organization&quot;,
		&quot;web&quot; =&gt; &quot;Web Site&quot;,
		&quot;address&quot; =&gt; &quot;Address&quot;,
		&quot;status&quot; =&gt; &quot;Status&quot;,
	);
	return $columns;
}

function org_custom_columns($column)
{
	global $post;

	if (&quot;ID&quot; == $column) echo $post-&gt;ID;
	elseif (&quot;title&quot; == $column) echo $post-&gt;post_title;
	elseif (&quot;web&quot; == $column) {
		$ourl = get_post_meta($post-&gt;ID, &#039;midea-org-web&#039;, true);
		echo &#039;&lt;a href=&quot;&#039; . $ourl . &#039;&quot; target=&quot;_blank&quot;&gt;&#039; . $ourl . &#039;&lt;/a&gt;&#039;;
	}
	elseif (&quot;address&quot; == $column) echo get_post_meta($post-&gt;ID, &#039;midea-org-address&#039;, true);
	elseif (&quot;status&quot; == $column) echo ucfirst($post-&gt;post_status) . &#039;ed&lt;br /&gt;&#039; . substr($post-&gt;post_date,0,10) ;

}
</pre>
<p>So <strong>org_columns</strong> merely defineds an array of the columns (&#8220;cb&#8221; is the built in check boxed for multiple editing, and the <strong>org_custom_columns</strong> function defines the output for each type; you can see for the URL where I do the extra work to extract my meta data values and build a hyperlink.</p>
<p>My sample code below includes the other functions used for my own other two content types. They have a bit more gymnastics, but follow the same concept as above.</p>
<p>So here is e <a href='http://cogdogblog.com/wp-content/uploads/2010/06/functions-midea-step2.php_.txt'>the functions.php cod</a> I have made up to this point. We have set up everything we need to do to create, edit custom content types, the next step is to do some WordPress theme hacking to set up the output. Easy peasy, eh? </p>
<p>For the other parts of this convoluted series&#8230;</p>
<ul>
<li><a href="http://cogdogblog.com/2010/05/28/wordpress-3-pt1/">Overview WordPress 3.0 Content Types</a>- my plans to dive in</li>
<li><a href="http://cogdogblog.com/2010/05/28/setting-up-custom-contenttypes/">Creating the content types.</a> The easiest part</li>
<li><a href="http://cogdogblog.com/2010/05/29/those-darn-kids/">Leveraging child themes</a>- my newly discovered best practice for chopping apart themes.</li>
<li><strong>Adding the meta data field editing boxes</strong> (add_post_meta). Hacking the Editing fields.</li>
<li>Modifying templates to display the content, Hacking the templates.</li>
</ul>
<p>You can find all the posts in this series at <a href="http://cogdogblog.com/2010/07/23/roundup-wp3/">http://cogdogblog.com/2010/07/23/roundup-wp3/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://cogdogblog.com/2010/06/06/put-on-your-hazmat-suits/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by/3.0/us/</creativeCommons:license>
	</item>
	</channel>
</rss>

