Make WordPress Core

Opened 17 years ago

Closed 17 years ago

Last modified 11 years ago

#10744 closed defect (bug) (worksforme)

autosaves deletes content for hidden meta_fields on custom widgets/plugins

Reported by: hertzel Owned by: azaozz
Priority: normal Milestone:
Component: Autosave Version: 2.8.4
Severity: major Keywords: reporter-feedback
Cc: Focuses:

Description

Steps to reproduce.

  1. create a new hidden meta field

add_post_meta($post_id, "_hidden", "under the table", true) or update_post_meta($post_id, $idx_item_name, $name);

  1. Create a regular meta field

add_post_meta($post_id, "Survive", "HI Im alive",true) or update_post_meta($post_di, "Survive", "HI Im alive");

using hook save_post
they save and be retrieved just fine but after the first call to autosave, _hidden will be be saved with "" instead of the value passed to the hook function

Change History (5)

#1 @leogermani
17 years ago

I am not sure if this can be considered a bug. Auto save only save the core information of a post, so if your plugin inadvertly does something like:

update_option('my_option' $_POST['my_field'];

you will erase your option in every auto-save becase $_POSTmy_field will be empty.

What plugin writers must do is add a hidden field to their forms and check it before updating the option

if ($_POST['my_hidden_field']) update_option('my_option' $_POST['my_field'];

And why checking upon a hidden field and not the field itself? because you may want to clear the information on your field, and if you check upon it, you would never be able to set it to empty again.

Conclusion: In my opinion this is not a bug and the documentation for add_meta_box should include the procedure above (I can do that if we agree)

#2 @scribu
17 years ago

  • Keywords reporter-feedback added
  • Milestone 2.8.52.9
  • Priority highest omg bbqnormal
  • Version2.8.4

#3 @azaozz
17 years ago

  • Resolutionworksforme
  • Status newclosed

Leogermani is right, also the first thing autosave does is define( 'DOING_AUTOSAVE', true ); so plugins can check whether that constant is defined and act accordingly.

#4 @leogermani
17 years ago

I have updated the example in the codex with a check for DOING_AUTOSAVE constant

http://codex.wordpress.org/Function_Reference/add_meta_box#Example

Leo

#5 @DrewAPicture
11 years ago

  • Milestone 2.9
Note: See TracTickets for help on using tickets.