How to add wysiwyg editor in Wordpress meta box

天涯浪子 提交于 2019-11-27 01:54:06

问题


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


回答1:


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.




回答2:


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




回答3:


But you need to replace presentation with nl2br() function as textarea in custom templates have the toogle JS issue, that removes all your <P> and <br/> tags and therefore all line breaks.




回答4:


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' );
}



回答5:


// 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>



回答6:


Try the custom field template plugin http://wordpress.org/extend/plugins/custom-field-template/




回答7:


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




回答8:


This did the trick for me:

http://www.farinspace.com/multiple-wordpress-wysiwyg-visual-editors/

It's basically creating your textarea with an id, then calling from js:

tinyMCE.execCommand('mceAddControl', false, 'your_textarea_id');

Hope it helps!



来源:https://stackoverflow.com/questions/3493313/how-to-add-wysiwyg-editor-in-wordpress-meta-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!