Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I got a client requirement where the editing facilities available in wordpress had to be available for content that was meant to be displayed on the sidebar of a particular post/page.

The content to be displayed was implemented originally using metaboxes which contained a single textarea where content was input as raw HTML and did not have insert/upload facilities.

I have decided to migrate this design to use wp_editor() as it gives all the wordpress native editing facilities (without using a metabox as it is not recommended). But I am not sure how to get the entered data and POST it to the backend and then tie into the following action to save the data:

add_action( 'save_post', 'save_sidebar_content' );

I was also using the following helper functions:

get_post_meta();
add_post_meta();
update_post_meta();
delete_post_meta();

I would like to know if there are any such functions to ease the CRUD operations related to this data when using wp_editor().

Thanks in advance. nav

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

It works just the same like a regular textarea with the exception of the data being escaped so when you call for the saved data just make sure to decode the html entities using html_entity_decode here is a very simple demo class, take a look at how the field is created.

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();
share|improve this answer
    
Thank you very much @Bainternet. Looks like have been a little mislead by WP saying that wp_editor() should not be used in metaboxes. Then again this is only a problem if I intend to move the metaboxes which is not going to happen in my case. Thanks, nav –  navanitachora Apr 21 '12 at 8:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.