Skip to content

Instantly share code, notes, and snippets.

@dcondrey
Created July 3, 2014 04:12
Show Gist options
  • Save dcondrey/ae987ec166fde5e35be3 to your computer and use it in GitHub Desktop.
Save dcondrey/ae987ec166fde5e35be3 to your computer and use it in GitHub Desktop.
Add a second TinyMCE editor to Wordpress post editor page. The second editor will look, and function exactly like the original one with full toolbar, and support for shortcodes.
/* Second Post Editor TinyMCE Editor */
class SubContentEditor {
public $meta_key = 'subcontent';
public $meta_label = 'Right Side'; // Headline above editor
public $post_type = array( 'page' ); // The post type in which the editor should display
public $wpautop = true; // Automatically create paragraphs?
function __construct() {
add_action( 'edit_form_after_editor', array( &$this, 'edit_form_after_editor' ) );
add_action( 'save_post', array( &$this, 'save_post' ) );
add_action( 'init', array( &$this, 'init' ) );
}
public function init() {
$this->meta_key = apply_filters( 'roman-sce-meta_key', $this->meta_key );
$this->post_type = apply_filters( 'roman-sce-post_type', $this->post_type );
$this->meta_label = apply_filters( 'roman-sce-meta_label', $this->meta_label );
$this->wpautop = apply_filters( 'roman-sce-wpautop', $this->wpautop );
}
public function edit_form_after_editor() {
if ( !is_admin() ) { return; }
if ( in_array( get_post_type( $GLOBALS['post'] ), $this->post_type ) ) { return; }
$value = $this->get_the_subcontent();
$sc_arg = array(
'textarea_rows' => apply_filters( 'roman-sce-row', 10 ),
'wpautop' => $this->wpautop,
'media_buttons' => apply_filters( 'roman-sce-media_buttons', true ),
'tinymce' => apply_filters( 'roman-sce-tinymce', true ),
);
echo '<h3 class="subcontentLabel" style="margin-top:15px;">' . $this->meta_label . '</h3>';
wp_editor( $value, 'subcontent', $sc_arg );
}
public function save_post( $post_id ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
if ( isset ( $_POST[ $this->meta_key ] ) ) {
return update_post_meta( $post_id, $this->meta_key, $_POST[ $this->meta_key ] );
}
delete_post_meta( $post_id, $this->meta_key );
return $post_id;
}
public function get_the_subcontent() {
global $post;
$subcontent = do_shortcode(get_post_meta( $post->ID, $this->meta_key, true ));
if ( $this->wpautop == true ) {
return wpautop( $subcontent );
} else {
return $subcontent;
}
}
}
$romanSCE = new SubContentEditor();
function get_the_subcontent() {
global $romanSCE;
return $romanSCE->get_the_subcontent();
}
/**
* use get_the_subcontent() where you want the content of the
* second editor to display, just like the_content
*/
function the_subcontent() {
echo get_the_subcontent();
}
@hadifathii
Copy link

How can I add third editor?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment