WordPress Custom Fields – Lost by Autosave??

Issue:

I have added some custom fields to the add_meta_box and hooked a function to edit_post, publish_post, save_post. Everything is working fine, but unfortunately the autosave destroy all the efforts. I didn’t find any hook to control autosave.

Solution:

I just added a if condition to my hook function. Many of them suggest to increase the autosave time to months/years etc. but I prefer this

This is the condition :) a simple hack.

if ($_REQUEST['action'] == 'autosave') return;

So final script looks like below

PHP Script:

add_action( 'save_post', 'save_request_data' );
add_action( 'edit_post', 'save_request_data' );

function save_request_data() {
	global $post;
        // Will return if it an Ajax Autosave action. We are safe now
	if ($_REQUEST['action'] == 'autosave') return;

	// ALL YOUR CODES COMES HERE...
}

Hope this will solve your problem with autosave. Thank you ;)


Share Your Thoughts

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="">