John Herren’s Blog

Reading Radar featured on Boing Boing

October 14, 2009 · 6 Comments

A guest blogger, Connie Choe, has posted a profile of Reading Radar on my all-time favorite blog Boing Boing. I can’t believe that this simple little mashup has received so much attention. Would anyone be interested in a video tutorial or something to learn how to build mashup sites like this?

→ 6 CommentsCategories: mashups

Liz’s Photography Site is Live

April 8, 2009 · 2 Comments

Liz has been asking me for some time now for a website to show off her photography. She’s been quite the shutterbug these last several years, consuming gigabytes upon gigabytes of disk space. I’ve had a lot of fun watching her try new things and watching her confidence build as what was one was a hobby has become much more. She has taken quite an interest in photographing local musicians, made some good contacts, and has begun selling prints. I feel really grateful to her that we have such and awesome time machine to look back on past memories in all of their splendor.

elhphotos.com - Elizabeth Herren Photography

elhphotos.com - Elizabeth Herren Photography

I finally sat down this evening and put together a simple site.  Her site, elhphotos.com pulls in an album from her “professional” Flickr account (as opposed to her personal one), so she’ll be able to manage the slideshow without needing to edit the site at all.

Technically you could call this site a mashup. It’s all client side, using some jquery plugins to access her Flickr feed, and the slimbox2 plugin to power the lightbox effect.

→ 2 CommentsCategories: mashups

Twitter and the Case for Web Hooks

March 5, 2009 · 16 Comments

My, my, we do love our Twitter. Tiny messages in a list. A global chatroom really, but just the messages you want to see. What an elegant system.

  • But it’s hard to track conversations.
  • But 140 characters isn’t enough for some dude who has trouble making his thoughts a wee bit more concise and is accustomed to using long, redundant, extra words.
  • But it doesn’t automatically link up my stock symbols in my tweets.

But, but, but.

Enter the API

Everyone who’s used Twitter for a significant amount of time has concocted a list of features that Twitter lacks and desperately needs. Fortunately, Twitter has a given us a nice API to let geeks fill in the gaps somewhat. Without Googling around to find the most recent stat, I’ve read that the majority of tweets are placed through the API instead of the Twitter site, through mobile and desktop apps. That’s a good indication.

Besides API based clients–alternate software that does what the Twitter site does, there has been a surge in Twitter powered bots. These bots read and write to Twitter using some kind of logic in between. I’ve seen autonomous chat bots and autoresponders.

Who Are My Twitter Followers?

A pattern I’ve seen a lot lately with Twitterized apps is “all you have to do to join my app is follow @sometwittername”. @sometwittername then uses the API to find his followers, and then sends the user tweets, or, links via direct message with special links just for that user.  It’s a MLM’ers dream. The top essential tactic of these web marketing guys is to BUILD YOUR LIST, and there’s no friendlier way than Twitter, right?

If you want to see an example of this pattern in action, here’s one, which I reluctantly link to for demonstration purposes only.

Yes, apparently we have “Twitter marketing gurus” now.

So now the developer needs to know who his followers are, so he can spam or provide $ValueAddedService. Currently, the Twitter API gives developers two ways of finding out who is following you. The first one requires polling, which means you have to call the API at regular intervals.

Polling sucks. If you request too often, it’s wasteful. If you don’t request often enough, you miss the opportunity to do cool stuff with new followers in a timely fashion.

A better way to detect new followers is to tap into your email stream. When Twitter emails you with a new follower notification, it tucks away some extra headers into your message. This is documented for some reason in the FAQ. The headers look something like this:

  • X-TwitterEmailType - will be ‘is_following’ or ‘direct_message’
  • X-TwitterCreatedAt - ex: Thu Aug 07 15:17:15 -0700 2008
  • X-TwitterSenderScreenName - ex: ‘bob’
  • X-TwitterSenderName - ex: ‘Bob Smith’
  • X-TwitterSenderID - ex: 12345
  • X-TwitterRecipientScreenName - ex: ‘john’
  • X-TwitterRecipientName - ex: ‘John Doe’
  • X-TwitterRecipientID - ex: 67890
  • X-TwitterDirectMessageID - ex: 2346346

So all you have to do to maintain an instantly up-to-date list of your followers is to send your email notifications through a script (most any SMTP server can do this) and check for messages with the X-TwitterEmailType set to ‘is_following’, and then grab the SenderID or SenderScreenName and you’re all set.

This is a wannabe web hook in action.

WTF is a Web Hook?

If this is the first time you’re hearing the term “web hook”, pay attention. It’s the next step in this whole read-write web thing.

In short, a web hook, or “http callback” allows you to subscribe to another website’s events with a URL. You tell a website, “Hey, when some event happens, send some information about it to this URL”. Why? It doesn’t matter, it’s up to the developer to decide what to do with that information.

An excellent example of a web hook is Paypal’s Instant Payment Notification. It works like this:

  1. You come to my store, fill up your cart, and hit the checkout button.
  2. My site sends you and your order info to Paypal, and you pay up.
  3. In my Paypal account, I’ve set up my IPN (web hook!) URL.
  4. Paypal POSTs the order information to the hook URL letting me know that you’ve paid
  5. I record the sale, do whatever business logic I need to do so my Oompa Loompas can ship your stuff to you.

There’s a little more to it, but that’s the basics. I can choose to do whatever I want with that data from Paypal.

The key part is step 5. I do whatever logic I need to do with my own code.

Web hooks are APIs knowing to call other API endpoints. That’s it.

Now back to the Twitter. Why should you have to hook into your email stream to get those “is_following” notifications?  In the same way Twitter sends you an email, it could just as easily send an HTTP POST with that same data to a script you control. Would it not be much simpler to specify a callback URL in your Twitter settings?

Your site would become integrated with Twitter itself. That word integrated is key. Instead a basic request-response API message, now our websites are having a conversation.

I Understand Now! What Possibilities! Web Hooks Rock My Casbah!

Now imagine what would be possible if Twitter allowed hooks for all the different events that happen on the platform.

  • Twitter, ping this URL of mine when someone I follow tweets or DM’s or replies to me.
  • Twitter, ping a different URL of mine when someone unfollows me.
  • Twitter, ping yet another URL when someone I follow updates her profile. She’s cute, and I need to stay on top of those thumbnail changes.

Twitter, just ping my scripts when stuff happens.

Now Make Hooks Programmable

Maybe I’m not interested in every single hookable event. How cool would it be if I could get a list of all my subscribed events through an API call?

  • Me: Yo Twitter, what do I care about? What am I subscribed to?
  • Twitter: Sup dawg, I heard you like profile changes and unfollows, so I send twirps to your tweets so you can twit when I twat.

But instead of jive talk you’d get back XML or JSON with event names and your hook URLs.

Take it one step further and suppose you only want your hook called when only certain people perform certain actions. Why not make setting and unsetting the hooks themselves programmable via the API?

Example: Call my URL http://myawesometwittersite.com/myhooks/profile/update when @cutetwitterchick updates her profile.

I could easily perform that logic in my own code, but telling Twitter to do it is that much cooler:

set_hook ( hook_name, hook_URL [, screename | ID | email ]  )
unset_hook ( hook_name, hook_URL [, screename | ID | email ]  )
list_hooks()

The Hooked Web

Jump to the future when all of your favorite sites implement programmable hooks. The pipedream, holy grail, end result is that you no longer even need Twitter, because it’s become a protocol. Just like blogs happily send pingbacks, you can install a Twitter-speaking, open sourced package on a Slicehost account that is your own personal Twitter. The protocol is extensible, so you can do things like:

  • display replied messages in threads as conversations
  • allow longer than 140 character messages
  • automatically hyperlink those stock symbols

And, unlike email, newsgroups,  and chatrooms, it doesn’t turn into a jacked-up, spammed to hell system, because you’re still only following what you want to follow. It’s a decentralized, pluggable architecture, and it integrates with any site using web hooks. At your service.

Take this platform one step further and visualize a user interface layer. You have a nice, decentralized activity stream–an open FriendFeed platform with all of your stuff. Maybe it’s organized by the source, or grouped by service type or however you want it organized. Messages can carry their own css styling, but only if you allow it. Messages can have interactive elements, but only if you allow them. And, holy crap, messages can come packaged with next actions! Twitter messages allow you to reply, calling a hook specified in the message. Flickr messages allow you to comment directly from this ‘StreamReader’ software. You subscribe to Mechanical Turk hits, and receive a new hit whenever you complete one.

One more step further. Not only do messages have next actions, but I can write plugins to attach next actions to any message, and I can do it conditionally. This would be like being able to write filters for Google Reader. For example, filter out all the messages from my group of PHP programming buddies when a message is about photography. I’m not interested, but those guys LOVE fucking cameras for some reason.

I’ve very conveniently ignored the implementation details of such an animal. Twitter could quickly become a push architecture with all of these hooks in place, and that turns quickly into n-squared network traffic patterns. Luckily we have some heavy brains working on xmpp and pubsub and the like, and maybe the end solution would be half push, half poll.

At this point it’s just really fun to imagine the possibilities of a read-write-read-write-read-write web.

→ 16 CommentsCategories: web hooks

Happy Valentizzle, Bitch

February 13, 2009 · Leave a Comment

Here’s a Valentine’s Day card I shooped up for Liz back in 2006. Thought I’d share it with the world this year.

→ Leave a CommentCategories: meh-ta

Mashing up the New York Times Best Sellers: ReadingRadar.com

February 3, 2009 · 13 Comments

readingradarI’m a fan of APIs, Web Services, and Mashups, and it’s no secret. One of my New Year’s resolutions is to create and publish more mashups, instead of simply lengthening my ever-growing ideas.txt file. A couple of weeks ago I relaunched, with little fanfare, DRM News, a domain I’ve owned for several years now. It’s simply an RSS aggregator from several sources revolving around digital rights management. It was trivial to slap together, to the point where I’d hardly consider it programming. But hey, at least I deployed something.

When I saw the Reddit post about the New York Times Best Seller API, I thought it would be a good opportunity to do a mashup for popular books. My hope was to create a site that could be on “auto-pilot” and maybe even send me a Amazon Associate check every now and then. I designed the site to use extensive caching of the NYT and Amazon APIs to minimize remote calls, but update the data often enough so that the information would be fresh. A few nights of hacking later and we have:

Reading Radar – From the New York Times Bestseller Lists

The NYT API was simple enough to use. The REST API offers three response formats, XML, JSON, or serialized PHP. I did find a bug in the API, and was very pleased how reactive the NYT API team was to resolve the problem. Kudos!

The guts of the mashup are simple. I’m using YUI for the layout and initial CSS, and JQuery for some visual effects on the list pages. On the server side, I’m using my current favorite thing ever, the Maintainable Framework. I was made aware of the framework by Mike Naberezny, one of the two main authors. Mike and I are ex-Zenders, and Mike was responsible for much of the code early on with the Zend Framework. The Maintainable Framework is very Rails-like, and because I’m familiar with some of the conventions in Rails, getting going with Maintainable was a cinch. Mike’s documentation is well-done, and I’m looking forward to using this framework for all things PHP for the foreseeable future, and hope to help out with bugs and maybe even some code.

Reading Radar is simple enough that I decided to forgo a database and just use a file based cache, powered by the Zend_Cache component of the Zend Framework. I’m using two caches, one that does not expire, and one that expired every several hours so that my data is fresh. I’m caching just a few API calls: the list of lists from the NYT API, which powers the left navigation; the actual lists themselves, which also provide book data; and the individual book histories. The NYT API offers a few other bits of information I’m not using, such as links to the first chapters and editorial reviews. The reason I don’t is because when I spot-checked several books, most of them did not have these links. My hope is that as the API matures, more of the data will find its way in.

To pull the Amazon information, I turn to the easy to use Zend_Service_Amazon component from Zend Framework. It provides me the ratings data, reviews, related products, images, number of pages, and so forth. So using the ISBN numbers from the NYT API, I’m able to query for the Amazon products, and spew those gratuitous affiliate links everywhere using the Amazon ASIN IDs.

So the end result, ideally, is that I have an automatically up-to-date site that I’ll never have to touch, is relevant to the search engines,  and will generate passive income. Realistically, it was a fun way to spend a few evenings doing what I love: making mashups. The site is far from perfect, and I’m sure there will be bugs to squash.

I’m love to hear opinions and ways to make this little site better. I know my designer buddies could offer suggestions, and I’d be interested to hear from the affiliate marketing gurus about better ways to integrate the Amazon links better than I have.  My real question is, how would you drive traffic to something like this, and would it even be worthwhile?

Jimmy Palmer and I have been thinking of some interesting ideas to extend sites like Reading Radar.. more on that as it develops.

→ 13 CommentsCategories: mashups

Liz at the Inauguration

January 21, 2009 · 1 Comment

Liz and her mom Audrey made the long haul to D.C. for the swearing in, leaving me at home for a week of bachelorhood/debauchery/lonelyness.

They decided to go for the cheap seats and watch from the Lincoln Memorial, and grabbed a nice spot on the steps. When we spoke yesterday through the saturated cellphone tubes, she told me she had done an interview with the Washington Post. While her interview was not published (that we know of), she and Audrey, and Audrey’s friend Dixie can be spotted in the background. Liz is the upper left in the blue coat, sending a Twitter message from her pink-clad iPhone.

LizAtInauguration

(click for fullsize)

The screencap is from 2:11 in the video, which is the one titled “The Swearing In”.

→ 1 CommentCategories: politik

Mashup Sighting

November 10, 2008 · Leave a Comment

Via the Hype Machine blog, I just found one of my mashups, Gblinker, mentioned in a textbook called Business Driven Technology. Chapter 14 has a section on mashups, and here’s a photo of the page. Judging by the other entries on the page, it looks like this section was lifted from the speek geeking page from the Mashup Camp III in Boston.

The idea for Gblinker was simple. At some of the conferences I attended, Google handed out some fun blinky LED pins as swag. I thought it would be a cool idea to rig one up to a computer and have it blink whenever I received a Gmail message. So to be lazy I decided to reuse what I could find instead of writing a program from scratch. I knew there was a Gmail widget for the Yahoo widget engine (formerly Konfabulator), so I started there. In the documentation, I discoveded that Yahoo widgets could make COM calls–what luck! So I wrote a simple dll in C# to flip on and off the serial port’s RTS bit, and modified the Gmail widget to call the dll. That’s all there was to it. The fun hack ended up winning 5th or 6th place at Mashup Camp in Boston, and as far as I know, was the first hardware mashup from any of the camps. The prize: a copy of Visual Studio :)

It’s always fun to see things you’ve done pop up out of nowhere, especially in dead-tree version. My favorite surprise was when my TagCloud prototype ended up in the book Yahoo Hacks. Coincidentally, the free sample chapter (PDF link) just happens to be the one on TagCloud.

→ Leave a CommentCategories: mashups

Laura Ann’s Music Debut

October 23, 2008 · 2 Comments

My niece, Laura Ann, spent some time with us over the weekend. Since I was recovering from the flu, we decided to have some relaxed playtime–a nice departure from the normal high-energy entertainment.

Laura Ann, 7, LOVES to play with PhotoBooth on the Mac, but finally grew tired of it after a while, so I though we would give Garage Band a try.  And boy, fun was had! Coming from the days of early PC based multitracking (ah, Cool Edit Pro, how I loved thee), I was quite impressed with the ease of Garage Band as well as the internal mic on the macbook. Here’s the result of some of the songs she recorded. She picked out the instrument arrangements from the included song catalog all by herself, and improvised all the lyrics in a bunch of one-takes. There’s no punch-in’s; all I did was bump her vocal track a tad and added some cheezy Garage Band effects. I absolutely loved the results and I’m sure we’ll be doing this some more in the future. 

The first song is one she’s doing with her school for Halloween. I’d heard her singing it randomly–she sings all the time–so i figured we might as well lay it down. Here’s “Five Little Pumpkins”

The next one is my favorite, “Funky Baby”. I had her list some funky stuff to be the lyrics and she came up with “Funky Baby, Turnip Greens, Funky Monkey, Cole Slaw, Cereal Box”. 

Next up is the roots ballad “I Love” which is destined to be a classic. She just sang about stuff she loves. No doubt the zoo tops that list, but I’m glad to be on it eventually. I especially like her lyric “I want to see what’s up there, in the serious beyond. I don’t know what to see with my eyes, it’s so special” And more howling.. she’s into wolves right now.

Laura Ann Herren

Finally is the way-over-the-top “I Have the Blues”. Pretty much speaks for itself.

→ 2 CommentsCategories: Music

Google Adsense for Feeds

August 19, 2008 · Leave a Comment

Looks like Google is now supporting Adsense ads for RSS feeds.

I remember the good ol’ days of Adsense. Now it’s about as ubiquitous as hit counters were in the 90’s. Still, I sure don’t mind seeing those Google checks in the mail, although they are fewer and further between these days.

The lack of Adsense support for feeds has been a pain point for bloggers and sploggers alike. Several startups exist solely to plug that hole. I guess we should get ready to start seeing ads in our feeds as often they appear on sites.

One more reason to love the keyboard shortcuts in Google Reader.

The announcement (if you must):

Inside AdSense: I feel the need… the need for feeds

→ Leave a CommentCategories: Geeking Out

Getting the number of posts per category in WordPress

August 13, 2008 · 9 Comments

This morning JTk presented me a WordPress riddle:

What I would like to be able to do is ask the MySQL if a category has any posts in it or if it is empty.  So I can do one thing if there are posts there and another if that category is empty.  And try as I might, no matter how hard I beat my head against it, all I get is a bruised head.  And, yes, I have to do this conditional because WP is flawed and will break if I try and do certain things with empty categories…..

Here is the schema ( maybe it helps )
http://blog.kapish.co.in/wp-content/uploads/2008/01/wp_db.png

And the worst part of this, is – I know this is trivial so I hate to even ask, but I guess I don’t hate enough not to ask :)  I asked the “community” and got told that there is not a WP function for this ( um, yeah, thanks…. ) that I shouldn’t make straight db calls ( arrrggg ), and that there are plugins that accomplish similar things….  But no real help, so I decided to ask a ninja….

So we want to get the number of posts per category. JTk, you’re in luck! A quick look at the schema (if that diagram is current) tells me we could query the wp_post2cat table and do a COUNT and a GROUP BY query on the category of interest. Even better, if wp_categories.category_count is what I think it is, that’s a simpler query. But let’s see what the code gives us already. In category.php, there are some tasty sounding functions like get_categories(), get_category(), get_category_by_path(), get_category_by_slug() and so on. I installed a clean version of WordPress, added some categories and assigned some fake posts to them. Then I tried some of these functions to see what’s up.

<?php var_dump(get_categories()); ?>

Output:

array(3) {
  [1]=>
  object(stdClass)#68 (15) {
    ["term_id"]=>
    &string(1) "3"
    ["name"]=>
    &string(14) "First Category"
    ["slug"]=>
    &string(14) "first-category"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "3"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    &string(0) ""
    ["parent"]=>
    &string(1) "0"
    ["count"]=>
    &string(1) "4"
    ["cat_ID"]=>
    &string(1) "3"
    ["category_count"]=>
    &string(1) "4"
    ["category_description"]=>
    &string(0) ""
    ["cat_name"]=>
    &string(14) "First Category"
    ["category_nicename"]=>
    &string(14) "first-category"
    ["category_parent"]=>
    &string(1) "0"
  }
  [2]=>
  object(stdClass)#69 (15) {
    ["term_id"]=>
    &string(1) "4"
    ["name"]=>
    &string(15) "Second Category"
    ["slug"]=>
    &string(15) "second-category"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "4"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    &string(0) ""
    ["parent"]=>
    &string(1) "0"
    ["count"]=>
    &string(1) "2"
    ["cat_ID"]=>
    &string(1) "4"
    ["category_count"]=>
    &string(1) "2"
    ["category_description"]=>
    &string(0) ""
    ["cat_name"]=>
    &string(15) "Second Category"
    ["category_nicename"]=>
    &string(15) "second-category"
    ["category_parent"]=>
    &string(1) "0"
  }
  [3]=>
  object(stdClass)#90 (15) {
    ["term_id"]=>
    &string(1) "1"
    ["name"]=>
    &string(13) "Uncategorized"
    ["slug"]=>
    &string(13) "uncategorized"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "1"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    &string(0) ""
    ["parent"]=>
    &string(1) "0"
    ["count"]=>
    &string(1) "1"
    ["cat_ID"]=>
    &string(1) "1"
    ["category_count"]=>
    &string(1) "1"
    ["category_description"]=>
    &string(0) ""
    ["cat_name"]=>
    &string(13) "Uncategorized"
    ["category_nicename"]=>
    &string(13) "uncategorized"
    ["category_parent"]=>
    &string(1) "0"
  }
}

OK, so it sounds like the person who told JTk there were no built-in WordPress functions for this was lying :)   This function tells us exactly what we need. A couple of interesting observations, though. I had a category named “Empty Category” with no posts, which does not show up here. The documentation page tells me that I can pass a parameter to include the empty ones easily enough:

<?php var_dump(get_categories(array(‘hide_empty’=>false))); ?>

array(4) {
  [0]=>
  object(stdClass)#67 (15) {
    ["term_id"]=>
    &string(1) "5"
    ["name"]=>
    &string(14) "Empty Category"
    ["slug"]=>
    &string(14) "empty-category"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "5"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    &string(0) ""
    ["parent"]=>
    &string(1) "0"
    ["count"]=>
    &string(1) "0"
    ["cat_ID"]=>
    &string(1) "5"
    ["category_count"]=>
    &string(1) "0"
    ["category_description"]=>
    &string(0) ""
    ["cat_name"]=>
    &string(14) "Empty Category"
    ["category_nicename"]=>
    &string(14) "empty-category"
    ["category_parent"]=>
    &string(1) "0"
  }
  [1]=>
  object(stdClass)#68 (15) {
    ["term_id"]=>
    &string(1) "3"
    ["name"]=>
    &string(14) "First Category"
    ["slug"]=>
    &string(14) "first-category"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "3"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    &string(0) ""
    ["parent"]=>
    &string(1) "0"
    ["count"]=>
    &string(1) "4"
    ["cat_ID"]=>
    &string(1) "3"
    ["category_count"]=>
    &string(1) "4"
    ["category_description"]=>
    &string(0) ""
    ["cat_name"]=>
    &string(14) "First Category"
    ["category_nicename"]=>
    &string(14) "first-category"
    ["category_parent"]=>
    &string(1) "0"
  }
  [2]=>
  object(stdClass)#69 (15) {
    ["term_id"]=>
    &string(1) "4"
    ["name"]=>
    &string(15) "Second Category"
    ["slug"]=>
    &string(15) "second-category"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "4"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    &string(0) ""
    ["parent"]=>
    &string(1) "0"
    ["count"]=>
    &string(1) "2"
    ["cat_ID"]=>
    &string(1) "4"
    ["category_count"]=>
    &string(1) "2"
    ["category_description"]=>
    &string(0) ""
    ["cat_name"]=>
    &string(15) "Second Category"
    ["category_nicename"]=>
    &string(15) "second-category"
    ["category_parent"]=>
    &string(1) "0"
  }
  [3]=>
  object(stdClass)#90 (15) {
    ["term_id"]=>
    &string(1) "1"
    ["name"]=>
    &string(13) "Uncategorized"
    ["slug"]=>
    &string(13) "uncategorized"
    ["term_group"]=>
    string(1) "0"
    ["term_taxonomy_id"]=>
    string(1) "1"
    ["taxonomy"]=>
    string(8) "category"
    ["description"]=>
    &string(0) ""
    ["parent"]=>
    &string(1) "0"
    ["count"]=>
    &string(1) "1"
    ["cat_ID"]=>
    &string(1) "1"
    ["category_count"]=>
    &string(1) "1"
    ["category_description"]=>
    &string(0) ""
    ["cat_name"]=>
    &string(13) "Uncategorized"
    ["category_nicename"]=>
    &string(13) "uncategorized"
    ["category_parent"]=>
    &string(1) "0"
  }
}

Sure enough,there is the empty category. I also noticed that the article count appears in both ->count and ->category_count. I’ll just assume the cached version, ->category_count, will remain correct through the code, but I’m just guessing it doesn’t matter which member we use.

So, that’s fine if we want the whole collection, but what about individual categories? Fortunately, we can extract this same data for both the category ID as well as the name of the category. Here’s an example of each, querying my “First Category” category:

Using ->category_count

<?php var_dump( (int) get_category(‘3′)->category_count); ?>

<?php var_dump( (int) get_category_by_slug(‘First Category’)->category_count; ?>

<?php var_dump( (int) get_category_by_slug(‘first-category’)->category_count); ?>

Using ->count

<?php var_dump( (int) get_category(‘3′)->count); ?>

<?php var_dump( (int) get_category_by_slug(‘First Category’)->count; ?>

<?php var_dump( (int) get_category_by_slug(‘first-category’)->count); ?>

All of these return the correct result, int(4). Good on ya, WordPress.

Now let’s see how robust the function is. Will it choke on a non-existent category, or return a zero like it should?

<?php var_dump( (int) get_category(‘6969′)->category_count); ?>

<?php var_dump( (int) get_category_by_slug(‘This Damn Category’)->category_count); ?>

Both of these do indeed return zero, which is nice and correct.

In practical terms, you can use this code stub to do something will all categories depending on whether or not there are posts:

foreach (get_categories(array('hide_empty'=>false)) as $category){
	if ($category->count > 0){
		//has posts, do something
	}else{
		//no posts, do something else
	}
}

That’s all there is to it!  There’s definitely a built in function to inspect categories, and even some nice examples to go with. Happy WordPress hacking, and don’t forget that now’s the time to upgrade to PHP 5 if you haven’t already :)

→ 9 CommentsCategories: wordpress