Wp Autosave clears post meta fields

August 1, 2012 | | Blog |

I came across this issue recently where the post meta field is cleared when a auto save draft or ajax request is made. There is a workaround for this under the function which is linked to action ‘save_post’ we need to add few lines of code

Add this lines inside the function before any action is performed

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;

if (defined('DOING_AJAX') && DOING_AJAX)
return;

So example function


add_action('save_post', 'save_example_form');
function save_example_form($postID){

if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;

if (defined('DOING_AJAX') && DOING_AJAX)
return;
update_post_meta($postID,'example_field',$_POST['example']);

}