Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@helgatheviking
Created November 29, 2015 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save helgatheviking/59f0bd3b7f5972752704 to your computer and use it in GitHub Desktop.
Save helgatheviking/59f0bd3b7f5972752704 to your computer and use it in GitHub Desktop.
Add multiple wp_editor() inputs to a single metabox. Save as single meta value.
/**
* Adds a box to the main column on the Post and Page edit screens.
*/
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function myplugin_meta_box_callback( $post ) {
// Add a nonce field so we can check for it later.
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
/*
* Use get_post_meta() to retrieve an existing value
* from the database and use the value for the form.
*/
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
?>
<div class="my_meta_control">
<p><?php _e('Text editor #1', 'myplugin_textdomain');?></p>
<p>
<?php
$one = isset($value['one'] ) ? $value['one'] : '';
$settings = array(
'textarea_rows' => '10',
'media_buttons' => true,
'textarea_name' => 'myplugin_new_field[one]',
);
wp_editor( $one, 'myplugin_new_field_one' , $settings );
?>
<span><?php _e( 'Enter in some text', 'myplugin_textdomain' );?></span>
</p>
<p>
<?php
$two = isset($value['two'] ) ? $value['two'] : '';
$settings = array(
'textarea_rows' => '10',
'media_buttons' => true,
'textarea_name' => 'myplugin_new_field[two]',
);
wp_editor( $two, 'myplugin_new_field_two' , $settings );
?>
<span><?php _e( 'Enter in some text', 'myplugin_textdomain' );?></span>
</p>
<p class="meta-save"><button type="submit" class="button-primary" name="save"><?php _e('Update');?></button></p>
</div>
<?php
}
/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function myplugin_save_meta_box_data( $post_id ) {
/*
* We need to verify this came from our screen and with proper authorization,
* because the save_post action can be triggered at other times.
*/
// Check if our nonce is set.
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's 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, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
// Sanitize user input.
$my_data = array();
foreach( $_POST['myplugin_new_field'] as $i => $value ){
$my_data[$i] = sanitize_post_field( 'post_content', $value, $post_id, 'db' );
}
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment