After some searching and working off of some pointers from toscho, and some other helpful posts to avoid an infinite loop I managed to figure out a solution. I’ll post the code below then briefly explain:
// 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'));
}
My problem was taking existing posts and converting them into a custom post type (the client had used regular posts to create a staff directory). I wanted to modify the default editor, rename and simplify it while using the existing post_content.
Basically, I looked at several pre-existing solutions (linked above) and arrived at my own. The important consideration is that I was using post_content
, though it was now edited through a custom metabox. To make edits, I needed to run updates through the wp_update_post
function hooked into save_post
.
However there are issues with an infinite loop being made, so then I needed to unhook and rehook the save_post
action as made clear in the WP Codex. The final issue was just having the correct formatting for the remove_action
and add_action
hooks, taking into consideration that I was calling them within my custom class.