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!


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!

|improve this answer|||||
  • 2
    You should sanitize this with wp_kses_post() before saving it to the database. – GhostToast Mar 18 '16 at 23:10

This is how I have it in one of my plugins:

wp_nonce_field('nonce_action', 'nonce_field');
$content = get_option('my_content');
wp_editor( $content, 'settings_wpeditor' );

function settings_save_wpeditor(){
// check the nonce, update the option etc...

if(isset($_POST['settings_wpeditor']) && isset($_POST['nonce_field']) && wp_verify_nonce($_POST['nonce_field'], 'nonce_action') ){
update_option('my_content', wp_kses_post($_POST['settings_wpeditor']));

add_action('admin_init', 'settings_save_wpeditor', 10);

Be sure to add prefixes to your variables and option names and also look up wp_none and wp_verify_nonce from the WordPress codex.

https://codex.wordpress.org/Function_Reference/wp_nonce_field https://codex.wordpress.org/Function_Reference/wp_verify_nonce

|improve this answer|||||
$editor_id = 'product_summary';

$summary =  get_post_meta( $post->ID,'product_summary', true );
wp_editor( $summary, $editor_id );
if( isset( $_REQUEST['product_summary'] ) ){
        update_post_meta( $post_id, 'product_summary',wp_kses_post( $_POST['product_summary'] ) );

value update in wp_editor but not update the value in database, please check and solve it?

|improve this answer|||||

Hi that is the solution of your problem, Just copy and paste and see.....

    function dhansikhi_hukamnama_post_page(){
    <div class='wrap'>
    <h2>My Super Admin Page</h2>
    <form method='post'>
    wp_nonce_field('nates_nonce_action', 'nates_nonce_field');
    $content = get_option('special_content');
    wp_editor( $content, 'special_content' );
    submit_button('Save', 'primary');
    </div><!-- .wrap -->
    add_action( 'admin_menu', 'dhansikhi_hukamnama_page' );
|improve this answer|||||
  • Problem was all ready solved! – Interactive Aug 21 '17 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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