它的工作原理与常规的文本区域一样,只是数据被转义,所以当您调用保存的数据时,请确保使用html_entity_decode
这是一个非常简单的演示类,看看字段是如何创建的。
if (!class_exists(\'wp_editor_meta_box\')){
class wp_editor_meta_box{
public function __construct(){
/* Define the custom box */
add_action( \'add_meta_boxes\', array($this,\'wp_editor__add_custom_box\' ));
/* Do something with the data entered */
add_action( \'save_post\', array($this,\'wp_editor__save_postdata\' ));
}
/* Adds a box to the main column on the Post and Page edit screens */
function wp_editor__add_custom_box() {
add_meta_box(
\'wp_editor_box\',
__( \'WP Editor Box\' ),
array($this,\'wp_editor_meta_box\'),
\'post\'
);
}
/* Prints the box content */
function wp_editor_meta_box( $post ) {
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), \'wp_editor_nonce\' );
$field_value = get_post_meta( $post->ID, \'_wp_editor_\', false );
wp_editor( html_entity_decode($field_value), \'_wp_editor_\' );
}
/* When the post is saved, saves our custom data */
function wp_editor__save_postdata( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return;
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( ( isset ( $_POST[\'wp_editor_nonce\'] ) ) && ( ! wp_verify_nonce( $_POST[\'wp_editor_nonce\'], plugin_basename( __FILE__ ) ) ) )
return;
// Check 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, we\'re authenticated: we need to find and save the data
if ( isset ( $_POST[\'_wp_editor_\'] ) ) {
update_post_meta( $post_id, \'_wp_editor_\', $_POST[\'_wp_editor_\'] );
}
}
}//end class
}//end if
new wp_editor_meta_box();