Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to create a custom post type and I've had the same problem that's described here; my custom meta information was properly saved, when saving manually, but got lost as soon as the autosave ajax ran at least once.

So I now use the shown solution to fix this:

function save_stationinfo($post_id) {
    if((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) || (defined('DOING_AJAX') && DOING_AJAX) || isset($_REQUEST['bulk_edit'])) return;
    update_post_meta($post_id, 'station_url', $_POST['station_url']);
    update_post_meta($post_id, 'station_subheadline', $_POST['station_subheadline']);
    update_post_meta($post_id, 'station_streams', $_POST['station_streams']);
}

But this disables the autosave functionality for all custom metadata. What I actually want is to make my metabox fully compatibly with the autosave, ajax (not sure what the DOING_AJAX mode is for, though) and bulk-edit/quick-edit functionality of Wordpress, so that custom meta fields get automatically saved and I am able to add some of the fields to the quick-/bulk-edit dialog.

Can anyone please help me here, or show me where I can find a tutorial for this? (Removing the if-statement doesn't help here, as it leads me back to my first problem, of course.) Thanks in advance!

share|improve this question

1 Answer

WordPress does not send the content of custom fields during autosave (just title, slug and content). That’s why the custom field content will be deleted if you try to save the data: You cannot see the difference between deleted and missing content.

I would create a separate autosave function for that, because the way WordPress handles it can change any time, and there is no real API. The saved fields are hard coded without any filter:

if ( fullscreen && fullscreen.settings.visible ) {
    post_data["post_title"] = jQuery('#wp-fullscreen-title').val() || '';
    post_data["content"] = jQuery("#wp_mce_fullscreen").val() || '';
} else {
    post_data["post_title"] = jQuery("#title").val() || '';
    post_data["content"] = jQuery("#content").val() || '';
}

if ( jQuery('#post_name').val() )
    post_data["post_name"] = jQuery('#post_name').val();

Look at wp-admin/includes/ajax-actions.php and wp-includes/js/autosave.js to see how it works.

So basically create a copy of that JavaScript file, remove anthing you can reuse and change just the fields you want to save automatically. Then enqueue it with 'autosave' in its dependency list.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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