在对toscho的一些指针进行一些搜索和处理之后some other helpful posts to avoid an infinite loop 我设法想出了一个解决办法。我将在下面发布代码,然后简要解释:
// Hook into the actions here
public function __construct() {
add_action( \'add_meta_boxes\', array($this, \'meta_boxes\' ));
add_action( \'save_post\', array($this, \'save_bio_data\' ), 10, 1);
}
// Modify default content editor
/**
* Adds a box to the main column on the Post edit screen uses modified editor
*
* @hook add_meta_boxes
* @see http://codex.wordpress.org/Function_Reference/add_meta_box
*/
public function meta_boxes()
{
global $_wp_post_type_features;
//check for the required post type page or post or <custom post type(here article)
if (isset($_wp_post_type_features[\'tn_cstm_people\'][\'editor\'])) {
unset($_wp_post_type_features[\'tn_cstm_people\'][\'editor\']);
add_meta_box(
\'tn_cstm_people_bio\',
__(\'Bio\'),
// To address callback function in a class
array($this, \'bio_editor_meta_box\'),
\'tn_cstm_people\', \'normal\', \'core\'
);
}
}
public function bio_editor_meta_box($post)
{
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'tn_cstm_noncename\' );
$settings = array(
// Removed media upload buttons and added other values here
\'media_buttons\' => false,
\'textarea_name\'=>\'tn_cstm_bio\',
\'tabindex\' => \'4\',
\'teeny\' => true
);
// Define your custom wp_editor here. Use existing post content: $post->post_content
wp_editor($post->post_content,\'tn_cstm_bio\', $settings);
}
/**
* When the post is saved, save our customized post data
*
* @hook save_post
*/
public function save_bio_data($post_id) {
// check if we\'re on the post page and if the nonce has been set
if ( !isset( $_POST[\'post_type\'] ) or !isset( $_POST[\'tn_cstm_noncename\'] ) ) {
return;
}
// check if the we\'re on the custom post page
if ( !in_array( $_POST[\'post_type\'], array( \'tn_cstm_people\' ) ) ) {
return;
}
// if we\'re doing an auto save return
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify nonce for the safety reasones
// @see http://codex.wordpress.org/WordPress_Nonces
if ( !wp_verify_nonce( $_POST[\'tn_cstm_noncename\'], plugin_basename( __FILE__ ) ) )
return;
// make sure that current user has proper rights to save the post
// @see http://codex.wordpress.org/Roles_and_Capabilities
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
/* Verify that post type is not set to revision and that $post object needs to be updated
*http://codex.wordpress.org/Function_Reference/wp_update_post
*/
// unhook this function so it doesn\'t loop infinitely
remove_action(\'save_post\', array($this,\'save_bio_data\'));
// update the post, which calls save_post again
$data_content = $_POST[\'tn_cstm_bio\'];
$my_post = array();
$my_post[\'ID\'] = $post_id;
$my_post[\'post_content\'] = $data_content;
wp_update_post( $my_post );
// re-hook this function don\'t forget to format as add_action(\'save_post\', array($this, \'$function_name\'))\'
add_action(\'save_post\', array($this, \'save_bio_data\'));
}
我的问题是获取现有帖子并将其转换为自定义帖子类型(客户使用常规帖子创建员工目录)。我想修改默认编辑器,重命名并简化它,同时使用现有的post\\u内容。
基本上,我查看了几个预先存在的解决方案(如上所述),并得出了自己的解决方案。重要的考虑是我使用post_content
, 虽然它现在是通过自定义元数据库编辑的。要进行编辑,我需要通过wp_update_post
连接到的函数save_post
.
然而,在进行无限循环时存在一些问题,因此我需要取消挂钩并重新挂钩save_post
行动如WP Codex. 最后一个问题是对remove_action
和add_action
hooks,考虑到我在自定义类中调用它们。