WordPress autosave and custom fields

Categories Wordpress
Wordpress coding article

If you find that the information stored in your custom fields is randomly disappearing on you then it is most likely the autosave feature thats at fault.

The most likely scenario is that you’ve created some custom fields, tested that you’re custom save function is firing when you hit Save Draft/Publish/Update, and all seems fine.  But then you notice that there are situations where WordPress is clearing your custom field values.

Basically, when you hit Save Draft/Publish/Update, all form fields are posted to the registered save routines.  For whatever reason the WordPress autosave does not post your custom field data, so when your save function kicks in the field data is empty and your routine will save the empty values to the database…  oops.

A simple solution is to add the following check to the beginning of the custom save function you defined for your custom fields:

global $post;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
    return $post->ID;
}

When an auto-update occurs it will fire the WordPress save function, and then the custom function you have defined and registered.  This code will stop your save function from clearing your stored data.

I say this is a simple solution because really you should be doing proper data validation on the form field data being posted to your save function.  However I recognise that there are times when either you may wish to allow empty strings, or just aren’t bothered about your custom fields getting saved during an auto-save.

11 Comments

  • Webdesign Erfurt
    December 12, 2010

    Seems to work, thx mate!

  • Geert
    December 29, 2010

    Does this code stop autosave from working all together? It looks like that to me. Not sure what is going on exactly.

    • Andrew Gail
      February 6, 2011

      @Geert: It probably would kill the autosave altogether, but it’s meant to be included in your custom save function. That way only your custom code will be killed and the built-in WordPress save routine will still run as expected.

      I’ve tweaked the article to better explain the situation. I hope.

  • ign
    April 2, 2011

    Thanks, you saved me some time debugging. Works great.

  • Mark
    April 22, 2011

    Thanks a lot for sharing this. I’ve been scratching my head over this for nearly 2 days now.

  • Me
    May 29, 2011

    Thanks for this man, you saved my sanity! Thanks a lot!

  • Rean
    July 20, 2011

    Thank you, I was almost pulling my hairs out on this one.

  • james
    February 29, 2012

    this issue is killing me, and after reading this, i can’t for the life of me figure out where to add those lines of code. Help please.

  • james
    February 29, 2012

    Got it. Figured it out. The code goes in the functions.php

    and would look something like this….this is from my site.

    function save_dastuff(){
    if ( defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE )
    return $post_id;
    global $post;
    update_post_meta($post->ID, “wpcf-vimeo-code”, $_POST[“vimmmmmmmeo”]);
    update_post_meta($post->ID, “wpcf-youtube-code”, $_POST[“yttttbbbe”]);
    update_post_meta($post->ID, “wpcf-quick-headline”, $_POST[“heeadddlinn”]);
    update_post_meta($post->ID, “wpcf-videographer”, $_POST[“videoggrappher”]);
    update_post_meta($post->ID, “wpcf-not-so-wide”, $_POST[“nottssoowidde”]);
    }

    hope that helps

  • Gavin
    May 3, 2012

    Thanks! You saved the day 🙂

  • Greg
    June 8, 2012

    Thanks, this one had me pulling my hair out!

Leave a Reply

Your email address will not be published. Required fields are marked *