php 用 WordPress TinyMCE wp_editor() 替换 textarea
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20331501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replacing a textarea with WordPress TinyMCE wp_editor()
提问by henrywright
I am trying to replace a textarea with wp_editor()
我正在尝试用 wp_editor() 替换 textarea
My textarea form element looks like this:
我的 textarea 表单元素如下所示:
<textarea name="post_text" id="post_text" rows="3"><?php echo $content; ?></textarea>
Then I have:
然后我有:
wp_editor( $content, 'post_text' );
The problem I am getting is both the form textarea and the wp_editor textarea are outputted on the page. Why are both textareas displaying? I only need one textarea to display. Everything saves fine, I just have this problem of 2 textareas showing.
我遇到的问题是表单 textarea 和 wp_editor textarea 都在页面上输出。为什么两个文本区域都显示?我只需要一个 textarea 来显示。一切都保存得很好,我只是有 2 个文本区域显示的问题。
EDIT: Is it as simple as putting a display: none;on my form's textarea so just the wp_editor() textarea displays? That seems to work but feels a bit hackish.
编辑:它是否像display: none;在我的表单的 textarea 上放置一个那么简单,以便 wp_editor() textarea 显示?这似乎有效,但感觉有点hackish。
回答by henrywright
I found the solution. You can use a third parameter to pass an array of arguments. Now this is pretty obvious as outlined in the Codex: http://codex.wordpress.org/Function_Reference/wp_editor
我找到了解决方案。您可以使用第三个参数来传递参数数组。现在这很明显,如 Codex 中所述:http: //codex.wordpress.org/Function_Reference/wp_editor
What is a little confusing (the source of my problem) is $editor_id may only contain lowercase letters. So if your form processing script is looking for something with underscores in it (as mine was) then you'll need to do this:
有点令人困惑(我的问题的根源)是 $editor_id 可能只包含小写字母。因此,如果您的表单处理脚本正在寻找带有下划线的内容(就像我的一样),那么您需要这样做:
$settings = array( 'textarea_name' => 'post_text' )
wp_editor( $content, $editor_id, $settings );
Note you can't do this:
请注意,您不能这样做:
wp_editor( $content, 'post_text' );
Which is where I went wrong.
这是我出错的地方。
回答by Entity
If you put a text area in your code
如果您在代码中放置了一个文本区域
<textarea></textarea>
Then of course it's going to show up on the page, that's what it's supposed to do. Unless there's something I'm misunderstanding, I don't see how that doesn't make sense.
然后当然它会显示在页面上,这就是它应该做的。除非有什么我误解的东西,否则我不明白这有什么意义。
Like you suggested, I think this will do what you want:
就像你建议的那样,我认为这会做你想做的:
<textarea style="display:none" name="post_text" id="posttext" rows="3"><?php echo $content; ?></textarea>
It will still be there functionally, but invisible.
它仍然会在功能上存在,但不可见。
回答by Jadeye
Call your template pagewhere you wish to place tinyMCE, on that template pageplace a placeholdersuch as CONTENT_EDITORand use php str_replacefunction to add tinyMCEto that templatecontent:
调用template page你想要放置的地方tinyMCE,在那个template page地方,placeholder例如CONTENT_EDITOR并使用 phpstr_replace函数添加tinyMCE到该template内容:
function add_tinymce_to_page(){
$creatorHTML = file_get_contents(
'your-template-pafe.php',
TRUE
);
$editorHTML = generate_content_with_editor();
$creatorHTML = str_replace(
'CONTENT_EDITOR',
$editorHTML,
$creatorHTML
);
return $creatorHTML;
}
function generate_content_with_editor(){
ob_start();
wp_editor( '', 'tinymcecontenteditor' );
$editor_contents = ob_get_contents();
ob_get_clean();
return $editor_contents;
}
I use php's obso tinyMCEdoes not display before full page is rendered.
我使用php's obsotinyMCE在呈现整页之前不显示。
回答by jgangso
Instead of outputting a new textarea to the page (by wp_editor()) and hiding the original textarea with display: none;, one can do this:
与其将新的 textarea 输出到页面(by wp_editor())并使用 隐藏原始 textarea display: none;,不如这样做:
add_action('admin_init', '20331501_convert_textarea_to_wysiwyg');
function 20331501_convert_textarea_to_wysiwyg(){
wp_enqueue_editor();
add_action( 'admin_print_footer_scripts', function() {
?>
<script>
jQuery(function(){
wp.editor.initialize('the-id-of-your-textarea-without-the-#', {
setup: function (editor) {
editor.on('change', function () {
editor.save();
});
}
});
});
</script>
<?php
}, 10, 2 );
}
This code snippet converts the existingtextarea to wysiwyg. The editor.save()takes care of updating the textarea value so that it gets passed along when one submits the form. (credits to @Dan Malcolm)
此代码片段将现有文本区域转换为所见即所得。该editor.save()以便它能够沿着当一个提交表单传递负责更新textarea的价值。(归功于@Dan Malcolm)

