Posts in Category: WordPress

Two New Plugins: SimpleTest for WordPress and My Plugin Libraries

My two new WordPress plugins are now available at wordpress.org.

  • Toppa Plugin Libraries for WordPress: here is Post to Post Links II error: No post found with slug "toppa-plugin-libraries-for-wordpress", and here is the download page at wordpress.org.
  • SimpleTest for WordPress: here is Post to Post Links II error: No post found with slug "simpletest-for-wordpress", and here is the download page at wordpress.org.

If you’re wondering why I’m releasing new plugins before finishing Shashin 3, it’s because these plugins are key building blocks for the new version of Shashin (they give me autoloading, unit testing, a WordPress functions facade, and more).

Shashin Update

I’ve updated the dev version of Shashin 3 on GitHub. If you installed it before, you’ll need to do a fresh installation with this release, as there are database changes. Note you can install Shashin 3 side-by-side with an existing Shashin 2 installation – they will not interfere with each other. The functionality is still limited to the Album management screen. The changes are to internal structure (the dependency injection container is more robust, and I straightened out some issues with the overall object model). Please see the installation steps in my earlier post about Shashin 3.

I won’t be making further changes to Shashin 2 on wordpress.org. I know that many people have been experiencing seemingly random problems synchronizing albums since Picasa switched to using https earlier this year. Fixing this is not practical in Shashin 2. Shashin 3 does its album synchronizing in a different and more robust way. I do my work on Shashin in 20 minutes batches as I ride back and forth to work on the subway (that’s when I have time), so I’m focusing on the new version only.

Please use the comments section for questions, feedback, and feature requests for Shashin 3.

Shashin 3 Development Version Available

Only a year overdue, I have a development version of Shashin 3 available for download. That’s the good news. The bad news is that all you can do with it right now is use its Tools Menu to sync albums. The Shashin tags and Highslide are not implemented yet. So it may not seem like much, but it’s been probably 80 hours of work so far. This is a complete rewrite, as I found myself unable to provide adequate support for the previous version, given the way I wrote it.

Over the past six months I have immersed myself in Clean Code, Agile Software Development, Refactoring, Growing Object-Oriented Software, Guided by Tests, and more. I’ve been programming since I was 12 (which was a long time ago…) and in recent years thought I had become darn good at it. What I’ve been learning in recent months has been both humbling and exciting. Although the Shashin 3 menus look almost the same as before, if you look under the hood you will see something very different from the old Shashin. I hope to have the time to say more about it later.

But for now, I’m hoping those who’ve had the patience to wait this long for the next version of Shashin will be willing to test this development version. I’m especially looking for feedback from people who have had difficulty syncing albums in Shashin 2, as the new version does syncing in a completely new way, which I expect will be faster and more reliable. It’s designed to be extremely flexible, so after I get the Picasa functionality completed, I intend to add support for Flickr, Youtube, etc. The one limitation right now is that it supports public albums only.

Here’s what you need to do:

  1. Download the Shashin 3 development version at github: https://github.com/toppa/Shashin/
  2. After you unzip it, rename the folder to “shashin3alpha” (the folder name github assigns will change every time I update it)
  3. Download “Toppa-libs” from github: https://github.com/toppa/Toppa-libs
  4. After you unzip it, rename the folder to “toppa-libs”
  5. Upload both to your plugins folder, and activate them. You will then see “Shashin3Alpha” in your Tools menu. Note that you can install the new version side-by-side with an existing Shashin installation. They will not interfere with each other. That means you can uninstall it as well, without affecting your Shashin 2 installation.
  6. If you run into any issues, please report them on github. It will be much easier for me to track problems there than in my blog’s comments: https://github.com/toppa/Shashin/issues

What I’m hoping to do from now on is add a new piece of functionality to it roughly once a week, and upload it to github. I’ll tweet any major updates. Once all the basic functionality is in place, I’ll update the plugin repository at wordpress.org

The Bane of WordPress Plugin Development: register_activation_hook

If you’re developing a WordPress plugin, any initial, one-time setup work your plugin needs is done through a call to register_activation_hook() (such as registering settings, or creating a database table). Debugging problems with code you call through this hook is notoriously difficult. I think I’ve pulled my hair out over each one of the many things that can go wrong, so I thought I’d share my hard-earned solutions (these all apply to the current version of WordPress, 3.0.4):

  • You get a “Cannot redeclare” error when activating your plugin from the plugin menu: this may tempt you to put a if function_exists() wrapper around your function (or if class_exists() around your class). But don’t waste your time – all it really means is that there was an error of some kind. Any kind of logic error within your function (such as an incorrect number of arguments to a function call) will result in this error being displayed. Don’t be fooled.
  • You try using echo or print to debug, but never see any output: normal output isn’t shown during plugin activation. If you want to see some debugging output, call trigger_error() instead. This will force your message to be displayed in the plugin activation status box.
  • It says “plugin activated” after you activate from the plugin menu, but nothing actually happened: this typically means there’s something wrong with your arguments to register_activation_hook, and it just fails silently. The first argument must be the path to your main plugin file (i.e. the file with the plugin comments at the top – __FILE__ will do fine), which is where your activation function must be. The existence of this argument is deceptive, as it suggests you could put the function in another file (you could, I suppose, put your call to register_activation_hook in another file, and then put your callback function in the main file, but I can’t think of a good reason to do that). The second argument is the function name – if you want to get fancy, it’s fine to use callback pseudo-types.
  • You’re having trouble using a global variable: this limitation is actually documented.

Two general guidelines I recommend are:

  1. Temporarily put an “activate” button on your plugin’s settings page, and have it call your activation method. This will allow you to separate any problems with your activation function from any problems that may exist in how you’re calling it through register_activation_hook. This is a good way to expose errors that are otherwise hidden behind the “cannot redeclare” error.
  2. Write unit tests and do test-driven development! This will give you a way to verify the functionality of your code as you work on it, and will let you know immediately if you break anything. I’ll have an upcoming post on how to use SimpleTest with WordPress.

Welcome Back, Toppa (Again), and Resuming Plugin Support

UPDATE 3/13: Shashin is not working properly with private albums, since Picasa introduced https support. Also, for public albums, enter their urls with “http” for now (not “https”). I have a major rewrite of Shashin underway, so I will address these problems in the upcoming version. I’m still putting in overtime at my job, so a new version is likely at least one month away. Thanks for your patience.


2010 was the year I worked too much. I have one job, but I was doing the work of two. That’s why my blog was mostly quiet last year, and I didn’t have time to update my WordPress plugins. But I remind myself this is a “nice to have” problem, as there are many people in need of any job these days. I’m happy to report that we are hiring for a new position, which means I can go back to doing one job in a few months (if you’re interested in being an Agile Product Owner for the U Penn SOMIS Web Team, you can apply here – look for reference number 110129833).

I still have too much to do for the next few months, but I’m freed up enough at this point that I can start responding to support questions again for my plugins. I’ve started work on a rewrite of Shashin, but at this point I can’t say when it’ll be done. Please use the comments section of this post for questions (I’ve closed comments on the previous support post).

I’ve added threaded comments to my site’s theme (keep an eye out for the “reply” links under each comment). This will help make different support discussions easier to follow.

Plugin Support Thread

I need to temporarily discontinue offering personal support for my WordPress plugins. I sincerely apologize for this. I’m putting in time nights and weekend at my job – that and my family have to come first. I don’t expect my situation at work to be like this for more than a few more months. In the meantime, please use the comments thread on this post if you have questions, and I’m hoping you can support each other (there are at least a few savvy users of my plugins who have posted helpful comments in the past). I will try to jump in if I can to offer help, but I can’t promise it right now.

Displaying WordPress Posts by Category, Even If They’re Not Recent

A couple years ago I wrote a post describing how to limit posts on your WordPress home page to ones in specific categories. A limitation of this approach is that it can only show recent posts that are in The Loop. Others have solved this problem as well, but all the solutions I’ve seen have this same limitation. What if posts in the categories you specify aren’t among your recent posts? Your home page would show no posts!

This was a problem for my new home page design. I’ve divided my site’s content into 3 major topics, and I show hyperlinked titles for the 3 most recent posts in each topic. But it’s possible that a topic may not have any posts among the most recent 10 posts, which is all The Loop knows about (10 is WordPress’ default setting for how many posts to show on your home page). So I want to get the most recent 3 posts for each topic, regardless of whether they happen to be in The Loop.

To do this, I created the following function and put it in my theme’s functions.php file. Note that I’m using a straight SQL query, which means this is not guaranteed to work in future versions of WordPress (the WP coders do a good job of maintaining a consistent programming API across versions, but they do change the database sometimes).

function get_top_category_posts ($term_ids) {
    global $wpdb;
    $top_3 = '';

    $results = $wpdb->get_results("select ID, post_title, post_date from wp_posts p
            inner join wp_term_relationships r on p.ID = r.object_id
            inner join wp_term_taxonomy t on t.term_taxonomy_id = r.term_taxonomy_id
            where t.term_id in ($term_ids) and p.post_type = 'post'
            order by post_date desc limit 3", ARRAY_A);

    foreach ($results as $result) {
        $top_3 .= "<li>" . date("M d", strtotime($result['post_date'])) . " - "
            . '<a href="' . get_permalink($result['ID']) . '">'
            . $result['post_title'] . "</a></li>\n";
    }

    return $top_3;
}

I then call it like this from my custom home page (where I’m not using The Loop at all):

<ul>
<?php echo get_top_category_posts('6,12,89,90,98,105,106,115'); ?>
</ul>

I’m passing the IDs for the categories I want. The easiest way to get a category ID is to go to its edit screen and note the cat_ID in the URL. This works for tag IDs also.

There are 3 database tables involved in the query: wp_posts contains your posts, wp_term_taxonomy contains the term IDs for categories (and tags), and wp_term_relationships connects the posts to their categories. Note that a cat_ID or tag_ID you see in an edit screen URL is actually called a term_id within the database. In this case I’m only retrieving the date and title of each post, but you could retrieve the entire post if you want (see the description of the wp_posts table).

If you want to simplify the query to improve performance, you can eliminate one of the table joins if you manually look up the term_taxonomy_id that corresponds to each category’s term_id, and pass those instead:

select ID, post_title, post_date from wp_posts
        inner join wp_term_relationships on ID = object_id
        where term_taxonomy_id in ($taxonomy_ids) and post_type = 'post'
        order by post_date desc limit 3

The downside of doing this is that every time you add a new category or tag, you’ll have to go into your database and look up its term_taxonomy_id. So I don’t recommend doing it this way unless you’re comfortable poking around in mySQL.

(Note to fellow cranky programmers: I know this is poorly abstracted – the need here is simple enough that, to me, abstraction didn’t seem worth the trouble).

Shashin Support Mea Culpa

Shashin’s popularity has recently spiked, which makes me happy, but it’s also caused a spike in support requests (my last Shashin post has 127 comments). Until the start of this year, I had been diligent in providing support, but my day job recently has been seeping into my nights and weekends, significantly reducing my availability. It’s likely to stay that way for the next several months.

I started on some major enhancements to Shashin early in the winter – most notably flickr and twitpic support – but it’s been on hold for a while now. At this point, my guess is that it’ll be summer before I can get back to it.

Please use the comment section on this post for support questions. I will do my best to provide responses twice per week. I will most likely not be able to delve into problems that involve the particulars of your server configuration, such as problems with connecting to Picasa via curl for unlisted albums (as those kinds of issues can sometimes take hours to work through). But general usage questions and Highslide issues are usually easier to address, and I’ll do what I can to help out.

Thanks everyone for your patience, and please remember I’m doing this for free 🙂 (donations come in very rarely).

UPDATE: I just posted a minor update, version 2.6.3:

  • Shashin is now compatible with WP 3 beta 1 (WP 3 appears to automatically unserialize options, which was causing errors)
  • Added support for child themes
  • Can now uninstall and delete Shashin from the main plugin page (added uninstall hook for existing uninstall function)

Shashin 2.6: Snowpocalypse Edition

Shashin 2.6 is now available for download at wordpress.org. I figure the end of the world is a good time to release a new version. Here’s what’s new:

  • Added support for unlisted Picasa albums (finally!). You must have the PHP curl extension installed to use this feature. I believe most PHP installations include curl, but some hosting providers may need you to ask them to enable it for your account.
  • Added ability to group albums by Picasa user accounts when using the [salbumthumbs] tag.
  • Bug fix: the EXIF support introduced in Shashin 2.4 caused an error when syncing albums in the Windows version of mySQL. Many thanks to MC for letting me debug this on his Windows server.
  • Bug fix: Shashin’s automatic album syncing was interfering with scheduled jobs from other plugins in some circumstances. This is fixed. Note that you also need WordPress 2.9.1 or higher, as this was related to a wp-cron bug in WordPress 2.9.

Also, I’ve installed the Subscribe to Comments plugin, so if you have a support question, you can get an email notification when I reply, instead of having to remember to check back.

UPDATE: I’ve made a couple bug fixes: v2.6.1 fixed another bug related to Windows servers, and v2.6.2 fixed a bug related to Google servers outside the US (see this comment for details).

Shashin 2.5 Now Available

Shashin 2.5 is now available for download at wordpress.org. This actually isn’t the major release I’ve been working on – it doesn’t include Flickr and Twitpic support (that’s still a work in progress). But it does include one very cool new feature: a jQuery based WYSIWYG browser for adding Shashin photos to your posts. You can now browse albums from within the editor, and click on photos to add them to your post (it will write [simage] and [sthumbs] tags for you). To use it, click the Picasa icon in the editor’s media button bar. This great feature was contributed by Sune Pedersen.

A couple usability items to point out with it:

  • If you mouse over a thumbnail on the left hand side, then the right hand side will show you a larger version of it, and vice versa. I found this a little confusing at first, but it’s actually a good way of using the available screen real estate effectively
  • To select a photo, just click it once. Don’t try to drag it. After you click it, you’ll see it appear under the “Selected” heading. If you change your mind and need to remove a photo that you selected, just click it, and it will be removed from the “Selected” area.

The other important new feature is that automatic album syncing is now done every 10 hours instead of once daily. This is because Picasa video URLs now expire every 11 hours, as I described previously. If you have any videos in your albums, then you will want to turn on this feature in the Shashin Settings menu.

There are also two bug fixes:

  • “Next” and “previous” links for browsing album photos now work in Google Chrome and Safari (this is actually a workaround for a webkit bug)
  • Album title links in the album thumbnails sidebar widget now point to the correct URL

Now back to work on version 3.0…