1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14.

Thursday, April 14, 2011

Auto save problem for custom variables

Readers while developing with wordpress i found a problem with the post . I had custom variable into my metabox.

So there is a default hook to be called to save that custom variable into the database.


add_action('save_post','function_save_var');


So by default when u click publish post or update post then 'function_save_var' would be called .
So when u update the post and then close the page then this would work perfectly fine but then i din know
auto save would call this hook too .

So when my page would be open for a while all my values would reset and my functionality would fail. So
after a little search i got the solution.


function function_save_var()
{
// Function without avoiding auto save.
}


The above function would fail as it would reset everything .

but then to avoid; you need to add the following code in the start of the function


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


if the post will autosave this would directly return n do not update anything . Thus this is all solved.

No comments:

Post a Comment