Metabox Second Editor 2
class sqpp_metabox_second_editor {
public function __construct() {
if ( is_admin() ) {
add_action( 'load-post.php', array( $this, 'init_metabox' ) );
add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
}
}
public function init_metabox() {
add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );
add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 );
}
public function add_metabox() {
add_meta_box(
'sqpp-mb-second-editor',
__( 'Additional Editor', 'sqppcptmeta' ),
array( $this, 'render_sqpp_metabox_second_editor' ),
'page',
'normal',
'default'
);
}
public function render_sqpp_metabox_second_editor( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'nonce_action', 'nonce' );
// Retrieve an existing value from the database.
$sqpp_mb_second_editor_wysiwyg = get_post_meta( $post->ID, 'sqpp_mb_second_editor_wysiwyg', true );
$sqpp_mb_second_editor_yn = get_post_meta( $post->ID, 'sqpp_mb_second_editor_yn', true );
// Set default values.
if( empty( $sqpp_mb_second_editor_wysiwyg ) ) $sqpp_mb_second_editor_wysiwyg = '';
if( empty( $sqpp_mb_second_editor_yn ) ) $sqpp_mb_second_editor_yn = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="sqpp_mb_second_editor_wysiwyg" class="sqpp_mb_second_editor_wysiwyg_label">' . __( '', 'sqppcptmeta' ) . '</label></th>';
echo ' <td>';
wp_editor( $sqpp_mb_second_editor_wysiwyg, 'sqpp_mb_second_editor_wysiwyg', array( 'media_buttons' => true ) );
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="sqpp_mb_second_editor_yn" class="sqpp_mb_second_editor_yn_label">' . __( 'Yes', 'sqppcptmeta' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="checkbox" id="sqpp_mb_second_editor_yn" name="sqpp_mb_second_editor_yn" class="sqpp_mb_second_editor_yn_field" value="checked" ' . checked( $sqpp_mb_second_editor_yn, 'checked', false ) . '> ' . __( '', 'sqppcptmeta' ) . '</label>';
echo ' <span class="description">' . __( 'An additional editor can be activated below to add and edit content.', 'sqppcptmeta' ) . '</span>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['nonce'] ) ? $_POST['nonce'] : '';
$nonce_action = 'nonce_action';
// Check if a nonce is set.
if ( ! isset( $nonce_name ) )
return;
// Check if a nonce is valid.
if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
return;
// Check if the user has permissions to save data.
if ( ! current_user_can( 'edit_post', $post_id ) )
return;
// Check if it's not an autosave.
if ( wp_is_post_autosave( $post_id ) )
return;
// Check if it's not a revision.
if ( wp_is_post_revision( $post_id ) )
return;
// Sanitize user input.
$new_sqpp_mb_second_editor_wysiwyg = isset( $_POST[ 'sqpp_mb_second_editor_wysiwyg' ] ) ? wp_kses_post( $_POST[ 'sqpp_mb_second_editor_wysiwyg' ] ) : '';
$new_sqpp_mb_second_editor_yn = isset( $_POST[ 'sqpp_mb_second_editor_yn' ] ) ? 'checked' : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'sqpp_mb_second_editor_wysiwyg', $new_sqpp_mb_second_editor_wysiwyg );
update_post_meta( $post_id, 'sqpp_mb_second_editor_yn', $new_sqpp_mb_second_editor_yn );
}
}
new sqpp_metabox_second_editor;