Nov 25

Fixing Postie for WordPress 2.3 Categories

Category: Entropy, Tech, WordPressstairway27 @ 7:42 pm

If you’ve tried using the Postie plug-in (version 1.1.1, anyway) with WordPress 2.3, you’ve probably seen that the feature related to Categories do not work. The database schema changed for WordPress2.3, and Postie was reading categories directly from the tables with SQL calls.

Thanks to efforts of a few others, who nearly got it right, I managed to cobble together a fix that allows emailed posts to be categorized and add a default category to emailed posts that do not include category details. Credit/kudos to Olivier and Greg and Benjamin for comments and work referenced on the Postie page.

I don’t mean to detract from the work these gentlemen did before I stumbled across the same problem. I tried implementing their corrections, but had a different problem that nobody else mentioned: only a fraction of my established categories appeared in the list on the config page. It had similar problems locating Categories specified in the email subject. The solution was to provide an additional parameter in the call to get_categories(). For efficiency, I also pulled the call to create a local list of categories out of a foreach loop, so that the same work wasn’t being done multiple times, in the case of an email specifying multiple categories. (Always optimyze…)

Instructions

Follow the instructions for installing Postie. Then, either download and install the two files from this zip archive, or edit the two files as shown below.

Edit config_form.php from the postie plugin directory. Search for “DEFAULT_POST_CATEGORY” and replace the line with the SELECT query with the new code shown below:

<td><select name="DEFAULT_POST_CATEGORY" id="DEFAULT_POST_CATEGORY">
        <?php
        //$categories = $wpdb->get_results(”SELECT * FROM $wpdb->categories ORDER BY cat_name”);
        $categories = get_categories(array(’orderby’ =>’name’, ‘get’ =>’all’));
        foreach ($categories as $category) {
            $selected = ($category->cat_ID == $config[”DEFAULT_POST_CATEGORY”] ? “SELECTED”: NULL);
            echo “\n\t<option value=’$category->cat_ID’ $selected>$category->cat_name</option>”;
        }
        ?>
</select></td>

Finally, edit postie-functions.php,

function GetPostCategories(&$subject) {
    $config = GetConfig();
    global $wpdb;
    $post_categories = array();
    $matches = array();
    //try and determine category
    if ( preg_match('/(.+): (.*)/', $subject, $matches))  {
        $subject = trim($matches[2]);
        $matches[1] = array($matches[1]);
    }
    else if (preg_match_all('/\[(.[^\[]*)\]/', $subject, $matches)) {
        preg_match("/](.[^\[]*)$/",$subject,$subject_matches);
        $subject = trim($subject_matches[1]);
    }
    else if ( preg_match_all('/-(.[^-]*)-/', $subject, $matches) ) {
        preg_match("/-(.[^-]*)$/",$subject,$subject_matches);
        $subject = trim($subject_matches[1]);
    }
    $categories = get_categories(array(’orderby’ =>’name’, ‘get’ =>’all’));
    if (count($matches)) {
        foreach($matches[1] as $match) {
            $match = trim($match);
            $category = NULL;
            print(”Working on $match\n”);
            //Work on the category search to see if we can determine the cat_id
            //check the database to see if their is a category similar
            foreach ($categories as $category) {
              if ($match == $category->cat_name) {
                $post_categories[] = $category->cat_ID;
                print(”   Match: $category->cat_name\n”);
                continue;
              }
              if ($match == $category->cat_ID) {
                $post_categories[] = $category->cat_ID;
                print(”   Match: $category->cat_name\n”);
                continue;
              }
              if (strpos($category->cat_name,$match) !== false) {
                $post_categories[] = $category->cat_ID;
                print(”   Match: $category->cat_name\n”);
              }
            }
        }
    }
    if (!count($post_categories)) {
          $post_categories[] =  $config[”DEFAULT_POST_CATEGORY”];
    }
    print(”Posting categories: “);
    print_r($post_categories);
    print(”\n”);
    return($post_categories);
}

28 Responses to “Fixing Postie for WordPress 2.3 Categories”

  1. Kydd says:

    Nice job! Works perfectly and apparently fixed the problem where the Quick ReadMe link was breaking the top navigation!

  2. Jimmy says:

    Hey thanks for this little haxie! Really appreciate it!

  3. mR. gImO says:

    Thanks!!!

  4. Johannes Brenner says:

    Thanks for the fix.

  5. John says:

    Works like a charm. You’re definitely a life saver. Thanks a bunch!

  6. Mark says:

    Happy with this, nice clean well thought out solution. Well done man!

  7. jj says:

    can someone help me here …this works but now my default category in postie is not working …it keeps sending messages without a category specified in the subject to uncategorized category?

  8. Mike says:

    Great Work !! !! !! I was begining to think I had setup the program wrong.. .. ..

  9. shioiri says:

    Wow! Thanks a lot!!

  10. Al Stevens says:

    Worked great for me as well. Thanks much! Watch for the single and double quotes when you cut and paste the code — replace the double and single primes with plain old quotation marks.

  11. Raphael says:

    Thank you!!! great job!!!

  12. Eddie says:

    Great Work !! Your hack solve many of my problems.
    But… I have one more question:
    - Is posssible to set de “from” address as the post owner?
    Because I have an intranet site with a lot of users and the posts aways point “admin” as the sender.
    Thanks for all.

  13. Terry says:

    Thanks so much! This works great. Fixes often don’t fix it, but yours is perfect. You Rule!

  14. jj says:

    PLEASE ANYONE ….. i don’t want to train my clients to use a category name followed by a semi colon unless they have to

    but i want to use POSTIE …. IT WILL NOT POST TO THE DEFAULT CATEGORY if you do not specify one in the subject of the email

    instead it creates its own category called uncategorized or something like that

    anybody know how to fix this? in the postie options you can specify a default category so its supposed to work

    but what i believe happened is this fix broke that feature yes?

    thank you for your help …postie is the best!

  15. Adri says:

    Perfect! This works perfectly! Thanks a lot!

  16. Chris says:

    Brilliant! Thank mate!

  17. Jorge says:

    :( nothing for me, keeps ignoring category tags

  18. Larry says:

    Thanks for this great bit of code! Helped ease my post-by-email headache considerably! :)

  19. lukmanf says:

    good job man! many thanks

  20. Sean D says:

    This fixed worked great, thanks for the help.

  21. jj says:

    hello …..can anyone help me fix this problem with posting images using postie? its removing the javascript popup code from images and just doing a direct link to the image so you click and it doesn’t even open a new browser window just goes to the image

    i have tried and this happens on 2.3 and 2.5 and doesn’t matter if its an email you put on the list of approved senders/posters or its from an account registered with wordpress …still strips the javascript popup code and here’s whats really strange SOMETIMES ..i got it to work once and have no idea why?

    thanks!

  22. Brazilian Don says:

    Thanks for your work. Fixed my problem and I can now post from my BlackBerry.

  23. Rytis says:

    Thanks for the fix!

    Anybody with some php knowledge to help me?

    In Postie I have enabled “Allow Anyone To Post Via Email”, need Email sender (the From: field in Email, like John Doe) to appear:

    1. as Post Author in blog Post instead of Admin
    2. be added at the end of Post Title separated by “-” or other separator like this: “Post Title From Email Subject - Email Author”

    thanks

  24. lukmanf says:

    how to create a new categories by email subject? (using [])
    is anyone ever do this?

    thx

  25. Irish says:

    Perfect fix, thanks a TON! Saved me from many hours of frustration!

  26. Patrick says:

    Great fix! Thanks for saving me the time of fixing it myself!

  27. woodstock says:

    Thanks Stairway27! That truely saved my blog!

  28. Daniel says:

    I read similar article also named Fixing Postie for WordPress 2.3 Categories, and it was completely different. Personally, I agree with you more, because this article makes a little bit more sense for me

Leave a Reply