Adding rich text editor to my plugin

I’m trying to add a rich text editor to my own plugin.
So I googled it first of course and I found several answers on dev4press and Stack Overflow. This code displays nothing so I tried another solution. This displays a rich text editor like :

enter image description here

Read More

This seems to work fine, but as soon I switch to the WYSIWYG tab the text I inserted is deleted, I only see a blank field and I can’t type anything.

Is someone familiar with this problem?
Is this a bug or what can I do to make it work?

These scripts and styles are included in my plugin:

function highlights_style() {
    wp_enqueue_style('thickbox');
}

function highlights_script() {
    wp_register_script( 'jquery.js', HIGHLIGHTS_PLUGIN_URL . 'jquery.js', array('jquery','media-upload','thickbox'));
    wp_enqueue_script( 'jquery.js' );

    wp_enqueue_script('media-upload');
    wp_enqueue_script('thickbox');
    wp_enqueue_script( 'editor' );

    wp_enqueue_media();

    add_action( 'admin_head', 'wp_tiny_mce' );
}

And I use this to display the editor:

the_editor( '' , 'content' );

Related posts

Leave a Reply

4 comments

  1. wp_editor() outputs the textarea(html codes),
    so, maybe you dont need to use that in functions.php, but use inside the page’s source.
    (for example, in your plugin.php:

    <div class="blabla>
      <form action="" method="POST">
      <?php wp_editor( 'Hi,its content' , 'desired_id_of_textarea', $settings = array('textarea_name'=>'your_desired_name_for_$POST') ); ?> 
      <input type="submit">
      </form>
    </div>
    

    note: it may wont work for add_option function..

  2. You have unnecessary scripts loading.

    You are able to see the editor.. but it is not loading it’s javascript. You can tell because there are no buttons present. This is most likely because you have a javascript error.

    I would remove the unnecessary scripts you have enqueued.

    Then, check your browser console to see what errors or notices are generated.

  3. to properly use the wp_editor use it like this:

    // add the admin settings and such
    add_action('admin_init', 'wp_your_plugin_admin_init');
    function wp_your_plugin_admin_init(){
    register_setting( 'wp_your_plugin_settings', 'wp_your_plugin_settings', 'wp_your_plugin_settings_validate');
    add_settings_field('wp_your_plugin_user_custom_text', __('Enter your message','wp_your_plugin'), 'wp_your_plugin_user_custom_text', 'wp_your_plugin', 'wp_your_plugin_main');
    
    function wp_your_plugin_user_custom_text() {
    $options = get_option('wp_your_plugin_settings');
    $settings  = array('media_buttons' => true,'textarea_rows' => 5,'textarea_name' => 'user_custom_text');
    wp_editor( $options['user_custom_text'],'user_custom_text', $settings  );}  
    
    // validate  
    function wp_your_plugin_settings_validate() {
    $options = get_option('wp_your_plugin_settings');
    
    
    if ( empty($_POST['user_custom_text']) ){
        $options['user_custom_text'] =  __('Enter your own content, it will be below the original message','wp_your_plugin');// as set when the plugin activated
    }else{
        $options['user_custom_text'] =  wp_kses_post($_POST['user_custom_text']) ;}// u need to Sanitize to be able to get the media to work