I’m creating a custom plugin. One of the fields in the plugin let’s the user add data.
If I add a textarea the user has no control over the text. So I would like to add a editor.
I know WordPress offers the wp_editor(); function.
After some Google-ing I found that it is very easy to implement the editor.:

$content="";
$editor_id = 'mycustomeditor';

wp_editor( $content, $editor_id );

This shows a nice editor. The problem is that the content isn’t getting saved.

The editor is a part of a form so I thought I add the save function to the form save function like this:
if( isset( $_POST[ 'mycustomeditor' ] ) ) {update_post_meta( $post_id, 'mycustomeditor', array_map('sanitize_text_field', $_POST[ 'mycustomeditor' ]) );}

However WordPress thinks different about this. It does create the meta_key in the database but no value.

I hope anyone can see what I’m doing wrong!

4 Answers
4

Solved it!

Hope somebody else can use it or it answers their problem. If there is a better way please share and tell why.

$editor_id = 'custom_editor_box';
$uploaded_csv = get_post_meta( $post->ID, 'custom_editor_box', true);
wp_editor( $uploaded_csv, $editor_id );

To save the data:

function save_wp_editor_fields(){
    global $post;
    update_post_meta($post->ID, 'custom_editor_box', $_POST['custom_editor_box']);
}
add_action( 'save_post', 'save_wp_editor_fields' );

And that’s all there is to it!

Leave a Reply

Your email address will not be published. Required fields are marked *