Php 在WordPress插件中调用TinyMCE

Php 在WordPress插件中调用TinyMCE,php,javascript,wordpress,plugins,tinymce,Php,Javascript,Wordpress,Plugins,Tinymce,有没有办法将TinyMCE添加到我自己的WordPress插件中 我的后端脚本中有一个textarea,希望将该区域设置为TinyMCE WYSIWYG可编辑字段。有办法吗 此代码不适用于我: <?php wp_tiny_mce(false,array("editor_selector" => "test")); ?> <textarea class="test" id="test" name="test"></textarea> Fireb

有没有办法将TinyMCE添加到我自己的WordPress插件中

我的后端脚本中有一个textarea,希望将该区域设置为TinyMCE WYSIWYG可编辑字段。有办法吗

此代码不适用于我:

<?php
    wp_tiny_mce(false,array("editor_selector" => "test"));
?>
<textarea class="test" id="test" name="test"></textarea>
Firebug屏幕截图:

这也不起作用:

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

下面的例子对我很有用。只需确保在$a[“elements”]变量中使用要选择的textarea的id即可

假设您有一个id为“intro”的文本区域:


?>

卡姆登已经回答了这个问题,但万一有人需要完整的代码。。。一定要钩住admin_head,钩住admin_enqueue_脚本会导致它在其他脚本(如jQuery)之前加载,所以它不会工作

add_action("admin_head","load_custom_wp_tiny_mce");
function load_custom_wp_tiny_mce() {

if (function_exists('wp_tiny_mce')) {

  add_filter('teeny_mce_before_init', create_function('$a', '
    $a["theme"] = "advanced";
    $a["skin"] = "wp_theme";
    $a["height"] = "200";
    $a["width"] = "800";
    $a["onpageload"] = "";
    $a["mode"] = "exact";
    $a["elements"] = "intro";
    $a["editor_selector"] = "mceEditor";
    $a["plugins"] = "safari,inlinepopups,spellchecker";

    $a["forced_root_block"] = false;
    $a["force_br_newlines"] = true;
    $a["force_p_newlines"] = false;
    $a["convert_newlines_to_brs"] = true;

    return $a;'));

 wp_tiny_mce(true);
}


}
然后在模板的某个位置插入一个常规文本区域:

<textarea id="intro"></textarea>

以下是来自和的指南(多亏了),以下是我如何在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>

jQuery(文档).ready(函数($){
$('a.toggleVisual')。单击(函数(){
log(tinyMCE.execCommand('mceAddControl',false,'tinyMCE_data');
});
$('a.toggleHTML')。单击(函数(){
log(tinyMCE.execCommand('mceRemoveControl',false,'tinyMCE_data');
});
});
  • VisualHTML


我也有类似的问题,而且
class=“theEditor”
也没有帮助我(起初)。我使用的自定义帖子类型不包括默认编辑器(即
supports
参数不包括
'editor'

这意味着WordPress没有包含TinyMCE代码。一旦我加上

add_action( 'admin_print_footer_scripts', 'wp_tiny_mce', 25 );

对于my functions.php(基于general template.php中的_editor函数中的代码),它运行良好(使用
class=“theEditor”
)。

测试并使用wordpress 3.3.1

添加到函数或插件文件

<?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');
    }
?>

用于添加新内容

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

用于编辑我的内容

<?php the_editor($mySavedContent); ?>

这将包括在插件或模板文件中生成tinyMCE文本区域所需的全部脚本/css和代码

希望这有帮助


这在WordPress 3.3中使用该函数更容易实现

我正在开发一个插件,它将向主题选项页面添加一个TinyMCE实例。下面是它的样子:

// Add TinyMCE visual editor
wp_editor( $content, $id );

其中,$content是存储的内容,$id是字段的名称。还可以通过选项自定义TinyMCE功能,有关更多详细信息,请查看WordPress Codex。

tiny mce功能wp_tiny_mce现在已去除润滑脂。对于最新的wordpress,您希望使用wp_编辑器

$initial_data='What you want to appear in the text box initially';
$settings = array(
'quicktags' => array('buttons' => 'em,strong,link',),
'text_area_name'=>'extra_content',//name you want for the textarea
'quicktags' => true,
'tinymce' => true
);
$id = 'editor-test';//has to be lower case
wp_editor($initial_data,$id,$settings);
有关更多说明,请参阅wordpress中的文档


我在这方面遇到了不少麻烦。在搜索了一整天并尝试了几十个代码示例之后,我无法获得Wordpress主题选项来将MCE值保存到数据库中。我尝试了一切,jQuery答案,隐藏字段,等等。这些都不适合我,可能是因为我在某个地方遗漏了一步

最后我找到了这个页面:

从Github下载并按指示安装(简单)。安装后,主题选项中的最后一个选项卡具有MCE编辑器。输入一些测试段落。现在打开下载中的index.php文件,查看如何在页面中包含每种内容的示例。例如,我打开footer.php并添加一些代码

我需要做的唯一编辑是:

<?php
$ft = of_get_option('example_editor', 'no entry');
$format_ft = wpautop( $ft, false );
echo ($format_ft);
?>


Wordpress中的函数wpautop()添加了段落标记,因为它们从未保存在wp数据库中。我把代码放在我的页脚中,以显示MCE的内容。

这对我的插件页面(wp 3.2.1)非常有用。我使用add_action('admin_head-mypluginpage'将其仅加载到我的插件页面编辑器函数wp_tiny_MCE(),在最新版本的wordpress中被删除…键“text_area_name”必须重写为“textarea_name”这确实是最新最简单的一个。用于一个新项目,效果非常好
$initial_data='What you want to appear in the text box initially';
$settings = array(
'quicktags' => array('buttons' => 'em,strong,link',),
'text_area_name'=>'extra_content',//name you want for the textarea
'quicktags' => true,
'tinymce' => true
);
$id = 'editor-test';//has to be lower case
wp_editor($initial_data,$id,$settings);
<?php
$ft = of_get_option('example_editor', 'no entry');
$format_ft = wpautop( $ft, false );
echo ($format_ft);
?>