如何在 Wordpress 元框中添加所见即所得编辑器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3493313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:37:29  来源:igfitidea点击:

How to add wysiwyg editor in Wordpress meta box

wordpresseditorfieldmeta-boxes

提问by DashaLuna

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?

我正在为我的自定义帖子类型创建一个元框。有多个字段我想使用所见即所得编辑器而不是<textarea>. 是否可以将多个编辑器添加到元框?

I would really appreciate your help!

我将衷心感谢您的帮助!

Many thanks. Dasha

非常感谢。大傻

回答by T.Todua

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=sanitize_text_field($_POST['MyInputNAME']);
        update_post_meta($post_id, 'SMTH_METANAME', $datta );
    }
}); 

P.S. MUST-Recommendation from my experience:

PS必须-根据我的经验推荐:

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

忘记添加自定义代码,使用高级自定义字段,它非常好,简化您的生活。

回答by Adam Sargant

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

http://codex.wordpress.org/Function_Reference/wp_editor是迄今为止我发现的最简单的方法,自 3.3 以来内置于 Wordpress(所以升级;-))

回答by WP Fan

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.

但是您需要用 nl2br() 函数替换演示文稿,因为自定义模板中的 textarea 有 toogle JS 问题,它会删除您的所有<P><br/>标签,因此会删除所有换行符。

回答by user3962908

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

回答by Akanksha Sharma

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

您可以使用 metabox 中的 wordpress 默认文本编辑器

add_action( 'edit_page_form', 'my_second_editor' );
function my_second_editor() {
    // get and set $content somehow...
    wp_editor( $content, 'mysecondeditor' );
}

回答by alesub

This did the trick for me:

这对我有用:

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

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

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

它基本上是用一个 id 创建你的 textarea,然后从 js 调用:

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

Hope it helps!

希望能帮助到你!

回答by blockhead

回答by Nabeel

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

首先安装 TinyMCE Advanced 插件。其次将“theEditor”类添加到您的文本区域,如下所示

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

Thats it ;)

就是这样 ;)

Nabeel

纳比尔