通过wp_EDITOR将该文本区域转换为富html格式

时间:2019-07-03 作者:Laura

我在页面中有一个texarea,它的工作方式很有魅力,但我想将该区域转换为一个html丰富的wp\\U编辑器。有人能告诉我如何将这个文本区域转换为wp\\U编辑器字段吗??谢谢

 <textarea id="yith_vendor_biografia" name="yith_vendor_data[biografia]" rows="10" cols="50" class="large-text" placeholder="Write your mission statement here"><?php echo esc_textarea( stripslashes( $vendor->biografia ) ) ?></textarea>

1 个回复
SO网友:majick

基本上有两种选择,使用PHP或Javascript。

对于PHP,您可以使用wp_editor 函数为您输出textarea,并使用Javascript转换现有的textareawp.editor.initialize.

注意,每种方法的设置略有不同。

PHP

wp_editor function in Codex

$id = \'yith_vendor_biografia\';
$content = esc_textarea( stripslashes( $vendor->biografia ) );
$name = \'yith_vendor_data[biografia]\';
$settings = array(\'tinymce\' => true, \'textarea_name\' => $name);
wp_editor($content, $id, $settings);
The$settings 还可以选择更详细地配置阵列:

$settings =   array(
    \'wpautop\' => true,              // Whether to use wpautop for adding in paragraphs. Note that the paragraphs are added automatically when wpautop is false.
    \'media_buttons\' => true,        // Whether to display media insert/upload buttons
    \'textarea_name\' => $name,       // The name assigned to the generated textarea and passed parameter when the form is submitted.
    \'textarea_rows\' => 10,          // The number of rows to display for the textarea
    \'tabindex\' => \'\',               // The tabindex value used for the form field
    \'editor_css\' => \'\',             // Additional CSS styling applied for both visual and HTML editors buttons, needs to include <style> tags, can use "scoped"
    \'editor_class\' => \'\',           // Any extra CSS Classes to append to the Editor textarea
    \'teeny\' => false,               // Whether to output the minimal editor configuration used in PressThis
    \'dfw\' => false,                 // Whether to replace the default fullscreen editor with DFW (needs specific DOM elements and CSS)
    \'tinymce\' => true,              // Load TinyMCE, can be used to pass settings directly to TinyMCE using an array
    \'quicktags\' => true,            // Load Quicktags, can be used to pass settings directly to Quicktags using an array. Set to false to remove your editor\'s Visual and Text tabs.
    \'drag_drop_upload\' => false     // Enable Drag & Drop Upload Support (since WordPress 3.9)
);

Javascript

wp.editor.initialize in the Codex

<script>settings = { tinymce: true, quicktags: true }
wp.editor.initialize(\'yith_vendor_biografia\', settings);</script>
您还可以配置任何javascript设置,以下是这些设置的完整列表:

settings = {

    tinymce: {
        wpautop  : true,
        theme    : \'modern\',
        skin     : \'lightgray\',
        language : \'en\',
        formats  : {
            alignleft  : [
                { selector: \'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li\', styles: { textAlign: \'left\' } },
                { selector: \'img,table,dl.wp-caption\', classes: \'alignleft\' }
            ],
            aligncenter: [
                { selector: \'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li\', styles: { textAlign: \'center\' } },
                { selector: \'img,table,dl.wp-caption\', classes: \'aligncenter\' }
            ],
            alignright : [
                { selector: \'p,h1,h2,h3,h4,h5,h6,td,th,div,ul,ol,li\', styles: { textAlign: \'right\' } },
                { selector: \'img,table,dl.wp-caption\', classes: \'alignright\' }
            ],
            strikethrough: { inline: \'del\' }
        },
        relative_urls       : false,
        remove_script_host  : false,
        convert_urls        : false,
        browser_spellcheck  : true,
        fix_list_elements   : true,
        entities            : \'38,amp,60,lt,62,gt\',
        entity_encoding     : \'raw\',
        keep_styles         : false,
        paste_webkit_styles : \'font-weight font-style color\',
        preview_styles      : \'font-family font-size font-weight font-style text-decoration text-transform\',
        tabfocus_elements   : \':prev,:next\',
        plugins    : \'charmap,hr,media,paste,tabfocus,textcolor,fullscreen,wordpress,wpeditimage,wpgallery,wplink,wpdialogs,wpview\',
        resize     : \'vertical\',
        menubar    : false,
        indent     : false,
        toolbar1   : \'bold,italic,strikethrough,bullist,numlist,blockquote,hr,alignleft,aligncenter,alignright,link,unlink,wp_more,spellchecker,fullscreen,wp_adv\',
        toolbar2   : \'formatselect,underline,alignjustify,forecolor,pastetext,removeformat,charmap,outdent,indent,undo,redo,wp_help\',
        toolbar3   : \'\',
        toolbar4   : \'\',
        body_class : \'id post-type-post post-status-publish post-format-standard\',
        wpeditimage_disable_captions: false,
        wpeditimage_html5_captions  : true

    },
    quicktags   : true,
    mediaButtons: true

}

Source

还请注意,如果您试图在站点前端使用这些资源,则需要将该上下文的编辑器资源排队wp_enqueue_editor(); (如果需要媒体按钮,还需要wp_enqueue_media();)

相关推荐

有没有可以根据主题编辑WordPress站点的WYSIWYG工具?

我梦想有一个wordpress仪表板,主要是Wysiwyg,让用户根据给定的主题编辑网站。事实上,我知道这在技术上是可能的,因为我在这个工具中看到了这一点:http://visualwebsiteoptimizer.com/features.php (参见视频;它不是wordpress工具,但它展示了如何使用Wysiwyg界面编辑网页底层代码)。例如,如果我使用WooThemes优化,仪表板将显示主页,并允许我编辑标题、按钮中的文本、特色图像等。有人知道能做到这一点的工具吗?或者有什么提示我怎么去那里