删除自定义帖子类型时删除的自定义字段值

时间:2011-10-25 作者:Nero_DCLXVI

我已经创建了一个带有2个自定义字段的自定义帖子类型。。。当我创建这种类型的帖子并填写自定义字段时,数据实际上会保存到db。。。但是,如果我丢弃其中一个,那么该帖子的自定义字段数据将从数据库中删除。

我以为垃圾处理基本上就是把post\\u状态改为“垃圾”,所以除非你真的永久删除它,否则它就看不见了?

如果是这样的话,为什么当我丢弃一个项目时会丢失自定义字段数据?

以下是上述问题的所有相关代码:

<?php
add_action(\'admin_init\', \'wpg_add_testimonial_author\');
function wpg_add_testimonial_author() {
  add_meta_box(\'wpg_testimonial_author\', __(\'Author Information\', \'quotable\'), \'wpg_testimonial_author\', \'testimonials\', \'side\', \'low\');
}

function wpg_testimonial_author() {
  global $post;
  $custom = get_post_custom($post->ID);
  $testimonial_author_name = $custom[\'testimonial_author_name\'][0];
  $testimonial_author_link = $custom[\'testimonial_author_link\'][0];
  ?>
  <p>
    <label><?php _e("Author\'s Name:", \'quotable\'); ?></label>
    <input name="testimonial_author_name" value="<?php echo $testimonial_author_name; ?>" />
  </p>
  <p>
    <label><?php _e(\'Attribution Link:\', \'quotable\'); ?></label>
    <input name="testimonial_author_link" value="<?php echo $testimonial_author_link; ?>" />
  </p>
  <?php
}

function wpg_save_testimonial_author() {
  global $post;
  if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
    return $post_id;
  }
  if (defined(\'DOING_AJAX\')) {
    return;
  }
  update_post_meta($post->ID, \'testimonial_author_name\', $_POST[\'testimonial_author_name\']);
  update_post_meta($post->ID, \'testimonial_author_link\', $_POST[\'testimonial_author_link\']);
}
add_action(\'save_post\', \'wpg_save_testimonial_author\');

function wpg_row_actions() {
  global $post;
  if($post->post_type == \'page\') {
    if(!current_user_can(\'edit_page\')) {
      return;
    }
  }
  else {
    if(!current_user_can(\'edit_post\')) {
      return;
    }
  }
  if($post->post_status == \'trash\') {
    $actionLinks  = \'<div class="row-actions"><span class="untrash"><a title="\'.__(\'Restore this item\', \'quotable\').\'" href="\'.wp_nonce_url(get_admin_url().\'post.php?post=\'.$post->ID.\'&action=untrash\', \'untrash-\'.$post->post_type.\'_\'.$post->ID).\'">\'.__(\'Restore\', \'quotable\').\'</a> | </span>\';
    $actionLinks .= \'<span class="trash"><a href="\'.wp_nonce_url(get_admin_url().\'post.php?post=\'.$post->ID.\'&action=delete\', \'delete-\'.$post->post_type.\'_\'.$post->ID).\'" title="\'.__(\'Delete this item permanently\', \'quotable\').\'" class="submitdelete">\'.__(\'Delete Permanently\', \'quotable\').\'</a></span>\';
  }
  else {
    $actionLinks  = \'<div class="row-actions"><span class="edit"><a title="\'.__(\'Edit this item\', \'quotable\').\'" href="\'.get_admin_url().\'post.php?post=\'.$post->ID.\'&action=edit">\'.__(\'Edit\', \'quotable\').\'</a> | </span>\';
    $actionLinks .= \'<span class="inline hide-if-no-js"><a title="\'.__(\'Edit this item inline\', \'quotable\').\'" class="editinline" href="#">\'.__(\'Quick Edit\', \'quotable\').\'</a> | </span>\';
    $actionLinks .= \'<span class="trash"><a href="\'.wp_nonce_url(get_admin_url().\'post.php?post=\'.$post->ID.\'&action=trash\', \'trash-\'.$post->post_type.\'_\'.$post->ID).\'" title="\'.__(\'Move this item to the Trash\', \'quotable\').\'" class="submitdelete">\'._x(\'Trash\', \'verb (ie. trash this post)\', \'quotable\').\'</a></span>\';
  }
  return $actionLinks;
}

function wpg_edit_testimonials_columns($columns) {
  $columns = array(
    \'cb\' => \'<input type="checkbox" />\',
    \'testimonial_author\' => __(\'Author\', \'quotable\'),
    \'testimonial_text\' => __(\'Testimonial\', \'quotable\'),
    \'attribution_link\' => __(\'Attribution Link\', \'quotable\'),
    \'date\' => __(\'Date Added\', \'quotable\')
  );
  return $columns;
}

function wpg_manage_testimonials_columns($column, $post_id) {
  global $post;
  $custom = get_post_custom($post->ID);
  $testimonial_author_name = $custom[\'testimonial_author_name\'][0];
  $testimonial_author_link = $custom[\'testimonial_author_link\'][0];

  $wpg_row_actions  = wpg_row_actions();

  switch($column) {
    case \'testimonial_author\':
      echo $testimonial_author_name.$wpg_row_actions;
    break;

    case \'testimonial_text\':
      echo $post->post_content;
    break;

    case \'attribution_link\':
      echo $testimonial_author_link;
    break;

    default :
      break;
    }
  }
  add_filter(\'manage_edit-testimonials_columns\', \'wpg_edit_testimonials_columns\');
  add_action(\'manage_testimonials_posts_custom_column\', \'wpg_manage_testimonials_columns\', 10, 2);
自定义帖子类型不支持帖子的“标题”部分,因此它不作为编辑帖子页面中的一列包含。因此,我不得不修改我自己版本的row\\u actions()函数,以便在每个帖子悬停时启用编辑/快速编辑/垃圾箱链接。

所有功能都正常工作。。。除了将帖子移动到垃圾箱时,会从数据库中删除“推荐作者”和“归属链接”值。不知道为什么。。。

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

似乎save\\u post操作在将帖子发送到垃圾箱时被触发。。。因此,我的自定义元数据保存功能被激活,没有要通过它发送的$\\u POST数据。

为了避免这种情况,我在save函数中添加了一个nonce。

function wpg_testimonial_author() {
  global $post;
  $custom = get_post_custom($post->ID);
  $testimonial_author_name = $custom[\'testimonial_author_name\'][0];
  $testimonial_author_link = $custom[\'testimonial_author_link\'][0];
  ?>
  <p>
    <label><?php _e("Author\'s Name:", \'quotable\'); ?></label>
    <input name="testimonial_author_name" value="<?php echo $testimonial_author_name; ?>" />
  </p>
  <p>
    <label><?php _e(\'Attribution Link:\', \'quotable\'); ?></label>
    <input name="testimonial_author_link" value="<?php echo $testimonial_author_link; ?>" />
    <input type="hidden" name="testimonial_author_noncename" id="testimonial_author_noncename" value="<?php echo wp_create_nonce(plugin_basename(__FILE__).$post->ID); ?>" />
  </p>
  <?php
}

function wpg_save_testimonial_author($post_id) {
  global $post;
  if (!wp_verify_nonce($_POST[\'testimonial_author_noncename\'], plugin_basename(__FILE__).$post->ID)) {
    return $post->ID;
  }
  if(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) {
    return $post_id;
  }
  if(defined(\'DOING_AJAX\')) {
    return;
  }
  if(!current_user_can(\'edit_post\')) {
    return $post->ID;
  }
  if($post->post_type == \'revision\') {
    return;
  }
  update_post_meta($post->ID, \'testimonial_author_name\', $_POST[\'testimonial_author_name\']);
  update_post_meta($post->ID, \'testimonial_author_link\', $_POST[\'testimonial_author_link\']);
}
add_action(\'save_post\', \'wpg_save_testimonial_author\');
希望这以后能帮助其他人。。。

SO网友:Grows

我遇到了同样的问题,找到了更短的解决方案(在写作方面)。

if (isset($_POST[\'action\']) && $_POST[\'action\'] == \'editpost\') {
// do your update_post_meta, delete_post_meta, etc.
}
我现在试着在5+WP版本上测试它,似乎只在编辑或保存一篇新帖子时才会启动。

SO网友:tbradley22

哇,谢谢你Nero\\u DCLXVI。非常有帮助。对于希望实现此功能的未来用户,以下是其解决方案的简化说明:

将此隐藏输入与其他自定义元输入一起添加:

<input 
    type="hidden" \\
    name="prevent_delete_meta_movetotrash" 
    id="prevent_delete_meta_movetotrash" 
    value="<?php 
        echo wp_create_nonce( plugin_basename(__FILE__) . $post->ID ); 
    ?>" />
<在update\\u post\\u meta()函数之前添加此项
if ( ! wp_verify_nonce( 
        $_POST[\'prevent_delete_meta_movetotrash\'], 
        plugin_basename(__FILE__) . $post->ID ) 
    ) { 
    return $post_id; 
}

结束

相关推荐