Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add wysiwyg editor in Wordpress meta box

I'm creating a meta box for my custom post type. There are multiple fields where I would like to use wysiwyg editor rather than <textarea>. Is is possible to add multiple editors to a meta box?

I would really appreciate your help!

Many thanks. Dasha

like image 990
DashaLuna Avatar asked Sep 06 '25 15:09

DashaLuna


1 Answers

Here is full code example:

add_action( 'add_meta_boxes',  function() { 
    add_meta_box('html_myid_61_section', 'TITLEEEEE', 'my_output_function');
});

function my_output_function( $post ) {
    $text= get_post_meta($post, 'SMTH_METANAME' , true );
    wp_editor( htmlspecialchars($text), 'mettaabox_ID', $settings = array('textarea_name'=>'MyInputNAME') );
}

add_action( 'save_post', function($post_id) {
    if (!empty($_POST['MyInputNAME'])) {
        $datta=sanitize_text_field($_POST['MyInputNAME']);
        update_post_meta($post_id, 'SMTH_METANAME', $datta );
    }
}); 

P.S. MUST-Recommendation from my experience:

Forget adding custom codes, use Advanced Custom Fields, it's excellent and simplify your life.

like image 55
T.Todua Avatar answered Sep 10 '25 12:09

T.Todua