简体   繁体   English

用 WordPress TinyMCE wp_editor() 替换 textarea

[英]Replacing a textarea with WordPress TinyMCE wp_editor()

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.我遇到的问题是表单 textarea 和 wp_editor textarea 都在页面上输出。 Why are both textareas displaying?为什么两个文本区域都显示? I only need one textarea to display.我只需要一个 textarea 来显示。 Everything saves fine, I just have this problem of 2 textareas showing.一切都保存得很好,我只是有 2 个文本区域显示的问题。

EDIT: Is it as simple as putting a display: none;编辑:是否像放置display: none;一样简单display: none; on my form's textarea so just the wp_editor() textarea displays?在我的表单的 textarea 上,所以只显示 wp_editor() textarea? That seems to work but feels a bit hackish.这似乎有效,但感觉有点hackish。

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.有点令人困惑(我的问题的根源)是 $editor_id 可能只包含小写字母。 So if your form processing script is looking for something with underscores in it (as mine was) then you'll need to do this:因此,如果您的表单处理脚本正在寻找带有下划线的内容(就像我的一样),那么您需要这样做:

$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.这是我出错的地方。

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.它仍然会在功能上存在,但不可见。

The following PHP code turns into a textarea with the name & id "expirationErrorMessage".以下 PHP 代码变成了一个名称和 ID 为“expirationErrorMessage”的文本区域。

$id = "expirationErrorMessage";
$name = 'expirationErrorMessage';
$content = esc_textarea( stripslashes( $yourtext ) );
$settings = array('tinymce' => true, 'textarea_name' => "expirationErrorMessage");
wp_editor($content, $id, $settings);

Instead of outputting a new textarea to the page (by wp_editor() ) and hiding the original textarea with display: none;而不是将新的文本区域输出到页面(通过wp_editor() )并使用display: none;隐藏原始文本区域display: none; , one can do this: ,可以这样做:

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 existing textarea to wysiwyg.此代码片段将现有文本区域转换为所见即所得。 The editor.save() takes care of updating the textarea value so that it gets passed along when one submits the form. editor.save()负责更新 textarea 值,以便在提交表单时传递它。 ( credits to @Dan Malcolm ) 归功于@Dan Malcolm

Call your template page where you wish to place tinyMCE , on that template page place a placeholder such as CONTENT_EDITOR and use php str_replace function to add tinyMCE to that template content:打电话给你的template page ,你希望的地方tinyMCE ,对template page地方placeholder ,如CONTENT_EDITOR和使用PHP的str_replace功能添加tinyMCEtemplate内容:

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 ob so tinyMCE does not display before full page is rendered.我使用phpob因此tinyMCE在呈现完整页面之前不会显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM