自定义字段在自动保存后被擦除

时间:2012-01-05 作者:zac

为什么此帖子类型中的自定义字段不保存?

我有一个称为照片的自定义帖子类型和几个元框。这是我的函数中的基本内容。php

add_action(\'init\', \'photo_register\');
 
function photo_register() {
 
    $labels = array(
        \'name\' => _x(\'My Photos\', \'post type general name\'),
        \'singular_name\' => _x(\'Photo\', \'post type singular name\'),
        \'add_new\' => _x(\'Add New\', \'photo item\'),
        \'add_new_item\' => __(\'Add New Photo\'),
        \'edit_item\' => __(\'Edit Photo\'),
        \'new_item\' => __(\'New Photo\'),
        \'view_item\' => __(\'View Photo\'),
        \'search_items\' => __(\'Search Photos\'),
        \'not_found\' =>  __(\'Nothing found\'),
        \'not_found_in_trash\' => __(\'Nothing found in Trash\'),
        \'parent_item_colon\' => \'\'
    );
    
    $args = array(
        \'labels\' => $labels,
        \'public\' => true,
        \'publicly_queryable\' => true,
        \'show_ui\' => true,
        \'query_var\' => true,
        //\'menu_icon\' => get_stylesheet_directory_uri() . \'/article16.png\',
        \'rewrite\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'menu_position\' => 4,
        \'taxonomies\' => array(\'post_tag\',\'category\'),
        \'supports\' => array(\'title\',\'comments\',\'trackbacks\',\'revisions\',\'custom-fields\',\'page-attributes\',\'thumbnail\', \'excerpt\', \'tags\')
      ); 
 
    register_post_type( \'photo\' , $args );
    register_taxonomy("Photos", array("photo"), array("hierarchical" => true, "label" => "Photos", "singular_label" => "Photo", "rewrite" => true));

    
}

add_action("admin_init", "admin_init");
add_action(\'save_post\', \'save_details\');
 
function admin_init(){ // add_meta_box( $id, $title, $callback, $page, $context, $priority ); 
  add_meta_box("media", "Media Type", "media", "photo", "side", "high");
  add_meta_box("map_meta", "Mapping Info", "map_meta", "photo", "normal", "high");
  add_meta_box("photo_meta", "Add a photograph", "photo_meta", "photo", "normal", "high");
}
 
function media(){
  global $post;
  $custom = get_post_custom($post->ID);
  $film = $custom["film"][0];
  $camera = $custom["camera"][0];
 
  ?>
  <label>Camera:</label>
  <input name="camera" value="<?php echo $camera; ?>" />
  
    <label>Film:</label>
  <input name="film" value="<?php echo $film; ?>" />
  
  <?php
}

function photo_meta(){
  global $post;
  $custom = get_post_custom($post->ID);
  $single_photo = $custom["single_photo"][0];
  ?>
  
  <div id="singleUpload">

    <div class="sUinput">
        <input id="single_photo" name="single_photo" value="<?php echo $single_photo; ?>" />
    </div>
    <div class="sUbutton">
    <input type="button" value="Upload" name="upload" id="upload_image_button" />
    </div>
  </div>
  
  <?php
}


function map_meta() {
  global $post;
  $custom = get_post_custom($post->ID);
  $latitude = $custom["latitude"][0];
  $longitude = $custom["longitude"][0];

  ?>
<div id="mapControls">

<div class="lat">
  <p><label for="lat">Latitude:</label><br />
  <input id="latitude"  name="latitude" value="<?php echo $latitude; ?>"></input></p>
 </div>
 <div class="lng">
  <p><label>Longitude:</label><br />
  <input id="longitude" name="longitude" value="<?php echo $longitude; ?>" ></input></p>
 </div>

   <?php }

  function save_details(){
  global $post;
  update_post_meta($post->ID, "single_photo", $_POST["single_photo"]);
  update_post_meta($post->ID, "latitude", $_POST["latitude"]);
  update_post_meta($post->ID, "longitude", $_POST["longitude"]);
  update_post_meta($post->ID, "camera", $_POST["camera"]);
  update_post_meta($post->ID, "film", $_POST["film"]);
 
}
我让它工作了一段时间,但现在它不会保存字段,刷新会擦除所有内容。有人知道问题出在哪里吗?有没有关于如何找到bug的建议?基本上,你能想到的任何改进都将不胜感激,尤其是如果我能让它发挥作用的话!

编辑我发现它们确实可以保存,但如果我在帖子上停留的时间足以运行自动保存,然后尝试导航离开,我会收到消息:

页面要求您确认是否要离开-您输入的数据可能不会保存。

我单击“离开”,然后当我返回帖子时,所有内容都被删除。这是怎么回事?

3 个回复
最合适的回答,由SO网友:Geert 整理而成

我使用以下代码来防止在自动保存、ajax请求(快速编辑)和批量编辑期间更新自定义字段。

add_action(\'save_post\', \'save_my_post\');
function save_my_post($post_id)
{
    // Stop WP from clearing custom fields on autosave,
    // and also during ajax requests (e.g. quick edit) and bulk edits.
    if ((defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) || (defined(\'DOING_AJAX\') && DOING_AJAX) || isset($_REQUEST[\'bulk_edit\']))
        return;

    // Clean, validate and save custom fields
}
另请参见:How to prevent custom fields from being cleared during a bulk edit?

SO网友:Velvet Blues

您需要忽略所有自动保存请求。按如下方式修改保存功能:

function save_details(){
   if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;

   global $post;
   update_post_meta($post->ID, "single_photo", $_POST["single_photo"]);
   update_post_meta($post->ID, "latitude", $_POST["latitude"]);
   update_post_meta($post->ID, "longitude", $_POST["longitude"]);
   update_post_meta($post->ID, "camera", $_POST["camera"]);
   update_post_meta($post->ID, "film", $_POST["film"]);
}
此外,您肯定应该再添加一点验证,以便该函数不能在预期用途以外的情况下调用。我通常验证帖子类型、用户权限和nonce。

SO网友:Brian Fegter

您需要隔离save\\u post操作。您也没有将post\\u id作为参数传递给函数。

function save_details($post_id){
    $post =& $_REQUEST;
    if($post[\'post_type\'] == \'post\'){
        update_post_meta($post_id, "single_photo", $_POST["single_photo"]);
    }
}

结束

相关推荐

从Metabox保存数据不起作用

我已经使用了添加metabox的教程。但我的数据不会保存在框中。我在编辑链接页面中有元框,但当我将任何数据放入框中并按下更新按钮时,它不会保存数据。那么实际上,我想知道我什么时候把数据放进框中,我可以使用$\\u POST[]获取数据吗?如果是,请帮助我,使我的代码部分出错。。// backwards compatible add_action( \'admin_init\', \'blc_add_custom_link_box\', 1 ); /*