WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#10744 closed defect (bug) (worksforme)

autosaves deletes content for hidden meta_fields on custom widgets/plugins

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

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 (4)

comment:1 leogermani5 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)

comment:2 scribu5 years ago

  • Keywords reporter-feedback added
  • Milestone changed from 2.8.5 to 2.9
  • Priority changed from highest omg bbq to normal
  • Version set to 2.8.4

comment:3 azaozz5 years ago

  • Resolution set to worksforme
  • Status changed from new to closed

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.

comment:4 leogermani5 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

Note: See TracTickets for help on using tickets.