| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Add and render Metabox for post |
|---|
| 4 | */ |
|---|
| 5 | |
|---|
| 6 | add_action( 'add_meta_boxes', 'wpdev_add_post_metabox' ); |
|---|
| 7 | |
|---|
| 8 | function wpdev_add_post_metabox() { |
|---|
| 9 | // Add new meta box. |
|---|
| 10 | add_meta_box( |
|---|
| 11 | 'wpdev_post_metabox', |
|---|
| 12 | 'Test WPeditor', |
|---|
| 13 | 'wpdev_render_post_metabox', |
|---|
| 14 | 'post', |
|---|
| 15 | 'normal' |
|---|
| 16 | ); |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | function wpdev_render_post_metabox() { |
|---|
| 20 | global $post; |
|---|
| 21 | |
|---|
| 22 | // Get saved meta data. |
|---|
| 23 | $post_note_meta_content = get_post_meta( $post->ID, 'wpdev_post_field_tinymce', true ); |
|---|
| 24 | |
|---|
| 25 | // Nonce field. |
|---|
| 26 | wp_nonce_field( 'wpdev_post_metabox_nonce_' . $post->ID, 'wpdev_post_metabox_nonce' ); |
|---|
| 27 | |
|---|
| 28 | // Render editor metabox. |
|---|
| 29 | wp_editor( |
|---|
| 30 | $post_note_meta_content, |
|---|
| 31 | 'wpdev_post_field_tinymce', |
|---|
| 32 | array( 'textarea_rows' => '5' ) |
|---|
| 33 | ); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Save post metabox |
|---|
| 38 | */ |
|---|
| 39 | |
|---|
| 40 | add_action( 'save_post', 'wpdev_save_post' ); |
|---|
| 41 | |
|---|
| 42 | function wpdev_save_post( $post_id ) { |
|---|
| 43 | |
|---|
| 44 | // Bail if auto save. |
|---|
| 45 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
|---|
| 46 | return; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | // Bail if nonce is missing or nonce verification fails. |
|---|
| 50 | if ( ! isset( $_POST['wpdev_post_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['wpdev_post_metabox_nonce'], 'wpdev_post_metabox_nonce_' . $post_id ) ) { |
|---|
| 51 | return; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | // Bail is current user is allowed to edit posts. |
|---|
| 55 | if ( ! current_user_can( 'edit_post' ) ) { |
|---|
| 56 | return; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | // Make sure our data is set before trying to save it. |
|---|
| 60 | if ( isset( $_POST['wpdev_post_field_tinymce'] ) ) { |
|---|
| 61 | update_post_meta( $post_id, 'wpdev_post_field_tinymce', $_POST['wpdev_post_field_tinymce'] ); |
|---|
| 62 | } |
|---|
| 63 | } |
|---|