Take the 2-minute tour ×
WordPress Development Stack Exchange 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 compatible 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
add comment

2 Answers

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
add comment

Another idea is to check my answer here with a link to my Custom Bulk/Quick Edit WordPress plugin and the complete Gist code for a custom post type bulk and quick edit scenario for 3-custom fields.

The gist deals with the DOING_AUTOSAVE, but ignores a direct DOING_AJAX because we want it to work anyways.

share|improve this answer
    
You've got to click two links to get to the real answer-- one link here and another link in the question that link refers to. That is really not ideal. –  s_ha_dum Sep 30 '13 at 13:52
add comment

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.