A
A
Alexander Sobolev2018-06-06 11:57:34
WordPress
Alexander Sobolev, 2018-06-06 11:57:34

How to add TinyMCE text editor to plugin options page?

I am writing a plugin. There is a need to add TinyMCE to the plugin options page. I know what is done through wp_editor (), but how to add options to the "container" and assign text to a variable for subsequent saving and editing?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor Sokolov, 2018-06-06
@Harley347

What do you mean by "add options to "container""?
Option 1, via php:

$set_of_buttons = add_filter('mce_buttons', create_function('', "return ['bold', 'italic', 'underline', 'bullist', 'numlist', 'alignleft', 'aligncenter', 'alignright', 'undo', 'redo'];")); // можно указать кнопки, которые будут в редакторе
$editor_params = [
  'wpautop'       => 0, // автоматически оборачивать абзацы в теги <p>
  'media_buttons' => 0,
  'textarea_name' => 'post_content', // параметр name для textarea, который будет в $_POST при передаче на сервер
  'textarea_rows' => 10,
  'tinymce'       => $set_of_buttons, 
  'quicktags'     => 0,
  'drag_drop_upload' => 0
];
wp_editor( $content, $tiny_name, $editor_params ); // $content - сюда контент который сразу пихать в редактор; $tiny_name - имя для манипуляций через js; $editor_params - параметры

Option 2, markup via php, initialization via js
wp_enqueue_editor(); // подключает все скрипты и стили для tinyMCE
<textarea name="post-content" id="post-content" cols="30" rows="10"><?= $content ?></textarea>

Initialization
tinymce.init({
selector:'#post-content',
branding: false,
toolbar1: 'bold italic underline | bullist numlist | alignleft aligncenter alignright | undo redo', // кнопки
content_css: ['//fonts.googleapis.com/css?family=Open+Sans:400,600&subset=cyrillic'],  // подключаю шрифт покрасивше
menubar: false,
});

A complete list of options can be found at tinyMCE website, there are many of them.
An important point, before sending to the server, you need to save from the editor to the textarea, such a snippet:
tinymce.triggerSave();

A
Alexander Sobolev, 2018-06-06
@san_jorich

Container - option section on plugin settings page. The code below will be clearer, probably
Now I have a solution with textarea:

add_settings_section( 'now_open_section_widgetcontent', 'Содержание Виджетов', '', $now_open_page );

$now_open_field_params = array(
      'type'      => 'textarea', 
      'id'        => 'now_open_opentxt',
      'desc'      => 'Например:  "Мы Ждем Вас по адресу: Красная пл., 1.  Добро пожаловать! "', 
      'label_for' => 'Виджет "Рабочее время"',
      
  );
  add_settings_field( 'txt', 'Ваше приветствие ', 'now_open_option_display_settings', $now_open_page, 'now_open_section_widgetcontent', $now_open_field_params );

Well, the switch case, respectively:
switch ( $type ) {  
    case 'textarea':  
      $o[$id] = esc_attr( stripslashes($o[$id]) );
      echo "<textarea class='code large-text' cols='50' rows='5' type='text' id='$id' name='" . $option_name . "[$id]'>$o[$id]</textarea>";  
      echo ($desc != '') ? "<br /><span class='description'>$desc</span>" : "";  
    break;
  }

The question is how to replace the text field with a pretty editor and store its value in a plugin option variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question