如何在Wordpress元框中添加wysiwyg编辑器

Das*_*una 21 wordpress field editor meta-boxes

我正在为自定义帖子类型创建一个元框.有多个字段我想使用wysiwyg编辑器而不是<textarea>.可以在元框中添加多个编辑器吗?

我将衷心感谢您的帮助!

非常感谢.大傻

T.T*_*dua 33

这是完整的代码示例:

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 );
    }
}); 
Run Code Online (Sandbox Code Playgroud)

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

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

  • @DannyCoulombe没花太长时间.这也是一次很棒的学习经历!如果你很好奇,你可以查看[这里](https://github.com/MatthewKosloski/wp-metabox-constructor-class) (6认同)
  • 但是对于高级自定义字段,您必须为转发器字段支付 30.00 美元。我宁愿自己做。 (3认同)
  • @Matthew 您宁愿自己编写代码也不愿支付 30.00 美元?您认为实现这一目标需要多长时间? (2认同)
  • 'get_post_meta($ post'必须是'get_post_meta($ post-> ID') (2认同)

小智 28

到目前为止,http://codex.wordpress.org/Function_Reference/wp_editor是我发现的最简单的方法,从3.3开始构建到Wordpress中(所以升级;-))


use*_*908 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>
Run Code Online (Sandbox Code Playgroud)