Blog

Twitter Tools 1.5b3

Posted in: News, WordPress

I’ve got a new beta version of Twitter Tools ready for testing. Twitter Tools is a WordPress plugin that creates an integration between your blog and your Twitter account.

This release is just the same as 1.5b2, but fixes the duplicate custom field issue for people running WordPress 2.6+.

Hopefully this will be ready for a full release shortly, with only minor changes (if any). I guess we’ll find out soon. :)

The download and more information are available on my WordPress Plugins page.

If you have any trouble with this, please open a thread in the WP Support Forums and e-mail me a link to the thread.

Popularity: 1% [?]

3 Comments |

Posted September 6th, 2008 @ 12:39 PM

WordPress 2.6.x “Duplicate Custom Field” Issue

Posted in: WordPress, Development

This is an explanation of a solution to a specific problem with one of my plugins, but the issue is larger and should be understood by all WordPress plugin developers. Posting the write-up here with the hope it will be useful.

The issue is a change in the activity happening in the save_post hook. I’d probably consider this change in WP to be a bug or regression - but it’s out there now in 2.6.x, so as plugin developers we have to work around it (even it a better solution is found in a future WP release).


I received a couple of bug reports from people recently that Twitter Tools was creating duplicate custom fields for them. This struck me as odd, as that code hasn’t changed and it’s pretty darn simple. Turns out it was WordPress that had changed, and Twitter Tools (and probably many other plugins) now need to adapt.

The Problem

After publishing a post, future edits to that post would cause additional, duplicate custom fields to be added to the post. Here is an example:

Duplicate Custom Fields

The Cause

It took be a little while to debug this. The code is simple:

function aktt_store_post_options($post_id) {
[…]
if (!update_post_meta($post_id, ‘aktt_notify_twitter’, $notify)) {
add_post_meta($post_id, ‘aktt_notify_twitter’, $notify);
}
}
add_action(’save_post’, ‘aktt_store_post_options’, 1);

This code fires when a post is saved, and updates the custom fields appropriately.1 The aktt_store_post_options function is attached to the save_post hook, so that code is executed when a post is saved.

The save_post hook actually sends two parameters $post_id (just the ID) and $post (the full post data). I’d chosen to use just the $post_id parameter here because I only needed to store the post ID along with the custom field key and value.

This works great on versions of WordPress prior to 2.6, but a new feature in WP 2.6 changed that. The new feature in question is: post revisions.

With the post revision feature, post revisions are saved along with the post - also triggering the save_post hook. For these, the $post_id (for an already published post) being passed to my function is the id of the post revision, not the original post id. As a result, my function is checking to see if the newly created revision (not the original post) has this custom field and adding it when it doesn’t find it.

WordPress then seems to aggregate the custom fields from all revisions into the display for the post on the Write page in the admin interface. I haven’t looked into the details of this yet, but that’s a fair guess. The point is that these custom fields are being added to post id 12, 13, 14, etc. but all being displayed for post id 12.

This isn’t what we want.

The Solution

The solution is to look at the post itself instead of just the post id. Several changes need to be made to do this.

The code that attaches my function to the save_post hook needs to be changed to request both parameters instead of just the post id:

add_action('save_post', 'aktt_store_post_options', 1, 2);

The last parameter tells it to give my function 2 parameters. Now my function will get the full post info, not just the post id.

Now my code needs to get smarter.

A post revision in WP 2.6 has a couple of things we can look for:

  • $post->post_type - this will be set to ‘revision’ for post revisions.
  • $post->post_parent - this will be set to the post id of the original post.
  • $post->post_status - this will be set to ‘inherit’ for post revisions.
  • $post->ancestors - this is an array of post ids - I’ve only seen it have a single item in the array so far, but I haven’t looked through the code around it.

I think we can just work with the $post->post_type in this situation - we just add some code at the top of the function:

function aktt_store_post_options($post_id, $post) {
if ($post->post_type == ‘revision’) {
return;
}
[…]
if (!update_post_meta($post_id, ‘aktt_notify_twitter’, $notify)) {
add_post_meta($post_id, ‘aktt_notify_twitter’, $notify);
}
}

Now we won’t do anything for revision posts, but the regular posts get the proper post meta treatments.

Here is the full replacement for the original code (top of post):

function aktt_store_post_options($post_id, $post) {
if ($post->post_type == ‘revision’) {
return;
}
[…]
if (!update_post_meta($post_id, ‘aktt_notify_twitter’, $notify)) {
add_post_meta($post_id, ‘aktt_notify_twitter’, $notify);
}
}
add_action(’save_post’, ‘aktt_store_post_options’, 1, 2);

Conclusion

Prior to WP 2.6, you could expect that the data coming through the save_post hook was a post. The post could have various states (draft, published, etc.) but it was a single logical data type. In WP 2.6.x, you need to account for a new data type coming through the save_post hook: revisions.

I don’t think revisions should be sent through the same save_post hook that normal posts are sent through. I think that sending them through a new save_revision hook would have been a better solution (and still would be). But since WP 2.6.x does treat them this way, our plugins need to handle the situation gracefully.

Remember this is in issue in WP 2.6+ only, so when working around it make sure your code is conditional so that it is backwards compatible with WP 2.5.x, 2.3.x, etc.

This change is already committed to the SVN repository for Twitter Tools, I’ll get a new beta out soon.

  1. Note that the update_post_meta function was changed in WP 2.5 or 2.6 to also do an add if needed, but does not do this in WP 2.3. [back]

Popularity: 1% [?]

5 Comments |

Posted September 6th, 2008 @ 9:09 AM

JabberWerx AJAX

Posted in: Crowd Favorite, Case Studies, News

JabberWerx AJAX has hit the streets!

Group Chat

JabberWerx AJAX is a set of thin client JavaScript libraries that can be easily embedded into existing web sites and web applications to add rich push functionality (chat, live content updates, etc.) to them.

There are easy to drop in bits for one to one chat (tabbed), group chat and a library that allows you to push data updates to your page (think pushed stock or twitter updates). It’s a pretty good system to get up and running easily.

Most of us think of XMPP/Jabber as a chat protocol, and it does that very well (as anyone using GTalk can attest to); but it’s also got a very interesting place in the web 2.0 space as a real time data push channel. Using push instead of pull/poll can dramatically reduce server loads, and also gives you new data faster.1

Building libraries and building applications are two very different things. With applications you need to make functionality available to your end users. With libraries, you need to build robust and complete solutions that will be used in ways you probably haven’t thought of yet; abstraction and building small, distinct components is key. It’s a fun and different challenge.

I’m very proud that my company, Crowd Favorite, was able to work with the great folks at Jabber to help bring this concept to fruition.

  1. Steve has a great Q&A on this. [back]

Popularity: 1% [?]

2 Comments |

Posted September 5th, 2008 @ 10:29 AM

Around the web

1 Comment

A Day in NY

Posted in: General

This weekend’s trip to New York is primarily a chance for me to see a game at Yankee Stadium (Saturday) before they tear it down after the season. However, to book the flight using miles we had to fly in on Thursday instead of Friday, so we got a bonus day to spend doing the tourist bit.

We walked around a lot during the day, checking out a bunch of stuff including going up to the observation deck at the Empire State Building. We tried to swing through B&H Photo but forgot they’d be closed early on Friday (probably a good thing - avoided the temptation). Then back to the hotel for a quick siesta and then out again in the evening.

The evening was quite excellent. We had planned to go to the Metropolitan Museum of Art, but decided to walk through Broadway first to see if we could get last minute tickets to a show.

The name and reviews for [Title of Show] caught my attention, and we were able to get seats for the performance that was starting in about an hour. It was really fantastic. I think I connected with it a little bit because I’ve done my share of creating things and trying to make them succeed. The writing was clever, the music was very pleasant and the performances were excellent. Highly recommended.

From there, it was time to eat again and we hopped in to Lucille’s1 - BB King’s (no relation) restaurant and music club. A blues band was just starting a set, and we were able to get a table near the stage. The music and food were excellent, and we stayed for a while enjoying both.

A very enjoyable evening, with both the show and restaurant exceeding expectations - always nice when that happens.

  1. Lucille is the name of BB King’s guitar. [back]

Popularity: 4% [?]

2 Comments |

Posted August 29th, 2008 @ 10:27 PM

Twitter Tools 1.5b2

Posted in: News, WordPress

I’ve got a new beta version of Twitter Tools ready for testing. Twitter Tools is a WordPress plugin that creates an integration between your blog and your Twitter account.

This release has a couple of bug fixes (from version 1.5b1) and a couple of new features:

  • fixed a logical bug that made the “exclude replies” option work backwards (oops!)
  • removed a try/catch for PHP 4 compatibility (oops!)
  • added support for hashtags (linked to search.twitter.com)
  • abstracted all API endpoints and URLs so that it can theoretically support any service that implements the Twitter API

Hopefully this will be ready for a full release shortly, with only minor changes (if any). I guess we’ll find out soon. :)

The download and more information are available on my WordPress Plugins page.

If you have any trouble with this, please open a thread in the WP Support Forums and send me the link.

Popularity: 8% [?]

13 Comments |

Posted August 27th, 2008 @ 11:48 AM

Around the web

1 Comment

Projector Choices

Posted in: Technology

I need to get a projector for the office and I’m having a hack of a time trying to decide on a relatively cost-effective projector that has a 1280 pixel wide resolution (generally 720p).

These two are the current leading contenders:

I have a Sharp for my home projector and I’m quite satisfied with it. Some of what I’ve read makes me think the Sharp is a better machine, however the Optma is smaller (which is important when it’s sitting on a conference table as it will, for a while at least) and has a bunch of good reviews, so I’m not that worried about it either.

Anyone have experience with either model or can see anything important in the specs that I could be overlooking?

Popularity: 9% [?]

1 Comment |

Posted August 22nd, 2008 @ 3:25 PM

ShareThis 2.3

Posted in: News, WordPress

There’s a new version of ShareThis out this evening. This release fixes a bug in version 2.2 where the ShareThis button might not be visible correctly if your WordPress theme shows post excerpts on certain pages.

ShareThis is a plugin that makes it easy for your visitors to share your content to other people or online destinations and social web sites. You can see it in action by clicking the ShareThis link at the bottom of any of the posts or pages on this site.

The download and more information are available on my WordPress Plugins page.

If you have any trouble with this, please open a thread in the ShareThis Support Forums and the team will be happy to help you out.

Popularity: 11% [?]

9 Comments |

Posted August 21st, 2008 @ 6:29 PM

New Lijit WordPress Plugin

Posted in: Crowd Favorite, Case Studies, News, WordPress

There’s a new and improved Lijit WordPress plugin available for download. This version is one that my company, Crowd Favorite, had the pleasure of helping build.

The new plugin makes it easy for you to enable Lijit search from the default WordPress search box, or you can add in the full Lijit widget wijit if you like.

I use Lijit search on alexking.info, where I aggregate content from all of the various sites and services I publish content on. The Lijit search performs a search across all of that content - pretty slick.

I’m pretty pleased with the overall user experience for the plugin. It nicely handles set-up for new or existing users, gives straightforward options and instructions, and even brings in your Lijit search stats with a new link on the WordPress admin dashboard.

Many thanks to the great folks we were able to work with at Lijit in building the new plugin. It was a pleasure working with people that care deeply about their users and are also technically astute.

Popularity: 13% [?]

11 Comments |

Posted August 20th, 2008 @ 12:17 AM

Next Page »

About This Site

This is the personal web site of Alex King, an independent developer based in Denver, Colorado USA. More...


Crowd Favorite

Crowd Favorite is my software and web development business.

We build web applications, design and develop custom WordPress themes and plugins, and build custom sites using WordPress as a CMS.


I also have a tumblog that aggregates my online content from other services (Twitter, Flickr, del.icio.us. etc.).

I'm voting for Barack Obama

Ads