开发者

Autosave and Custom Fields in WordPress [closed]

开发者 https://www.devze.com 2023-02-06 22:59 出处:网络
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time,or an extraordinarily narrow situation that is not generally applic
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 10 years ago.

I was having the problem with WordPress autosave not sending custom field data, and found this entry on SO:

Wordpress add_meta_box() weirdness

The approved answer works, but is it not just preventing any autosave from happening by returning early? Isn't the same as disabling autosave completely? If so, wouldn't it be better to do so in the proper way instead of letting it try to autosave just to prevent it?

The only exception I could see is if one checked for a pos开发者_高级运维t-type before checking for autosave so that you only disabled autosave for certain post types.

I'm going to be disabling autosave completely, but was wondering what you guys thought.

EDIT: I'm not having problems with the autosave anymore. This question is to discuss the merits of the solutions that I mentioned above.

Additionally, I can't see how this would be a feature of WordPress, and would assume that autosave should be changed to include all post data. No?


The code in the linked answer does not disable autosave, it only stops a custom save function executing when autosaving. This is necessary because the Wordpress autosave system does not support post meta data (custom fields), either in the Javascript which collates the post data or in the PHP which creates and restores revisions.


I've used the following for Custom Fields I've created and it's worked fine.

<?php
// Save Fields
add_action('save_post', 'save_details');

function save_details(){
 if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
    return $post_id;
 global $post;
 update_post_meta($post->ID, "event_featuring", $_POST["event_featuring"]);
 update_post_meta($post->ID, "event_time", $_POST["event_time"]);
 update_post_meta($post->ID, "event_date", $_POST["event_date"]);
 update_post_meta($post->ID, "event_end_date", $_POST["event_end_date"]);
 update_post_meta($post->ID, "event_location", $_POST["event_location"]);
 update_post_meta($post->ID, "bhs_event", $_POST["bhs_event"]);
}
?>


FYI, the solution posted here http://wordpress.org/support/topic/custom-post-type-information-disappearing worked for me and I think is much more elegant.

0

精彩评论

暂无评论...
验证码 换一张
取 消