WP如何添加富文本编辑器到文章字段或者后台字段

Published
2022-09-06
浏览次数 :  338

wp自带富文本编辑器的,也就是一直用的经典编辑器,代码是:wp_editor()

但是直接使用不行。这个函数调出的事一个textarea的编辑框。

我们先看这个函数的介绍:

wp_editor( string $content, string $editor_id, array $settings = array() )

$content是初始化内容,$editor_id就是字段的id,也就是Html里textarea和tinymce的ID,$settings是这函数的定义好的设置。

settings参数值:

$settings array Required

Array of editor arguments.

  • wpautopboolWhether to use wpautop() . Default true.
  • media_buttonsboolWhether to show the Add Media/other media buttons.
  • default_editorstringWhen both TinyMCE and Quicktags are used, set which editor is shown on page load. Default empty.
  • drag_drop_uploadboolWhether to enable drag & drop on the editor uploading. Default false.
    Requires the media modal.
  • textarea_namestringGive the textarea a unique name here. Square brackets can be used here. Default $editor_id.
  • textarea_rowsintNumber rows in the editor textarea. Default 20.
  • tabindexstring|intTabindex value to use. Default empty.
  • tabfocus_elementsstringThe previous and next element ID to move the focus to when pressing the Tab key in TinyMCE. Default ':prev,:next'.
  • editor_cssstringIntended for extra styles for both Visual and Text editors.
    Should include <style> tags, and can use “scoped”. Default empty.
  • editor_classstringExtra classes to add to the editor textarea element. Default empty.
  • teenyboolWhether to output the minimal editor config. Examples include Press This and the Comment editor. Default false.
  • dfwboolDeprecated in 4.1. Unused.
  • tinymcebool|arrayWhether to load TinyMCE. Can be used to pass settings directly to TinyMCE using an array. Default true.
  • quicktagsbool|arrayWhether to load Quicktags. Can be used to pass settings directly to Quicktags using an array. Default true.

用法举例:

$content = "";
$custom_editor_id = "editorid";
$custom_editor_name = "editorname";
$args = array(
		'media_buttons' => false, // This setting removes the media button.
		'textarea_name' => $custom_editor_name, // Set custom name.
		'textarea_rows' => get_option('default_post_edit_rows', 10), //Determine the number of rows.
		'quicktags' => false, // Remove view as HTML button.
	);
wp_editor( $content, $custom_editor_id, $args );

$content就是get_post_meta你这个meta字段的值。$editor_id 是你提交到数据的form里面的Teatarea的name的值,也就是post的name.

在我们的框架中直接在framework-options.php里添加:

//rich text
case ‘editor’ :
$content = ”;
if( !empty( $data ) ) $content = $data; elseif( !empty( $value[‘default’] ) ) $content = $value[‘default’];
wp_editor( $content, $option_name, array(‘textarea_rows’ => 6) );
break;

即可。


Top