我正在使用带有自定义元字段的自定义帖子类型,但这些自定义元字段似乎不会触发自动保存和“未保存的更改”对话框。对我来说,自动保存不如“未保存的更改”对话框重要-有没有办法触发它?
function add_meta_boxes() {
add_meta_box(\'places_location\', __(\'Location\'), array(&$this, \'location_box\'), \'place\', \'normal\', \'high\');
}
function location_box($post) {
wp_nonce_field(plugin_basename(__FILE__), \'places_location_nonce\');
$lat = get_post_meta($post->ID, \'places_lat\', true);
$lng = get_post_meta($post->ID, \'places_lng\', true);
?>
<p>
<label>
Latitude:
<input name="places_lat" value="<?php echo esc_attr($lat); ?>" />
</label>
<label>
Longitude:
<input name="places_lng" value="<?php echo esc_attr($lng); ?>" />
</label>
</p>
<?php
}
function save_place($id) {
// skip unverified location nonces
if(!wp_verify_nonce($_POST[\'places_location_nonce\'], plugin_basename(__FILE__))) return;
// skip autosave calls
// commenting this out still doesn\'t trigger saving these fields on autosave
//if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) return;
// update our custom post meta
update_post_meta($id, \'places_lat\', (float)$_POST[\'places_lat\']);
update_post_meta($id, \'places_lng\', (float)$_POST[\'places_lng\']);
}
最合适的回答,由SO网友:Jan Fabry 整理而成
代码确实only looks at the TinyMCE editor 或隐藏编辑器时的标题和内容字段:
window.onbeforeunload = function(){
var mce = typeof(tinyMCE) != \'undefined\' ? tinyMCE.activeEditor : false, title, content;
if ( mce && !mce.isHidden() ) {
if ( mce.isDirty() )
return autosaveL10n.saveAlert;
} else {
title = $(\'#post #title\').val(), content = $(\'#post #content\').val();
if ( ( title || content ) && title + content != autosaveLast )
return autosaveL10n.saveAlert;
}
};
你可以替换它
onbeforeunload
你自己的处理程序(当然要包括现有的功能)。或者玩
isDirty()
TinyMCE编辑器的状态
autosaveLast
价值