Sun
25
May
2008
10:39

WordPress 2.5 and Cricket Moods

Tags:   , ,
Mood:  Cool emoticon Cool, Positive emoticon Positive

One of my major annoyances with WordPress 2.5 was with the Cricket Moods plugin, whose author is planning to produce an all-new and much spiffier thing rather than an update, which is really cool, and I’m sure it’ll be great. But as he has a life[1], it may be a while before he can devote enough time to the project. All of which left Cricket Moods a bit broken in WordPress 2.5. It actually works perfectly, but the box where you select moods just appears at the bottom of the Write page in WordPress 2.5, far below sections that I never use, way below the fold, and where I found myself forgetting to add moods to my posts. Now as I like to add at least one to every post[2], this was slightly annoying, so I had to do something quite shocking and out of character: I looked at the code. Yes, I opened up the plugin in my text editor and tried to work out what needed to change to make it a bit more WordPress 2.5 friendly.

My first attempt, based on borrowing code from another plugin, managed to put the box into a WordPress 2.5 style clicky thing, but it was still at the bottom of the page, which wasn’t quite what I wanted. Then I saw a reference somewhere to the function used in WordPress 2.5 to add these boxes:

WordPress Codex: Function Reference/add meta box

This gave me the clue I needed. You insert a box with this function:

<?php add_meta_box('id', 'title', 'callback', 'page', 'context'); ?>

I’d worked out the right things to put in most of the values, but the one I’d missed was “context”. Reading the Codex told me that this could be either “advanced”, which would drop the box into the bottom of the lower section of the Write page, or “normal”, which would add it to the upper section, under the Tags and Categories sections. I updated my edits, and woo hoo, it worked. It’s still not quite where I want it, but for a non-coding person it’s as good as I’m likely to get it.

I’ve left a comment on the author’s site explaining what I’ve done, but I’ll repeat it here for anyone who’s interested. Note that these changes will most likely make Cricket Moods crash and burn on versions of WordPress lower than 2.5. A proper coder would write stuff that runs the correct code based on the version being used, but as this was purely for my own use, I didn’t attempt that. Anyway, this is what I did:

I started with a copy of cricket-moods.php version 3.6, and made the following changes:

  1. Added this function on Line 289, just above the beginning of cm_list_select_moods. It could probably have gone somewhere else, and for coding reasons I don’t know about, might have been better somewhere else, but this works:
    289
    
    function cm_add_moods_box() {add_meta_box('cricket', 'Moods','cm_list_select_moods', 'post','normal');}
  2. Then I changed the bit of code that inserts the box. In the original, this is on line 312, but after adding the first bit it will be line 313. It looks like this:
    313
    
    echo '<fieldset id="cm_moodlist" class="dbx-box"><h3 class="dbx-handle">'. __('Moods', 'cricket-moods') .'</h3><div class="dbx-content">';

    After adding the first bit, that’s now line 313. That whole line needs to be replaced with the next bit of code. You can either delete it or just make it a comment by adding // at the beginning of the line. Either way, make the new line look like this:

    313
    314
    
    // echo '<fieldset id="cm_moodlist" class="dbx-box"><h3 class="dbx-handle">'. __('Moods', 'cricket-moods') .'</h3><div class="dbx-content">';
    echo '<fieldset id="cm_moodlist" class="meta_box">';

    Yes, that is a bit simpler, isn’t it? That’s partly because of how the new code works, and partly because I haven’t attempted to do the translation string thingy (if you don’t know about that, you probably don’t want to…).

  3. At the end of the cm_list_select_moods function, around line 342 or 343, depending on your editing, you’ll find this:
    343
    
    add_action('dbx_post_sidebar', 'cm_list_select_moods');

    Either delete it or comment it out:

    343
    
    // add_action('dbx_post_sidebar', 'cm_list_select_moods');
  4. Finally, we need to add the code that ties the box in to the Write page:
    963
    964
    965
    
    add_action('admin_menu', 'cm_add_moods_box');
     
    ?>

    I added it right at the bottom of the file, just before the closing ?>, so I could find it, but it could probably have gone in place of the line deleted in the previous step.

That wasn’t too bad, was it?

Now save the changed file (keep a copy of the original in case it goes horribly wrong). Upload the new file to your WordPress plugins folder and activate it in the usual way. You should now find the Cricket Moods box lives in a more useful place in your Write page. If it doesn’t work, or it messes up the page, then either one of your edits is wrong, or I’ve made a typo. If you can get to the plugins page, deactivate it and the problem will go away. If you can’t get there, use FTP to delete the edited cricket-moods.php file and replace it with the original, which will work as it did before.

As with anything code-related, I have to emphasise that I have no idea what I’m doing and just fiddle with things until they either do what I want or I give up in despair, so I can’t really answer any questions about this kind of thing. All I can say is that this worked for me…

[1] The fiend!!
[2] It’s a habit

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*