<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ClarkLab</title>
	<atom:link href="http://clarklab.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://clarklab.com</link>
	<description>I try to make things that are awesome</description>
	<lastBuildDate>Sat, 19 May 2012 04:47:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4-alpha-19719</generator>
		<item>
		<title>Convert links, username mentions, and hashtags in a tweet with PHP in WordPress</title>
		<link>http://clarklab.com/posts/convert-links-username-mentions-and-hashtags-in-a-tweet-with-php-in-wordpress/</link>
		<comments>http://clarklab.com/posts/convert-links-username-mentions-and-hashtags-in-a-tweet-with-php-in-wordpress/#comments</comments>
		<pubDate>Fri, 09 Mar 2012 03:28:15 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[posts]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=600</guid>
		<description><![CDATA[While working a recent project, we had a WP cron running that scraped Twitter and saved all the returned tweets as a custom post type. When displaying the tweet content, I realllly wanted to make the @username mentions, hashtags, and URLs auto-convert into proper links. Even if you aren&#8217;t make a silly webapp, there are [...]]]></description>
			<content:encoded><![CDATA[<p>While working a <a href="http://sxswrpg.com">recent project</a>, we had a WP cron running that scraped Twitter and saved all the returned tweets as a custom post type. When displaying the tweet content, I realllly wanted to make the @username mentions, hashtags, and URLs auto-convert into proper links. Even if you aren&#8217;t make a silly webapp, there are still <em>plenty</em> of times when you might be displaying Twitter content.</p>
<p>In my hunt, I noticed WordPress has a built-in core function called <a href="http://codex.wordpress.org/Function_Reference/make_clickable">make_clickable()</a>. Like the name implies, it would scan the provided value and make all the URLs into clickable links. It worked just fine, but I also wanted user mentions and hashtags, so I went a step further and made my own function, aptly named make_twitter(). Check it out below:</p>
<pre name="code" class="php">&lt;?php 
//place the following in functions.php or the like

function make_twitter($tweetcontent) {
  $tweetcontent = preg_replace("/[@]+([A-Za-z0-9-_]+)/", "<a href=\"http://twitter.com/\\1\" target=\"_blank\">\\0</a>", $tweetcontent );
  $tweetcontent = preg_replace("/[#]+([A-Za-z0-9-_]+)/", "<a href=\"http://twitter.com/search?q=%23\\1\" target=\"_blank\">\\0</a>", $tweetcontent );
  $tweetcontent = preg_replace("/((http)+(s)?:\/\/[^<>\s]+)/i", "<a href=\"\\0\" target=\"_blank\">\\0</a>", $tweetcontent );
  return $tweetcontent;
}

//then use the function in your templates like so:

echo make_twitter(get_the_content()); ?&gt;</pre>
<p>What it does is actually pretty simple. First, place the make_twitter() function into your functions.php file (or wherever you're stashing your functions). Next, use make_twitter() in your templates. All you've gotta do is provide the tweet content, in this case my post content, so I use <a href="http://codex.wordpress.org/Function_Reference/get_the_content">get_the_content()</a>.</p>
<p>Once my function receives the tweet, it simply runs a couple of regular expressions to sniff out the usernames, hashtags, and URLs, replacing them with proper links. On <a href="http://sxswrpg.com">my project</a>, I even changed the user link to the local site profile (since our accounts mirrored the Twitter account namespace).</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/posts/convert-links-username-mentions-and-hashtags-in-a-tweet-with-php-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Updated Notes from Cultivating Community</title>
		<link>http://clarklab.com/posts/notes-from-wordup-session-cultivating-community/</link>
		<comments>http://clarklab.com/posts/notes-from-wordup-session-cultivating-community/#comments</comments>
		<pubDate>Tue, 28 Feb 2012 07:20:52 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[posts]]></category>
		<category><![CDATA[austin]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[WordUp]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=565</guid>
		<description><![CDATA[The following are some key links from my talk on Cultivating Community at WordUp Austin on January 21, 2012 (and now again at the WordPress Austin meetup on Feb 28, 2012). You can find the slides over on Google Docs, but the real magic came in person&#8230; Follw on Twitter @wpatx &#8211; I&#8217;m running the [...]]]></description>
			<content:encoded><![CDATA[<p>The following are some key links from my talk on Cultivating Community at WordUp Austin on January 21, 2012 (and now again at the WordPress Austin meetup on Feb 28, 2012). You can find the <a href="https://docs.google.com/present/edit?id=0AaJR_R1Sv5FhZGNwY3NrZ3hfNDNjMzZucmpmbQ">slides over on Google Docs</a>, but the real magic came in person&#8230;</p>
<h3>Follw on Twitter</h3>
<p><a href="http://twitter.com/wpatx">@wpatx</a> &#8211; I&#8217;m running the official WordPress Austin Twitter account now, you should follow it!</p>
<h3>Comments</h3>
<p><a href="http://wordpress.org/extend/plugins/jetpack/">Jetpack</a> &#8211; Has a new module that allows users to subscribe to comments.<br />
<a href="http://disqus.com/welcome/">Disqus</a> &#8211; A managed plugin that adds new community features to comments.<br />
<a href="http://www.intensedebate.com/">Intense Debate</a> &#8211; A plugin managed by Automattic that adds social features to comments.<br />
<a href="http://mailchimp.com/social-plugin-for-wordpress/">Social Plugin for WordPress</a> &#8211; A light(er)weight plugin by Crowd Favorite for allowing comments via Twitter and Facebook.<br />
<a href="http://www.livefyre.com/">Livefyre</a> &#8211; The new kid on the block, with added features like live comments.<br />
<a href="http://akismet.com/">Akismet</a> &#8211; Quality spam protection for your growing comment section.</p>
<h3>Ask questions</h3>
<p><a href="http://androidandme.com/tag/topic-tuesday/">Topic Tueday</a> &#8211; A new question once a week that is locked to comments from users only. A point multiplier is also in effect.</p>
<h3>Poll Your Audience</h3>
<p><a href="http://polldaddy.com/">PollDaddy</a> &#8211; A powerful and easy to use poll plugin maintained by Automattic.<br />
<a href="http://wordpress.org/extend/plugins/wp-polls/">WP-Polls</a> &#8211; A lightweight poll plugin written by Lester &#8216;Gamerz&#8217; Chan.</p>
<h3>Newsletters</h3>
<p><a href="http://mailchimp.com">Mailchimp</a> &#8211; A paid (and thus pretty awesome) email newsletter solution.<br />
<a href="http://wordpress.org/extend/plugins/mailchimp/">Mailchimp Subscribe Plugin</a> &#8211; A easy widget for newsletter subscriptions.<br />
<a href="http://wordpress.org/extend/plugins/jetpack/">Jetpack</a> &#8211; Has a new module that allows users to subscribe to your blog.<br />
<a href="http://feedburner.com">Feedburner</a> &#8211; An old-school RSS tool that offers an email newsletter digest.</p>
<h3>Get Social</h3>
<p><a href="http://shakenandstirredweb.com/plugins/social-bartender/">Social Bartender</a> &#8211; Easy, WP-menu-like drag and drop setup for all your social network links.<br />
<a href="http://wordpress.org/extend/plugins/sociable/">Sociable</a> &#8211; Configurable sharing links and widgets for all your content.<br />
<strong>Explore</strong> &#8211; There are more social plugins than hairs on my head. Check them out and find out that works for you!<br />
<a href="http://wordpress.org/extend/plugins/pin-it-on-pinterest/">Pin it!</a> &#8211; A Pinterest plugin for adding a &#8216;Pin it&#8217; button to your content.<br />
<a href="http://wordpress.org/extend/plugins/easy-pinterest/">Easy Pinterest</a> &#8211; Display your latest pins and boards in your WordPress sidebar.</p>
<h3>Identity / Influence</h3>
<p><a href="http://androidandme.com/user/clark">And Me Account</a> &#8211; A sample account from my site, showing real name, user pic, user score, percentile rank, and links to various social accounts.<br />
<a href="http://gravatar.com">Gravatar</a> &#8211; A widely used avatar service maintained by Automattic.<br />
<a href="http://cubepoints.com">Cubepoints</a> &#8211; A lightweight point tracking system with modules that make for easy customization.<br />
<a href="http://www.bunchball.com/">Bunchball</a> &#8211; Full-bore gamification with badges and achievements and all that stuff.</p>
<h3>Contests</h3>
<p><a href="http://wordpress.org/extend/plugins/pick-giveaway-winner/">Pick Giveaway Winner</a> &#8211; A super simple plugin for running bespoke contests with user comments.<br />
<a href="http://androidandme.com/tegra">25 Days of Tegra</a> &#8211; My biggest promo to date, where we gave away a device per day only to users, generating tens of thousands of user interactions.<br />
<a href="http://androidandme.com/hackthenews/">#HackTheNews</a> &#8211; A sample contest I ran on Android and Me where users could tweet stories with tweaked headlines, generating over 1k tweets in barely a week.<br />
<a href="http://androidandme.com/tag/midweek-madness/">Midweek Madness</a> &#8211; A weekly contest with usually small prizes.</p>
<h3>Do Things with your Users</h3>
<p><a href="http://codex.wordpress.org/Function_Reference/get_users">get_users</a> &#8211; A new (since 3.1) function for working with users. Makes things super simple (compared to the old ways, at least).<br />
<a href="http://codex.wordpress.org/Function_Reference/update_user_meta">update_user_meta</a> &#8211; A super-handy function for adding and updating extra information to any user account. Use with <a href="http://codex.wordpress.org/Function_Reference/get_user_meta">get_user_meta</a>.<br />
<a href="http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters">orderby=comment_count</a> &#8211; A parameter you can use when querying posts to return the most commented posts.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/posts/notes-from-wordup-session-cultivating-community/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Favorite community-focused WordPress plugins</title>
		<link>http://clarklab.com/posts/favorite-community-focused-wordpress-plugins/</link>
		<comments>http://clarklab.com/posts/favorite-community-focused-wordpress-plugins/#comments</comments>
		<pubDate>Mon, 27 Feb 2012 17:42:01 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[posts]]></category>
		<category><![CDATA[austin]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=596</guid>
		<description><![CDATA[I&#8217;m giving a talk tomorrow night called Cultivating Community where I&#8217;ll be sharing some insight into the process of running a blog with 10 authors and 45k registered users. I&#8217;ve given the talk once before and already my have own list of favorite plugins growing, but I was curious what you guys use. Are there [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m giving <a href="http://www.meetup.com/austinwordpress/events/51733752/">a talk</a> tomorrow night called Cultivating Community where I&#8217;ll be sharing some insight into the process of running <a href="http://androidandme.com">a blog</a> with 10 authors and 45k registered users. I&#8217;ve given the talk once before and already my have own <a href="http://clarklab.com/posts/notes-from-wordup-session-cultivating-community/" title="Notes from WordUp session: Cultivating Community">list</a> of favorite plugins growing, but I was curious what you guys use.</p>
<p>Are there any community plugins you can&#8217;t live without? Or something new you&#8217;ve seen floating around that you&#8217;d like me to investigate and talk about during my session tomorrow? It doesn&#8217;t even have to be a plugin really, feel free to leave me some broad questions about practice or strategy, I&#8217;m really just looking for some extra angles to cover to make sure everyone finds use in tomorrow&#8217;s talk.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/posts/favorite-community-focused-wordpress-plugins/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Editing remote text files on Android with DroidEdit Pro</title>
		<link>http://clarklab.com/posts/editing-remote-files-on-android-with-droidedit-pro/</link>
		<comments>http://clarklab.com/posts/editing-remote-files-on-android-with-droidedit-pro/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 01:25:55 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[posts]]></category>
		<category><![CDATA[android]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=591</guid>
		<description><![CDATA[Just a quick note as I sit at the WordPress Austin to share an Android app I found for editing text files. DroidEdit Pro (and the matching, ad-supported Free version) allow you to edit text files with full syntax highlighting and a tabbed pane interface (on my TF Prime, at least). What you&#8217;re looking at [...]]]></description>
			<content:encoded><![CDATA[<p>Just a quick note as I sit at the WordPress Austin to share an Android app I found for editing text files. DroidEdit Pro (and the matching, ad-supported Free version) allow you to edit text files with full syntax highlighting and a tabbed pane interface (on my TF Prime, at least).<br />
<span id="more-591"></span><br />
<a href="http://clarklab.com/wp-content/uploads/2012/02/Screenshot_2012-02-07-18-53-58.jpg"><img src="http://clarklab.com/wp-content/uploads/2012/02/Screenshot_2012-02-07-18-53-58-1024x640.jpg" alt="" title="DroidEdit Pro on the Transformer Prime" width="600" height="auto" class="alignnone size-large wp-image-592" /></a></p>
<p>What you&#8217;re looking at is a screen capture from my Android Tablet, the Transformer Prime. I&#8217;ve got the matching keyboard dock, so this thing is damn near feeling like a &#8216;real&#8217; computer. </p>
<p>In the right pane, you&#8217;ll see a list of files I&#8217;ve got open, in this case some WordPress templates direct from my <a href="http://androidandme.com/wpe">SFTP server</a>. Using SFTP is a pro feature, as well as editing direct to Dropbox, both of which are well worth a dollar.</p>
<p><a href="https://market.android.com/search?q=droidedit+pro">Go download it in the Android Market</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/posts/editing-remote-files-on-android-with-droidedit-pro/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Change the default avatar in WordPress</title>
		<link>http://clarklab.com/quick-tip/change-the-default-avatar-in-wordpress/</link>
		<comments>http://clarklab.com/quick-tip/change-the-default-avatar-in-wordpress/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 07:45:48 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[Quick Tips]]></category>
		<category><![CDATA[avatar]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=586</guid>
		<description><![CDATA[If you&#8217;ve ever wanted to change the default avatar in WordPress, all you need is a quick filter in functions.php that inserts and names your new avatar. Once you add the markup below, you&#8217;ll see your new avatar type listed in wp-admin &#62; Settings &#62; Discussion along with the choices like Identicons and MonsterID. The [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever wanted to change the default avatar in WordPress, all you need is a quick filter in functions.php that inserts and names your new avatar. Once you add the markup below, you&#8217;ll see your new avatar type listed in <em>wp-admin &gt; Settings &gt; Discussion</em> along with the choices like Identicons and MonsterID. The code:</p>
<pre name="code" class="php">&lt;?php 
add_filter( 'avatar_defaults', 'newavatar' );  
function newavatar ($avatar_defaults) {  
$myavatar = get_bloginfo('template_directory') . '/images/avatar.png';  
$avatar_defaults[$myavatar] = "Pigs";  
return $avatar_defaults; } ?&gt;</pre>
<p>This is a pretty simple one. Our function newavatar() is hooked to the avatar_defaults, and we simply add our new avatar choice, which includes a path to the image URL along with a title (pigs!). At the end, we <em>return</em> the defaults, which has been impregnated with our new custom avatar, and no one is ever the wiser!</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/quick-tip/change-the-default-avatar-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress comment hacks: User-only comments, restrict by user age and more</title>
		<link>http://clarklab.com/posts/wordpress-comment-hacks-user-only-comments-restrict-by-user-age-and-more/</link>
		<comments>http://clarklab.com/posts/wordpress-comment-hacks-user-only-comments-restrict-by-user-age-and-more/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 03:43:56 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[posts]]></category>
		<category><![CDATA[comments]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[user meta]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=550</guid>
		<description><![CDATA[In a recent promo for Android and Me, we created a bunch of giveaways, each with a specific limitation on entry. We had 25 days worth of prizes, and each day held a different challenge. We used comments as the main way to enter, and on certain days we wanted to restrict entry to certain [...]]]></description>
			<content:encoded><![CDATA[<p>In a recent <a href="http://androidandme.com/tegra">promo for Android and Me</a>, we created a bunch of giveaways, each with a specific limitation on entry. We had 25 days worth of prizes, and each day held a different challenge. We used comments as the main way to enter, and on certain days we wanted to restrict entry to certain groups of users.</p>
<p>Some days we only wanted users who had added their Twitter account to the profile (a piece of user meta), some days we wanted only members of a certain age to be eligible (a piece of user meta compared to a set value), and overall, we wanted all comments to be from registered users only (private comments on certain WordPress posts).</p>
<p>To accomplish all these things, I came up with the simple hack of creating a slew of conditional checks that surround <a href="http://codex.wordpress.org/Function_Reference/comment_form">comment_form()</a>. If you&#8217;re not familiar, it&#8217;s a function that outputs the comment form itself and is usually found near the bottom of comments.php. It&#8217;s a pretty easy way to control who is commenting, taking away the form. Take a peek a the heavily commented code below (no pun intended) and join me after for some play by play.</p>
<pre name="code" class="php">&lt;?php 
//start by checking for a couple of key pieces of info
$current_user = wp_get_current_user();
$private_comments = get_post_meta($post->ID, 'private_comments', true);
$meta_lock = get_post_meta($post->ID, 'meta_lock', true);
$time_lock = get_post_meta($post->ID, 'time_lock', true);

//if the post has the custom field 'meta_lock'
if ($meta_lock) {
  //check the current user for that meta field
  $user_meta_key =  get_user_meta($current_user->ID, $meta_lock, true);
    if ($current_user->ID AND $user_meta_key) {
      //display the comment form
      comment_form();
      //give an error message (add some flare!)
    } else { echo 'Special users only!'; }

//if the post has the custom field 'time_lock'	
} elseif ($time_lock) {
  //get the age of the user and prepare the age limits
  $user_age=date("Y-m-d", strtotime($current_user->user_registered));
  $age_limit = strtotime($time_lock);
  $account_age = strtotime($user_age);
  
  //if the current user is OLDER than the set limit
  if ($current_user->ID AND $account_age <= $age_limit) {
    //display the comment form
    comment_form();
  //give an error message (add some flare!)
  } else { echo 'Users older than '.$tegra_time.' only!'; }

//if the post has the field 'private_comments'	
} elseif ($private_comments) {
  //check if the user has an ID (aka is signed in)
  if ( $current_user->ID ) {
    //display the comment form
    comment_form();
  //give an error message (add some flare!)
  } else { echo 'Users only!'; }

//no special conditions met, just display the comment form  
} else { comment_form(); } ?&gt;</pre>
<p>Jeeeez that's a scary wall of text! Ok, not really, but compared to just the single line comment_form() we started with it looks pretty major. What we're doing is actually pretty simple. Anytime the template would normally call comment_form, we're getting in the way and checking some settings first.</p>
<p>What settings? Things we set on a post per post (or page) basis, using custom fields. If you'd like to turn on user-only comments, set a custom field 'private_comments' with any value (I usually just go with 'true'). If you'd like to restrict comments based on whether or not a user has a certain meta field, on your post set a custom field with the key 'meta_lock' and a value matching whatever <em>user</em> meta you'd like to filter by. This can be a user's real name, website link, bio, etc. It's a great way to push your users to fill out certain fields of their profile.</p>
<p>One of the funnest challenges we did was based on user age, something you'll see in the snippet above as age_lock. To use it, simply set a custom field on your post with a YYYY-MM-DD format (one year before today's date, for example). When comments.php is rendered, it'll compare the date the user registered with the date you've entered in the field 'age_lock'.</p>
<p>These few examples are really just to get the juices flowing, and to introduce you to the simple hack of conditionally displaying the comment form. There are an infinite number of things you could check or compare, so if you explore and find something killer make sure you come back and share it with us.</p>
<p>So far, I think my favorite use of this technique was tracking user tweets with the service <a href="http://s.rowfeeder.com/eb5cec4ea7h6S">Rowfeeder</a>, which I then used to export a full list of Twitter usernames. Since our accounts let users link their Twitter handle, I was able to check which WordPress accounts had successfully Tweeted and then conditionally allow them to comment. Total score!</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/posts/wordpress-comment-hacks-user-only-comments-restrict-by-user-age-and-more/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Sort WordPress users by custom meta value with get_users</title>
		<link>http://clarklab.com/posts/sort-wordpress-users-by-custom-meta-value-with-get_users/</link>
		<comments>http://clarklab.com/posts/sort-wordpress-users-by-custom-meta-value-with-get_users/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 00:45:42 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[posts]]></category>
		<category><![CDATA[get_users]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[wp_user_query]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=578</guid>
		<description><![CDATA[I&#8217;ll be honest. This one took me a while. I spent a lot of time looking for a solution but there doesn&#8217;t seem to be a whole bunch of discussion around user queries in plain ol&#8217; WordPress. I guess most of the big user action has moved on to bbPress and BuddyPress. For those of [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ll be honest. This one took me a while. I spent a lot of time looking for a solution but there doesn&#8217;t seem to be a whole bunch of discussion around user queries in plain ol&#8217; WordPress. I guess most of the big user action has moved on to bbPress and BuddyPress.</p>
<p>For those of us with a sizable user base contained <em>only</em> inside WordPress however, actually <em>doing</em> things with users takes a bit of elbow grease. I&#8217;m sure all the same possibilities exist, but people don&#8217;t seem to talk or write about them as often, which is where this guide comes in.</p>
<p>Today we&#8217;re going to look at getting WordPress to sort users by a custom meta value, something that would be useful if your users have any sort of scores, totals, or custom profile fields.</p>
<p>Over on <a href="http://androidandme.com">Android and Me</a>, all users have a point total. We then rank all users by that point total and assign everyone a percentile rank. To call up thousands of users based on a custom meta value seemed daunting, and it was, until I got a bit more familiar with <a href="http://codex.wordpress.org/Class_Reference/WP_User_Query">WP_User_Query</a>.</p>
<p><a href="http://codex.wordpress.org/Class_Reference/WP_User_Query">WP_User_Query</a> was introduced in WordPress 3.1 and brings us an easy and flexible way to query our user database. We&#8217;ll be using the function <a href="http://codex.wordpress.org/Function_Reference/get_users">get_users</a>, which is <a href="http://forrst.com/posts/WordPress_WP_User_Query_vs_get_users-6jY">basically a wrapper</a> for WP_User_Query. You&#8217;ll notice <a href="http://codex.wordpress.org/Function_Reference/get_users">the function</a> includes an &#8216;orderby&#8217; parameter, but the only options given are:</p>
<blockquote><p>orderby &#8211; Sort by &#8216;nicename&#8217;, &#8216;email&#8217;, &#8216;url&#8217;, &#8216;registered&#8217;, &#8216;display_name&#8217;, or &#8216;post_count&#8217;.</p></blockquote>
<p>Notice we&#8217;re missing &#8216;meta_key&#8217; or &#8216;meta_value&#8217;, so we&#8217;re on our own when it comes to ordering these guys by a custom field. Also of note is &#8216;post_count&#8217;, which I could totally see coming in handy on certain community blogs (MAKE A NOTE!). Now that we know what we&#8217;re after and what tools we&#8217;ll need to make it happen, it&#8217;s time we get started.</p>
<h2 id="codestuff">The Code</h2>
<pre name="code" class="php">&lt;?php 
//these are the arguments for the get_users function below
$args  = array(
  'fields' => 'all_with_meta',
  'meta_query' => array(
    array(
    'key' => 'points', // the meta field (or key) we want to target
    'value' => '2',    // the value we want to target (optional)
    'compare' => '>='  // comparison method (optional:  =, >, <, etc)
    )
));

//get_users calls WP_User_Query and returns an array of matching users
$users = get_users($args);

//custom function for comparing the data we want to sort by
function cmp($a, $b){
  if ($a->points == $b->points) {
    return 0;
  }
  return ($a->points > $b->points) ? -1 : 1;
}

//usort sorts our $users array with our function cmp()
usort($users, 'cmp');

//leaving an array of $users sorted by the value of meta 'points'
foreach ($users as $user) {  
  // do something rad!
} ?&gt;</pre>
<p>I've commented the code above so hopefully you were able to follow along in the actual markup, but if you'd like a short summary: here we go!</p>
<p>I start off with a short list of arguments, or options, that get_users() needs to return the users I want. A full list of parameters can be found on the <a href="http://codex.wordpress.org/Function_Reference/get_users">get_users page</a> in the Codex. The key here is <em>meta_query</em>, which lets me specify the user meta field I'd like to target and compare. I so happen to be calling up users based on their point totals, so I call any user with points above or equal to 2 (any below that I can assume is a scrub and just rank at the bottom of the pile to save some query juice).</p>
<p>Once I've got my $args, I go ahead and run get_users($args). Doing so will return a full list of users that match the arguments, all handily stored in the array $users.</p>
<p>But what's that? They aren't <em>sorted</em>? While we were allowed to compare points while calling the users, no actual sorting took place. This is where we get a bit tricky. Instead of relying on WordPress to sort the users for us, something the Codex says isn't possible right now, we're going to bust out PHP's <a href="http://php.net/manual/en/function.usort.php">usort</a> along with a simple custom function for comparing the points.</p>
<p>Our custom function, cmp(), is just a little check that runs that compares the point total of a user with the other point totals in the array. If the totals are the same, the function returns '0'. If they are different, the function returns a '1' or '-1'. That simple math is enough to let usort crunch through thousands of users in seconds, something I'd spent a couple weeks trying to figure out how to make WordPress do for me. Oh well, <em>you know how that goes.</em></p>
<p>Now that we've got $users in order by the meta field 'points', all that's left is to do something rad. Simply use a <a href="http://php.net/manual/en/control-structures.foreach.php">foreach</a> (PHP's version of The Loop) to run through the users in the array and accomplish something. In my case, it was doing even a bit more math to calculate each user's percentile in order to show what everyone's point totals really meant in relation to one another.</p>
<p>I know not everyone's blog has a point system, but if you've got any amount of users or even multiple authors this might come in handy. I used WP_User_Query all over the place on the <a href="http://wordupaustin.com">WordUp Austin</a> site. On <a href="http://androidandme.com">Android and Me</a>, I list the author pictures in the sidebar. Now, if I wanted to, I could order them by post count or point totals.</p>
<p>Slowly but surely I'm accumulating bits of community-focused solutions like this one and soon I'll be able to say with complete certainty that my WordPress install would slay any bbPress or BuddyPress install you can toss at it.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/posts/sort-wordpress-users-by-custom-meta-value-with-get_users/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Intelligent caching of changing data with the WordPress Transients API</title>
		<link>http://clarklab.com/posts/intelligent-caching-of-changing-data-with-the-wordpress-transient-api/</link>
		<comments>http://clarklab.com/posts/intelligent-caching-of-changing-data-with-the-wordpress-transient-api/#comments</comments>
		<pubDate>Sat, 28 Jan 2012 04:37:40 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[posts]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=570</guid>
		<description><![CDATA[Recently I built a shortcode for displaying data from the Android Market. You can see it in action on Android and Me. To make the magic happen, I query the AppAware API for a couple key bits of data, mainly the app icon, title, developer name, etc. After a while, I realized that even without [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I built a shortcode for displaying data from the Android Market. You can see it in action on <a href="http://androidandme.com/2011/11/applications/launching-an-app-is-totally-as-fun-as-it-looks-from-beta-to-20k-downloads-in-two-weeks/">Android and Me</a>. To make the magic happen, I query the <a href="http://appaware.org">AppAware</a> API for a couple key bits of data, mainly the app icon, title, developer name, etc.</p>
<p>After a while, I realized that even without a rate limit being imposed by AppAware, some of my calls were failing. To lighten the load, I turned to the <a href="http://codex.wordpress.org/Transients_API">WordPress Transients API</a>, a super simple way of saving (or caching) data for later use. Because saving a transient stores the data locally, you won&#8217;t need to make as many external calls, which is good for you and for your API partners.</p>
<h2>What is the Transients API?</h2>
<p>If you&#8217;re an experienced developer and you&#8217;re familiar with the concept of caching, you can probably skip this section. The code samples are below. But if you don&#8217;t know a thing about saving data locally, WordPress says: <em>the Transients API offers a simple and standardized way of storing cached data in the database temporarily by giving it a custom name and a timeframe after which it will expire and be deleted. Also of note is that Transients are inherently sped up by caching plugins, where normal options are not.</p>
<p>For this reason, transients should be used to store any data that is expected to expire, or which can expire at any time. Transients should also never be assumed to be in the database, since they may not be stored there at all.</em></p>
<p>This makes transients the perfect solution to storing data you would otherwise call from a 3rd party. Ideally, when you need the data, you&#8217;ll first check to see if the transient exists. If it does, you use that data. If it doesn&#8217;t, you call for a fresh version of that data, when you then not only use, but save and timestamp for later use. Sound simple enough?<span id="more-570"></span></p>
<h2>Setting and getting transients</h2>
<p>The main functions we&#8217;ll be using are, predictably, <a href="http://codex.wordpress.org/Function_Reference/set_transient">set_transient</a> and <a href="http://codex.wordpress.org/Function_Reference/get_transient">get_transient</a>, both of which couldn&#8217;t be simpler. To get started, let&#8217;s look at the absolute first step, saving a transient. If you&#8217;ve ever saved meta data using <a href="http://codex.wordpress.org/Function_Reference/update_post_meta">update_post_meta</a> or <a href="http://codex.wordpress.org/Function_Reference/update_user_meta">update_user_meta</a>, this should look totally familiar to you.</p>
<pre name="code" class="php">&lt;?php set_transient( $transient, $value, $expiration ); ?&gt;</pre>
<p>The function accepts three arguments, all of which are pretty straight forward. First, <strong>$transient</strong> is the name of the actual piece of data to be stored. This is the unique ID by which you'll call the data later, so make it good. The <strong>$value</strong> is the actual data being stored, aka the payload. You'll use $expiration to set the time limit on this transient, in seconds. When the time expires, <strong>so does the data</strong>.</p>
<p>Once you've got some data saved, you'll likely want to call it in your templates. Getting a transient is even easier than setting it:</p>
<pre name="code" class="php">&lt;?php get_transient( $transient ); ?&gt;</pre>
<p>Here, again, <strong>$transient</strong> is the unique identifier of the data your looking for. There isn't much to explain here. When you call the transient, if the time limit hasn't expired, you'll have the data to do as you please. If the data has expired and you still need it, things get a tiny bit more complex, which brings us to my next point...</p>
<h2>Putting it all together</h2>
<p>Our sample case will be displaying our <a href="http://klout.com">Klout</a> score, via the <a href="http://developer.klout.com/api_gallery">Klout API</a>. Like I said, this Transients API should be used for time-sensitive data, data that changes or that needs to be refreshed at a reliable interval. And while we're discussing use, it doesn't have to be external data, you could use it to store just about anything. We're just going the API route during the example to score some bonus points.</p>
<p>I've heavily commented the code below, but the basic path goes like this. First, we'll attempt to call the transient. If the data exists, our job is done and we use it. If the data doesn't exist or has expired, we'll call for a fresh copy from the Klout API, which we'll then save and immediately use.</p>
<p>The entire process is basically an <a href="http://php.net/manual/en/control-structures.if.php">if</a> statement that uses both <a href="http://codex.wordpress.org/Function_Reference/set_transient">set_transient</a> and <a href="http://codex.wordpress.org/Function_Reference/get_transient">get_transient</a>.</p>
<pre name="code" class="php">&lt;?php //let's get my klout score!!

//first, let's see if I've got the data already cached locally
$klout = get_transient("klout"); 
  
  //dang it! it looks like I might not. let's check with the Klout API
  if( !$klout ) {  
    $json = file_get_contents("http://api.klout.com/1/klout.json?key=[API key]&users=clarklab");
    $return = json_decode($json, true);
    $kloutscore = $return["users"][0]["kscore"]; 

    //if the Klout API gave us a score, let's save it!
    if( $kloutscore ) {
      
      //in a field named "klout", we'll save our $kloutscore, for 12 hours (60 seconds x 60 minutes x 12 hours)
      set_transient("klout", $kloutscore, 60 * 60 * 12);

      //let's also make sure to set the data you were originally looking for
      $klout = $kloutscore;  
    }  
  }
	
echo 'Now I can safely check my '.$klout.' score with transients!'; ?&gt;</pre>
<p>The main check is <strong>if( !$klout )</strong>, which, of course, checks to see if the value <strong>$klout</strong> is set, which we attempted to do with <a href="http://codex.wordpress.org/Function_Reference/get_transient">get_transient</a>. If it is not, we know we need to fetch a fresh copy of the data and save it (<a href="http://codex.wordpress.org/Function_Reference/set_transient">set_transient</a>). Since we're using a private API, you'll need to replace [API key] with your personal API key (duh).</p>
<p>If you wanted to get fancy, you could always work in the <a href="http://codex.wordpress.org/Function_Reference/delete_transient">delete_transient</a> function, but since these things expire with time I'm content to be the lazy type and just let decay do its work.</p>
<h2>Go forth and cache</h2>
<p>Equipped with your new army of drifter data you're ready to speed up page loads by reducing reliance on often slow external API calls. And if you're dealing with a rate-limited API, caching is a necessity, so you might as well do it with a supported <a href="http://codex.wordpress.org/Transients_API">WordPress API</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/posts/intelligent-caching-of-changing-data-with-the-wordpress-transient-api/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using PHP to find a discount on a price in WordPress</title>
		<link>http://clarklab.com/posts/using-php-to-find-a-discount-on-a-price-in-wordpress/</link>
		<comments>http://clarklab.com/posts/using-php-to-find-a-discount-on-a-price-in-wordpress/#comments</comments>
		<pubDate>Fri, 27 Jan 2012 02:26:40 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[posts]]></category>
		<category><![CDATA[deals]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=568</guid>
		<description><![CDATA[In today&#8217;s version of programming 101, we&#8217;re going to learn how to find the discount on a deal price. It&#8217;s pretty standard fare, but as someone who isn&#8217;t the sharpest mathematician, I figured this might be worth sharing, if for no other reason that I&#8217;ll probably need to look it up at some point in [...]]]></description>
			<content:encoded><![CDATA[<p>In today&#8217;s version of programming 101, we&#8217;re going to learn how to find the discount on a deal price. It&#8217;s pretty standard fare, but as someone who isn&#8217;t the sharpest mathematician, I figured this might be worth sharing, if for no other reason that I&#8217;ll probably need to look it up at some point in the future.</p>
<p>For bonus points, my example will be pulling the prices from a custom field on a WordPress post using get_post_meta. If you&#8217;re not running WordPress, you could just include the plain text prices in the script (where I set $deal_price and $old_price).</p>
<div id="gist-1686586" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;?php</span></div><div class='line' id='LC2'><span class="nv">$deal_price</span> <span class="o">=</span> <span class="nx">get_post_meta</span><span class="p">(</span><span class="nv">$post</span><span class="o">-&gt;</span><span class="na">ID</span><span class="p">,</span> <span class="s1">&#39;deal_price&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span></div><div class='line' id='LC3'><span class="nv">$old_price</span> <span class="o">=</span> <span class="nx">get_post_meta</span><span class="p">(</span><span class="nv">$post</span><span class="o">-&gt;</span><span class="na">ID</span><span class="p">,</span> <span class="s1">&#39;old_price&#39;</span><span class="p">,</span> <span class="k">true</span><span class="p">);</span></div><div class='line' id='LC4'><span class="nv">$discount</span> <span class="o">=</span> <span class="nx">round</span><span class="p">(((</span><span class="mi">1</span><span class="o">-</span><span class="p">(</span><span class="nv">$deal_price</span> <span class="o">/</span> <span class="nv">$old_price</span><span class="p">))</span><span class="o">*</span><span class="mi">100</span><span class="p">),</span><span class="mi">0</span><span class="p">);</span></div><div class='line' id='LC5'><span class="k">echo</span> <span class="nv">$discount</span><span class="o">.</span><span class="s1">&#39;%&#39;</span><span class="p">;</span> <span class="cp">?&gt;</span><span class="x"></span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1686586/c646511944a839ee5bce3d42cf7cf3870742c923/wordpress-find-discount.php" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1686586#file_wordpress_find_discount.php" style="float:right;margin-right:10px;color:#666">wordpress-find-discount.php</a>
            <a href="https://gist.github.com/1686586">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>If you recall math class, to find a percentage you first divide the new price by the old price. Then to find the <em>discount</em>, you subtract from one and multiply by 100, leaving you with the actual percentage discount on the original price (rounding to zero decimal places).</p>
<p>Again, this is a simple one, I just wanted to log it for posterity&#8217;s sake.</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/posts/using-php-to-find-a-discount-on-a-price-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What type of WordPress theme should I make?</title>
		<link>http://clarklab.com/posts/what-type-of-wordpress-theme-should-i-make/</link>
		<comments>http://clarklab.com/posts/what-type-of-wordpress-theme-should-i-make/#comments</comments>
		<pubDate>Thu, 26 Jan 2012 20:12:52 +0000</pubDate>
		<dc:creator>clark</dc:creator>
				<category><![CDATA[posts]]></category>
		<category><![CDATA[poll]]></category>
		<category><![CDATA[themes]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://clarklab.com/?p=567</guid>
		<description><![CDATA[In the next few weeks, I&#8217;m going to make a series of three WordPress themes for public consumption, and I need some quick advice on which type of theme you&#8217;d like to see me release. I&#8217;m fairly certain theme one will be a news theme, and theme two will be a portfolio theme, but theme [...]]]></description>
			<content:encoded><![CDATA[<p>In the next few weeks, I&#8217;m going to make a series of three WordPress themes for public consumption, and I need some quick advice on which type of theme you&#8217;d like to see me release. I&#8217;m fairly certain theme one will be a news theme, and theme two will be a portfolio theme, but theme three is still in the air.</p>
<p>The themes will be fully responsive for perfect performance on all devices and will have only a minimal set of theme options (which will be completely removable, if desired, like I would). A good bit of the design is going to be done by <a href="http://twitter.com/angiedoes">@angiedoes</a>, so I know things will come out looking sharp.<span id="more-567"></span></p>
Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.
<p>Sound off in the comments below if you&#8217;ve got some extra ideas. I&#8217;m starting the builds pretty soon and I&#8217;m still open to feature suggestions&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://clarklab.com/posts/what-type-of-wordpress-theme-should-i-make/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

