问题描述

所以我在自定义帖子类型中的自定义字段出现问题。无论什么原因,字段保存,然后随机清除… 我确定这不是随机的,但我不知道是什么触发了这种情况发生。以下是我的自定义帖子类型的代码:

    // Custom Post Type: Strategic Giving
add_action('init', 'giving_register');

function giving_register() {

  $labels = array(
    'name' => _x('Invest Items', 'post type general name'),
    'singular_name' => _x('Item', 'post type singular name'),
    'add_new' => _x('Add New', 'giving item'),
    'add_new_item' => __('Add New Item'),
    'edit_item' => __('Edit Item'),
    'new_item' => __('New Item'),
    'view_item' => __('View Item'),
    'search_items' => __('Search Items'),
    '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() . '/images/cpt-giving.png',
    'rewrite' => array( 'slug' => 'giving_items' ),
    'capability_type' => 'post',
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array('title','thumbnail','editor'),
    'paged' => false,
    );

  register_post_type( 'givings' , $args );
}

register_post_type( 'givings' , $args );

add_action("admin_init", "giving_admin_init");

function giving_admin_init(){
  add_meta_box("giving_info-meta", "Item Options", "giving_info", "givings", "side", "high");
}

function giving_info(){
  global $post;
  $custom = get_post_custom($post->ID);
  $amount = $custom["amount"][0];
  $monthly = $custom["monthly"][0];
  $user_entered_value = $custom["user_entered_value"][0];
  $item_name = $custom["item_name"][0];
  $special = $custom["special"][0];
  ?>
  <div style="text-align: right;">
    <p>
      <label for="amount"><strong>Amount:</strong></label>
      <input style="width: 180px" type="text" name="amount" id="amount" value="<?php echo $amount; ?>" />
      <em>Example: 30.00</em>
    </p>
    <p>
      <label for="monthly"><strong>Monthly?</strong></label>
      <?php if ($monthly == 'on') { ?>
        <input type="checkbox" name="monthly" id="monthly" checked="checked" />
      <?php } else { ?>
        <input type="checkbox" name="monthly" id="monthly" />
      <?php } ?>
    </p>
    <p>
      <label for="special"><strong>Special Item?</strong></label>
      <?php if ($special == 'on') { ?>
        <input type="checkbox" name="special" id="special" checked="checked" />
      <?php } else { ?>
        <input type="checkbox" name="special" id="special" />
      <?php } ?>
    </p>
    <p>
      <label for="user_entered_value"><strong>Allow Giver To Enter Custom Value?</strong></label>
      <?php if ($user_entered_value == 'on') { ?>
        <input type="checkbox" name="user_entered_value" id="user_entered_value" checked="checked" />
      <?php } else { ?>
        <input type="checkbox" name="user_entered_value" id="user_entered_value" />
      <?php } ?>
    </p>
    <p>
      <label for="item_name"><strong>Item Name:</strong></label>
      <input style="width: 180px" type="text" name="item_name" id="item_name" value="<?php echo $item_name; ?>" /><br />
      If item is a <em>per item</em> then enter the name of the singular item. <em>Example: Chair - which will be displayed as "30.00 per Chair"</em>
    </p>
    <p style="text-align: left;">
      Strategic Giving photo must be horizontal and uploaded/set as the <strong>Featured Image</strong> (see below).
      <em>Do not add photo to content area.</em>
    </p>
  </div>
  <?php }

add_action('save_post', 'giving_save_details_amount');
add_action('save_post', 'giving_save_details_monthly');
add_action('save_post', 'giving_save_details_user_entered_value');
add_action('save_post', 'giving_save_details_item_name');
add_action('save_post', 'giving_save_details_special');

function giving_save_details_amount(){
  global $post;
  update_post_meta($post->ID, "amount", $_POST["amount"]);
}

function giving_save_details_monthly(){
  global $post;
  update_post_meta($post->ID, "monthly", $_POST["monthly"]);
}

function giving_save_details_user_entered_value(){
  global $post;
  update_post_meta($post->ID, "user_entered_value", $_POST["user_entered_value"]);
}

function giving_save_details_item_name(){
  global $post;
  update_post_meta($post->ID, "item_name", $_POST["item_name"]);
}

function giving_save_details_special(){
  global $post;
  update_post_meta($post->ID, "special", $_POST["special"]);
}

add_action("manage_pages_custom_column",  "givings_custom_columns");
add_filter("manage_edit-givings_columns", "givings_edit_columns");

function givings_edit_columns($columns){
  $columns = array(
    "cb" => "<input type="checkbox" />",
    "title" => "Strategic Giving Item",
    "amount" => "Amount",
    "monthly" => "Monthly",
    "special" => "Special Item",
    "giving_image" => "Image"
    );

  return $columns;
}

function givings_custom_columns($column){
  global $post;

  switch ($column) {
    case "amount":
    $custom = get_post_custom();
    echo $custom["amount"][0];
    break;

    case "monthly":
    $custom = get_post_custom();
    $is_monthly = $custom["monthly"][0];
    if ($is_monthly == "on") {
      echo "Yes";
    };
    break;

    case "special":
    $custom = get_post_custom();
    $is_special = $custom["special"][0];
    if ($is_special == "on") {
      echo "Yes";
    };
    break;

    case "giving_image":
      echo get_the_post_thumbnail(NULL, 'staff_admin');
    break;
  }
}

function giving_amount(){
  $custom = get_post_custom();
  return $custom["amount"][0];
}

function giving_monthly(){
  $custom = get_post_custom();
  return $custom["monthly"][0];
}

function giving_special(){
  $custom = get_post_custom();
  return $custom["special"][0];
}

function giving_user_entered_value(){
  $custom = get_post_custom();
  return $custom["user_entered_value"][0];
}

function giving_item_name(){
  $custom = get_post_custom();
  return $custom["item_name"][0];
}

更新:所以我做了更多的研究,弄清楚了。自动保存 (又名修订版) – Post metadata deletes itself

是否可以关闭自动保存只是一个单一的邮政类型,而不是全球?

最佳解决方案

这是一个容易的:)

add_action( 'admin_enqueue_scripts', 'my_admin_enqueue_scripts' );
function my_admin_enqueue_scripts() {
    if ( 'your_post_type' == get_post_type() )
        wp_dequeue_script( 'autosave' );
}

次佳解决方案

显然,de-registering 自动保存 JavaScript 将基本上停止自动保存程序运行。它不一定会禁用自动保存在该类型上发生的功能,但会停止本机自动保存脚本的运行。

这不是一个完美的解决方案,但它应该有所期望的效果。

function wpse5584_kill_autosave_on_postype( $src, $handle ) {
    global $typenow;
    if( 'autosave' != $handle || $typenow != 'your-post-type-here' )
        return $src;
    return '';
}
add_filter( 'script_loader_src', 'wpse5584_kill_autosave_on_postype', 10, 2 );

希望有帮助…

编辑:使用上面的代码,当在该类型的帖子创建屏幕上,您应该在页面的源中看到以下内容。

<script type='text/javascript'>
/* <![CDATA[ */
var autosaveL10n = {
    autosaveInterval: "60",
    previewPageText: "Preview this Page",
    previewPostText: "Preview this Post",
    requestFile: "http://yoursite/wp-admin/admin-ajax.php",
    savingText: "Saving Draft…",
    saveAlert: "The changes you made will be lost if you navigate away from this page."
};
try{convertEntities(autosaveL10n);}catch(e){};
/* ]]> */
</script>
<script type='text/javascript' src=''></script>

变量不是我们在这里看的,脚本在底部,注意到 src 现在无处不在 (这最初是指向 autosave.js 文件) 。

你看到类似于上面的内容,还是 src 仍然被写入 autosave.js 文件的路径?

EDIT2:这是我看到的连接脚本。

<script type='text/javascript' src='http://example/wp-admin/load-scripts.php?c=0&amp;load=hoverIntent,common,jquery-color,schedule,wp-ajax-response,suggest,wp-lists,jquery-ui-core,jquery-ui-sortable,postbox,post,word-count,thickbox,media-upload&amp;ver=e1039729e12ab87705c047de01b94e73'></script>

注意,自动保存脚本被排除在静止..(所以到目前为止我无法重现你的问题)..

你在哪里放置我提供的代码?

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。