Call TinyMCE in a WordPress plugin

2020-02-17 06:13发布

Is there a way to add TinyMCE into my own WordPress plugin?

I have a textarea in my back end script and want to make this area into a TinyMCE WYSIWYG editable field. Is there a way to do that?

wysiwyg demonstration screenshot

This code does not work for me:

<?php
    wp_tiny_mce(false,array("editor_selector" => "test"));
?>
<textarea class="test" id="test" name="test"></textarea>

It shows the javascript error

f is undefined

Firebug screenshot: TinyMCE error

This didn't work either:

<textarea class="theEditor" id="videogalerie-add_description" name="videogalerie-add_description"></textarea>

8条回答
别忘想泡老子
2楼-- · 2020-02-17 07:10

Following guides from here and there (found thanks to this), here's how I've managed to make something work on wordpress 3.0.5 :

<?php
add_action("admin_print_scripts", "js_libs");
function js_libs() {
    wp_enqueue_script('tiny_mce');
}
wp_tiny_mce( false , // true makes the editor "teeny"
    array(
        "editor_selector" => "tinymce_data"
    )
);
?>

<script type="text/javascript">
    jQuery(document).ready(function($) {
        $('a.toggleVisual').click(function() {
            console.log(tinyMCE.execCommand('mceAddControl', false, 'tinymce_data'));
        });
        $('a.toggleHTML').click(function() {
            console.log(tinyMCE.execCommand('mceRemoveControl', false, 'tinymce_data'));
        });
    });
</script>

<form method="post" action="">
<ul>
  <li>
    <span id="submit"><input class="button" type="submit"></span>
    <p id="toggle" align="right"><a class="button toggleVisual">Visual</a><a class="button toggleHTML">HTML</a></p>
  </li>
  <li><textarea style="width:100%;" class="tinymce_data" id="tinymce_data" name="tinymce_data"></textarea></li>
</ul>
</form>
查看更多
The star\"
3楼-- · 2020-02-17 07:13

Tested and working on wordpress 3.3.1

add to functions or plugin file.

<?php
    add_filter('admin_head','ShowTinyMCE');
    function ShowTinyMCE() {
        // conditions here
        wp_enqueue_script( 'common' );
        wp_enqueue_script( 'jquery-color' );
        wp_print_scripts('editor');
        if (function_exists('add_thickbox')) add_thickbox();
        wp_print_scripts('media-upload');
        if (function_exists('wp_tiny_mce')) wp_tiny_mce();
        wp_admin_css();
        wp_enqueue_script('utils');
        do_action("admin_print_styles-post-php");
        do_action('admin_print_styles');
    }
?>

for Adding new content..

<?php the_editor($id='content');?>

for editing my content

<?php the_editor($mySavedContent); ?>

this will include the entire rage of scripts / css and code needed to produce a tinyMCE textarea within either your plugin or template files..

hope this helps..

M

查看更多
登录 后发表回答