Custom Editor Meta Box

add_action( ‘init’, ‘wpt_event_posttype’ );
function wpt_event_posttype() {
register_post_type( ‘events’,
array(
‘labels’ => array(
‘name’ => __( ‘Events’ ),
‘singular_name’ => __( ‘Event’ ),
‘add_new’ => __( ‘Add New Event’ ),
‘add_new_item’ => __( ‘Add New Event’ ),
‘edit_item’ => __( ‘Edit Event’ ),
‘new_item’ => __( ‘Add New Event’ ),
‘view_item’ => __( ‘View Event’ ),
‘search_items’ => __( ‘Search Event’ ),
‘not_found’ => __( ‘No events found’ ),
‘not_found_in_trash’ => __( ‘No events found in trash’ )
),
‘public’ => true,
‘supports’ => array( ‘title’, ‘editor’, ‘thumbnail’, ‘comments’ ),
‘capability_type’ => ‘post’,
‘rewrite’ => array(“slug” => “events”), // Permalinks format
‘menu_position’ => 5,
‘register_meta_box_cb’ => ‘add_events_metaboxes’
)
);
}

add_action( ‘add_meta_boxes’, ‘add_events_metaboxes’ );

// Add the Events Meta Boxes
function add_events_metaboxes() {
add_meta_box(WYSIWYG_META_BOX_ID, __(‘WYSIWYG Meta Box’, ‘wysiwyg’), ‘wysiwyg_render_meta_box’, ‘events’);//custom post type name events
}

 

 

<?php
define(‘WYSIWYG_META_BOX_ID’, ‘my-editor’);
define(‘WYSIWYG_EDITOR_ID’, ‘myeditor’); //Important for CSS that this is different
define(‘WYSIWYG_META_KEY’, ‘extra-content’);

function wysiwyg_render_meta_box(){

global $post;

$meta_box_id = WYSIWYG_META_BOX_ID;
$editor_id = WYSIWYG_EDITOR_ID;

//Add CSS & jQuery goodness to make this work like the original WYSIWYG
echo ”
<style type=’text/css’>
#$meta_box_id #edButtonHTML, #$meta_box_id #edButtonPreview {background-color: #F1F1F1; border-color: #DFDFDF #DFDFDF #CCC; color: #999;}
#$editor_id{width:100%;}
#$meta_box_id #editorcontainer{background:#fff !important;}
#$meta_box_id #$editor_id_fullscreen{display:none;}
</style>

<script type=’text/javascript’>
jQuery(function($){
$(‘#$meta_box_id #editor-toolbar > a’).click(function(){
$(‘#$meta_box_id #editor-toolbar > a’).removeClass(‘active’);
$(this).addClass(‘active’);
});

if($(‘#$meta_box_id #edButtonPreview’).hasClass(‘active’)){
$(‘#$meta_box_id #ed_toolbar’).hide();
}

$(‘#$meta_box_id #edButtonPreview’).click(function(){
$(‘#$meta_box_id #ed_toolbar’).hide();
});

$(‘#$meta_box_id #edButtonHTML’).click(function(){
$(‘#$meta_box_id #ed_toolbar’).show();
});

//Tell the uploader to insert content into the correct WYSIWYG editor
$(‘#media-buttons a’).bind(‘click’, function(){
var customEditor = $(this).parents(‘#$meta_box_id’);
if(customEditor.length > 0){
edCanvas = document.getElementById(‘$editor_id’);
}
else{
edCanvas = document.getElementById(‘content’);
}
});
});
</script>
“;

//Create The Editor
$content = get_post_meta($post->ID, WYSIWYG_META_KEY, true);
the_editor($content, $editor_id);

//Clear The Room!
echo “<div style=’clear:both; display:block;’></div>”;
}

add_action(‘save_post’, ‘wysiwyg_save_meta’);
function wysiwyg_save_meta(){

$editor_id = WYSIWYG_EDITOR_ID;
$meta_key = WYSIWYG_META_KEY;

if(isset($_REQUEST[$editor_id]))
update_post_meta($_REQUEST[‘post_ID’], WYSIWYG_META_KEY, $_REQUEST[$editor_id]);

}

 

//Display Editor Content in page

<?php $content = get_post_meta($post->ID, WYSIWYG_META_KEY, true); ?>
<p><?php echo $content; ?></p>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s