How to add wysiwyg editor in Wordpress meta box

2019-01-17 21:35发布

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

8条回答
别忘想泡老子
2楼-- · 2019-01-17 21:36
// for custom post type

function wo_second_editor($post) {

  echo "<h3>Write here your text for the blue box on the right:</h3>";
  $content = get_post_meta($post->ID, 'wo_blue_box' , true ) ;
  wp_editor( htmlspecialchars_decode($content), 'wo_blue_box', array("media_buttons" => false) );
}

add_action('edit_form_advanced', 'wo_second_editor');


function wo_save_postdata($post_id, $post, $update) {

  //...

  if (!empty($_POST['wo_blue_box'])) {
    $data=htmlspecialchars($_POST['wo_blue_box']);
    update_post_meta($post_id, 'wo_blue_box', $data );
  }
}

add_action('save_post', 'wo_save_postdata');


// Theme:

<div class="blue">
  <?php
  $content = get_post_meta(get_the_ID(), 'wo_blue_box' , true );
    $content = htmlspecialchars_decode($content);
    $content = wpautop( $content );
    echo $content;
  ?>
</div>
查看更多
对你真心纯属浪费
3楼-- · 2019-01-17 21:40

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_decode($text), 'mettaabox_ID', $settings = array('textarea_name'=>'MyInputNAME') );
}

add_action( 'save_post', function($post_id) {
    if (!empty($_POST['MyInputNAME'])) {
        $datta=htmlspecialchars($_POST['MyInputNAME']); //make sanitization more strict !!
        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.

查看更多
欢心
4楼-- · 2019-01-17 21:47
【Aperson】
5楼-- · 2019-01-17 21:52

http://codex.wordpress.org/Function_Reference/wp_editor was by far the easiest method I found, built into Wordpress since 3.3 (so upgrade ;-) )

查看更多
smile是对你的礼貌
6楼-- · 2019-01-17 21:52

You can use the wordpress default text editor in the metabox using

add_action( 'edit_page_form', 'my_second_editor' );
function my_second_editor() {
    // get and set $content somehow...
    wp_editor( $content, 'mysecondeditor' );
}
查看更多
神经病院院长
7楼-- · 2019-01-17 21:55

First install TinyMCE Advanced plugin. Second add "theEditor" class to your textarea like this

<textarea  class="theEditor" name="custom_meta_box"></textarea>

Thats it ;)

Nabeel

查看更多
登录 后发表回答