自定义帖子类型的WP_EDITOR不保存值

时间:2015-08-11 作者:terminator

我已经这么做过很多次了,但这些天来,我面临着元框和wp\\u编辑器的问题,许多通知、错误和奇怪的行为。

我已经能够解决元框通知和保存的问题。主要问题是暂时的。我使用的nonce用于插件使用。但是我的代码在主题中,所以现在我无法在metabox中保存值,也没有更多关于metabox的通知。

但现在我得到通知了Notice: Undefined variable: main_detail 对于wp\\U编辑器。在我点击发布/更新后,它不会保存值

   <?php
function wpt_performer_posttype() {
  register_post_type( \'performer\',
    array(
      \'labels\' => array(
        \'name\' => __( \'Performer\' ),
        \'singular_name\' => __( \'performer\' ),
        \'add_new\' => __( \'Add New performer\' ),
        \'add_new_item\' => __( \'Add New performer\' ),
        \'edit_item\' => __( \'Edit performer\' ),
        \'new_item\' => __( \'Add New performer\' ),
        \'view_item\' => __( \'View performer\' ),
        \'search_items\' => __( \'Search performer\' ),
        \'not_found\' => __( \'No performer found\' ),
        \'not_found_in_trash\' => __( \'No performer found in trash\' )
      ),
      \'public\' => true,
      \'supports\' => array( \'title\',\'thumbnail\',\'page-attributes\' ),
      \'capability_type\' => \'post\',
      \'rewrite\' => array("slug" => "performer"), // Permalinks format
      \'menu_position\' => 6,         
      \'show_ui\'=>true,
      \'query_var\'=>true,
      \'register_meta_box_cb\' => \'add_performer_metaboxes\'

      )
  );
}
add_action( \'init\', \'wpt_performer_posttype\' );

/*Add featured thumbnails to the custom post type*/
add_theme_support( \'post-thumbnails\' );
/*Now we add the meta boxes to the performer*/
// add_action(\'add_meta_boxes\', \'add_performer_metaboxes\');
function add_performer_metaboxes() {   
  add_meta_box(\'wpt_performer_lines\', __(\'Extra fields\'), \'wpt_performer_lines\', \'performer\', \'normal\', \'high\'); 
}

function wpt_performer_lines(){
  global $post;


  wp_nonce_field( \'my_meta_box\', \'performermeta_noncename\' );

  $short_description = get_post_meta($post->ID, \'_short_description\', true);

  echo \'<label >\';?><?php _e( \'short description:\' );?></label>
  <?php echo \'<br><textarea name="_short_description" rows="1" cols="90">\'.$short_description.\'</textarea>\';?>

  <?php wp_editor( $main_detail, \'main_detail\', array( \'textarea_name\' => \'_main_detail\', \'textarea_rows\' =>5, \'media_buttons\' => false ) );  
}

function wpt_save_performer_meta($post_id, $post) {
  if ( ! isset( $_POST[\'performermeta_noncename\'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST[\'performermeta_noncename\'], \'my_meta_box\' ) ) {
        return;
    }
  if ( !current_user_can( \'edit_post\', $post->ID ))
    return $post->ID;

  $performer_meta[\'_short_description\'] = trim($_POST[\'_short_description\']); 

  foreach ($performer_meta as $key => $value) { // Cycle through the $performer_meta array!
    if( $post->post_type == \'revision\' ) return; // Don\'t store custom data twice
    $value = implode(\',\', (array)$value); // If $value is an array, make it a CSV (unlikely)
    if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
      update_post_meta($post->ID, $key, $value);
    } else { // If the custom field doesn\'t have a value
      add_post_meta($post->ID, $key, $value);
    }
    if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
  }
}

add_action(\'save_post\', \'wpt_save_performer_meta\', 1, 2); // save the custom fields

?>

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

您忘记声明变量$main_detail. 因为它是一个元字段,所以在添加之前需要先检索它。

$main_detail = get_post_meta($post->ID, \'_main_detail\', true);  
此外,我可以看到您的代码中存在一些错误。

echo \'<label >\';?><?php _e( \'short description:\' );?></label>
应该是。。

echo \'<label >\' . _e( "short description:" ) . \'</label>\';
而且,您还需要添加密钥_main_detail 在您的$performer_meta 数组以保存它。

SO网友:Marek

在您的wpt_save_performer_meta() 正在迭代的函数$performer_meta 数组并保存其值。但它只有一个手动设置的键(_short_description) ... 其价值是isset($_POST[\'_short_description\']), 这可能总是true, 并保存(或显示)为1.

所以应该是这样的:

$performer_meta[\'_short_description\'] = trim($_POST[\'_short_description\']);
但您的代码不会保存_main_detail 编辑器字段,因为其键不存在于$performer_meta 大堆

Edit: Notice: Undefined variable: main_detail ... 是否因为变量$main_detail 缺少,但您正在中使用它wp_editor().

结束

相关推荐

保存Metabox自定义字段值

我使用此代码显示帖子类型下拉列表,以选择可用的帖子类型,但当我提交帖子时,它会将值还原回帖子,如何确保它会保存它?<select name=\'my_meta_box_post_type\' id=\'my_meta_box_post_type\'> <?php $post_types=get_post_types(\'\', \'objects\'); foreach ($post_types as $post_type): ?>