Home > Uncategorized > Auto save problem for custom variables

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.

About these ads
  1. April 30, 2012 at 10:37 am | #1

    Thank you very much. You saved my lot of time.
    Cheers !

    • May 2, 2012 at 7:24 am | #2

      Welcome, Glad your time was saved :D

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: