如何将多个wp_Editor_box添加到新帖子

时间:2019-03-27 作者:m.javad koushki

我正在尝试使用多个wp_editor_meta_box 到我的帖子进行自定义post_meta 但问题是,当我对它们中的每一个使用相同的代码时,都会出现错误

Fatal error: Cannot redeclare wp_editor_meta_box() (previously declared in C:\\xampp\\htdocs\\aria\\wp-content\\themes\\ariadl\\functions.php:100) in C:\\xampp\\htdocs\\aria\\wp-content\\themes\\ariadl\\functions.php on line 175
以下是我的代码:

/* Define the custom box */
add_action( \'add_meta_boxes\', \'myplugin2_add_custom_box\' );

/* Do something with the data entered */
add_action( \'save_post\', \'myplugin2_save_postdata\' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin2_add_custom_box() {
  add_meta_box( \'wp_editor_test_2_box\', \'اطلاعات سیستم مورد نیاز\', \'wp_editor_meta_box\' );
}

/* Prints the box content */
function wp_editor_meta_box( $post ) {

  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );

  $field_value = get_post_meta( $post->ID, \'_wp_editor_test_2\', false );
  wp_editor( $field_value[0], \'_wp_editor_test_2\' );
}

/* When the post is saved, saves our custom data */
function myplugin2_save_postdata( $post_id ) {

  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( ( isset ( $_POST[\'myplugin_noncename\'] ) ) && ( ! wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) ) )
      return;

  // Check permissions
  if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] )  ) {
    if ( ! current_user_can( \'edit_page\', $post_id ) ) {
      return;
    }    
  }
  else {
    if ( ! current_user_can( \'edit_post\', $post_id ) ) {
      return;
    }
  }

  // OK, we\'re authenticated: we need to find and save the data
  if ( isset ( $_POST[\'_wp_editor_test_2\'] ) ) {
    update_post_meta( $post_id, \'_wp_editor_test_2\', $_POST[\'_wp_editor_test_2\'] );
  }

}


/* Define the custom box */
add_action( \'add_meta_boxes\', \'myplugin1_add_custom_box\' );

/* Do something with the data entered */
add_action( \'save_post\', \'myplugin1_save_postdata\' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin1_add_custom_box() {
  add_meta_box( \'wp_editor_test_3_box\', \'راهنمای نصب\', \'wp_editor_meta_box\' );
}

/* Prints the box content */
function wp_editor_meta_box( $post ) {

  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin1_noncename\' );

  $field_value = get_post_meta( $post->ID, \'_wp_editor_test_3\', false );
  wp_editor( $field_value[0], \'_wp_editor_test_3\' );
}

/* When the post is saved, saves our custom data */
function myplugin1_save_postdata( $post_id ) {

  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( ( isset ( $_POST[\'myplugin1_noncename\'] ) ) && ( ! wp_verify_nonce( $_POST[\'myplugin1_noncename\'], plugin_basename( __FILE__ ) ) ) )
      return;

  // Check permissions
  if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] )  ) {
    if ( ! current_user_can( \'edit_page\', $post_id ) ) {
      return;
    }    
  }
  else {
    if ( ! current_user_can( \'edit_post\', $post_id ) ) {
      return;
    }
  }

  // OK, we\'re authenticated: we need to find and save the data
  if ( isset ( $_POST[\'_wp_editor_test_3\'] ) ) {
    update_post_meta( $post_id, \'_wp_editor_test_3\', $_POST[\'_wp_editor_test_3\'] );
  }

}


/* Define the custom box */
add_action( \'add_meta_boxes\', \'myplugin_add_custom_box\' );

/* Do something with the data entered */
add_action( \'save_post\', \'myplugin_save_postdata\' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
  add_meta_box( \'wp_editor_test_1_box\', \'اطلاعات باکس دانلود >> در این قسمت می توانید لینک های دانلود مطلب مورد نظر را با متن دلخواه قرار دهید.در هر خط یه لینک قرار دهید\', \'wp_editor_meta_box\' );
}

/* Prints the box content */
function wp_editor_meta_box( $post ) {

  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );

  $field_value = get_post_meta( $post->ID, \'_wp_editor_test_1\', false );
  wp_editor( $field_value[0], \'_wp_editor_test_1\' );
}

/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {

  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( ( isset ( $_POST[\'myplugin_noncename\'] ) ) && ( ! wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) ) )
      return;

  // Check permissions
  if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] )  ) {
    if ( ! current_user_can( \'edit_page\', $post_id ) ) {
      return;
    }    
  }
  else {
    if ( ! current_user_can( \'edit_post\', $post_id ) ) {
      return;
    }
  }

  // OK, we\'re authenticated: we need to find and save the data
  if ( isset ( $_POST[\'_wp_editor_test_1\'] ) ) {
    update_post_meta( $post_id, \'_wp_editor_test_1\', $_POST[\'_wp_editor_test_1\'] );
  }

}

1 个回复
最合适的回答,由SO网友:Tanmay Patel 整理而成

试试这个。您已使用相同的函数名3次。它必须是动态名称。所以

<?php
/**
 * Plugin Name: WP Editor in a Meta Box
 * Plugin URI: 
 * Description: Demonstration of WP Editor in a meta box.
 * Version: 1.0
 * Author: goto10
 * Author URI: 
 * License: 
 */

// file name: wp_editor-in-meta-box-test.php 

/* Meta box code based on http://codex.wordpress.org/Function_Reference/add_meta_box */

/* Define the custom box */
add_action( \'add_meta_boxes\', \'myplugin2_add_custom_box\' );

/* Do something with the data entered */
add_action( \'save_post\', \'myplugin2_save_postdata\' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin2_add_custom_box() {
  add_meta_box( \'wp_editor_test_2_box\', \'اطلاعات سیستم مورد نیاز\', \'wp_editor_meta_box2\' );
}

/* Prints the box content */
function wp_editor_meta_box2( $post ) {

  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );

  $field_value = get_post_meta( $post->ID, \'_wp_editor_test_2\', false );
  wp_editor( $field_value[0], \'_wp_editor_test_2\' );
}

/* When the post is saved, saves our custom data */
function myplugin2_save_postdata( $post_id ) {

  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( ( isset ( $_POST[\'myplugin_noncename\'] ) ) && ( ! wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) ) )
      return;

  // Check permissions
  if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] )  ) {
    if ( ! current_user_can( \'edit_page\', $post_id ) ) {
      return;
    }    
  }
  else {
    if ( ! current_user_can( \'edit_post\', $post_id ) ) {
      return;
    }
  }

  // OK, we\'re authenticated: we need to find and save the data
  if ( isset ( $_POST[\'_wp_editor_test_2\'] ) ) {
    update_post_meta( $post_id, \'_wp_editor_test_2\', $_POST[\'_wp_editor_test_2\'] );
  }

}
add_action( \'add_meta_boxes\', \'myplugin1_add_custom_box\' );

/* Do something with the data entered */
add_action( \'save_post\', \'myplugin1_save_postdata\' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin1_add_custom_box() {
  add_meta_box( \'wp_editor_test_3_box\', \'راهنمای نصب\', \'wp_editor_meta_box3\' );
}

/* Prints the box content */
function wp_editor_meta_box3( $post ) {

  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin1_noncename\' );

  $field_value = get_post_meta( $post->ID, \'_wp_editor_test_3\', false );
  wp_editor( $field_value[0], \'_wp_editor_test_3\' );
}

/* When the post is saved, saves our custom data */
function myplugin1_save_postdata( $post_id ) {

  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( ( isset ( $_POST[\'myplugin1_noncename\'] ) ) && ( ! wp_verify_nonce( $_POST[\'myplugin1_noncename\'], plugin_basename( __FILE__ ) ) ) )
      return;

  // Check permissions
  if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] )  ) {
    if ( ! current_user_can( \'edit_page\', $post_id ) ) {
      return;
    }    
  }
  else {
    if ( ! current_user_can( \'edit_post\', $post_id ) ) {
      return;
    }
  }

  // OK, we\'re authenticated: we need to find and save the data
  if ( isset ( $_POST[\'_wp_editor_test_3\'] ) ) {
    update_post_meta( $post_id, \'_wp_editor_test_3\', $_POST[\'_wp_editor_test_3\'] );
  }

}
add_action( \'add_meta_boxes\', \'myplugin_add_custom_box\' );

/* Do something with the data entered */
add_action( \'save_post\', \'myplugin_save_postdata\' );

/* Adds a box to the main column on the Post and Page edit screens */
function myplugin_add_custom_box() {
  add_meta_box( \'wp_editor_test_1_box\', \'اطلاعات باکس دانلود >> در این قسمت می توانید لینک های دانلود مطلب مورد نظر را با متن دلخواه قرار دهید.در هر خط یه لینک قرار دهید\', \'wp_editor_meta_box1\' );
}

/* Prints the box content */
function wp_editor_meta_box1( $post ) {

  // Use nonce for verification
  wp_nonce_field( plugin_basename( __FILE__ ), \'myplugin_noncename\' );

  $field_value = get_post_meta( $post->ID, \'_wp_editor_test_1\', false );
  wp_editor( $field_value[0], \'_wp_editor_test_1\' );
}

/* When the post is saved, saves our custom data */
function myplugin_save_postdata( $post_id ) {

  // verify if this is an auto save routine. 
  // If it is our form has not been submitted, so we dont want to do anything
  if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) 
      return;

  // verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( ( isset ( $_POST[\'myplugin_noncename\'] ) ) && ( ! wp_verify_nonce( $_POST[\'myplugin_noncename\'], plugin_basename( __FILE__ ) ) ) )
      return;

  // Check permissions
  if ( ( isset ( $_POST[\'post_type\'] ) ) && ( \'page\' == $_POST[\'post_type\'] )  ) {
    if ( ! current_user_can( \'edit_page\', $post_id ) ) {
      return;
    }    
  }
  else {
    if ( ! current_user_can( \'edit_post\', $post_id ) ) {
      return;
    }
  }

  // OK, we\'re authenticated: we need to find and save the data
  if ( isset ( $_POST[\'_wp_editor_test_1\'] ) ) {
    update_post_meta( $post_id, \'_wp_editor_test_1\', $_POST[\'_wp_editor_test_1\'] );
  }

}
?>

相关推荐

自定义MetaBox中的WP查询排序顺序

我正在尝试对基于meta\\u键交易金额的帖子列表进行排序,这将首先显示最高的交易,然后逐步降低到较小的交易金额。交易金额将在1000至900万之间。目前,下面的代码正在根据第一个数字对交易金额进行排序。因此,如果我有一个6000和6000000的交易,他们会将它们并排放置,而不是先显示6000000。交易金额从CMB2内的自定义元数据库中提取。我的代码中是否缺少了排序无法识别逗号后的金额的内容?我的WP\\U查询 <?php $transaction = n