I'm using a custom post type with custom meta fields, but autosave and the "unsaved changes" dialog don't seem to be triggered for these custom meta fields. Autosave isn't as important to me as the unsaved changes dialog - is there a way to trigger it?

function add_meta_boxes() {
    add_meta_box('places_location', __('Location'), array(&$this, 'location_box'), 'place', 'normal', 'high');

function location_box($post) {
    wp_nonce_field(plugin_basename(__FILE__), 'places_location_nonce');

    $lat = get_post_meta($post->ID, 'places_lat', true);
    $lng = get_post_meta($post->ID, 'places_lng', true);

            <input name="places_lat" value="<?php echo esc_attr($lat); ?>" />
            <input name="places_lng" value="<?php echo esc_attr($lng); ?>" />

function save_place($id) {
    // skip unverified location nonces
    if(!wp_verify_nonce($_POST['places_location_nonce'], plugin_basename(__FILE__))) return;

    // skip autosave calls
    // commenting this out still doesn't trigger saving these fields on autosave
    //if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    // update our custom post meta
    update_post_meta($id, 'places_lat', (float)$_POST['places_lat']);
    update_post_meta($id, 'places_lng', (float)$_POST['places_lng']);
  • show us you metabox code – Bainternet Apr 6 '11 at 23:05
  • Didn't think it was all that necessary as the code is incredibly straightforward, but I added it anyway. Saving drafts and updating published posts works just fine, but changes to the custom fields does not trigger an "unsaved changes" dialog nor an autosave. – Kevin Apr 7 '11 at 2:04

The code indeed only looks at the TinyMCE editor or the title and content field if the editor is hidden:

window.onbeforeunload = function(){
    var mce = typeof(tinyMCE) != 'undefined' ? tinyMCE.activeEditor : false, title, content;

    if ( mce && !mce.isHidden() ) {
        if ( mce.isDirty() )
            return autosaveL10n.saveAlert;
    } else {
        title = $('#post #title').val(), content = $('#post #content').val();
        if ( ( title || content ) && title + content != autosaveLast )
            return autosaveL10n.saveAlert;

You could replace that onbeforeunload handler with your own (be sure to include the existing functionality of course). Or play with the isDirty() state of the TinyMCE editor and that autosaveLast value?

  • Any ideas why autosave isn't updating my custom post meta? – Kevin Apr 7 '11 at 11:51
  • @Kevin: If you read the autosave function in that Javascript file, you will notice that it never looks at the meta fields. So you will also have to duplicate that function and add meta field data yourself. This is the reason why all those save_post handlers for metadata return early if DOING_AUTOSAVE is set. – Jan Fabry Apr 7 '11 at 12:55

I solved this problem using this excellent jQuery plugin that checks the dirty state of a form. It contains methods to checks added/removed/modified fields via javascript. It totally kicks ass!

I did not handled the "AUTOSAVE" (yet). I hope someone finds this useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.