可以关闭单个自定义POST类型的自动保存

时间:2010-12-18 作者:Marc

因此,我的自定义帖子类型中的自定义字段有问题。无论出于何种原因,字段都会随机保存并清除。。。我肯定这不是随机的,但我不确定是什么触发了这种情况的发生。以下是我自定义帖子类型的代码:

    // 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];
}
Update: 所以我做了更多的研究并找到了答案。自动保存(又名修订版)-Post metadata deletes itself

是否可以仅为单个帖子类型而非全局关闭autosave?

2 个回复
SO网友:kovshenin

这很简单:)

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\' );
}

SO网友:t31os

显然,取消注册autosave javascript实际上会停止autosave例程的运行。它不一定会禁用在该post类型上进行自动保存的功能,但会停止本机自动保存脚本的运行。

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

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 );
希望这有助于。。。

EDIT: 使用上述代码,当在该类型的后期创建屏幕上时,您应该在页面的源代码中看到以下内容。。

<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&#8230;",
    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是否仍在使用自动保存的路径编写。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>
请注意,自动保存脚本仍被排除。。(到目前为止,我无法重现您的问题)。。

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

结束

相关推荐