返回介绍

wp_editor()

发布于 2017-09-11 11:54:39 字数 5047 浏览 1474 评论 0 收藏 0

wp_editor( string $content,  string $editor_id,  array $settings = array() )

Renders an editor.


description

Using this function is the proper way to output all needed components for both TinyMCE and Quicktags. _WP_Editors should not be used directly. See https://core.trac.wordpress.org/ticket/17144.

NOTE: Once initialized the TinyMCE editor cannot be safely moved in the DOM. For that reason running wp_editor() inside of a meta box is not a good idea unless only Quicktags is used. On the post edit screen several actions can be used to include additional editors containing TinyMCE: ‘edit_page_form’, ‘edit_form_advanced’ and ‘dbx_post_sidebar’. See https://core.trac.wordpress.org/ticket/19173 for more information.


参数

$content

(string) (Required) Initial content for the editor.

$editor_id

(string) (Required) HTML ID attribute value for the textarea and TinyMCE. Can only be /[a-z]+/.

$settings

(array) (Optional) See _WP_Editors::editor().

Default value: array()


源代码

File: wp-includes/general-template.php

function wp_editor( $content, $editor_id, $settings = array() ) {
	if ( ! class_exists( '_WP_Editors', false ) )
		require( ABSPATH . WPINC . '/class-wp-editor.php' );
	_WP_Editors::editor($content, $editor_id, $settings);
}

更新日志

Versiondescription
3.3.0Introduced.

相关函数

Uses

  • wp-includes/class-wp-editor.php: _WP_Editors::editor()

Used By

  • wp-admin/includes/class-wp-press-this.php: WP_Press_This::html()
  • wp-admin/includes/template.php: wp_comment_reply()
  • wp-admin/includes/media.php: edit_form_image_editor()
  • wp-includes/deprecated.php: the_editor()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Codex

    Display an empty editor using the default settings

    
    $content   = '';
    $editor_id = 'mycustomeditor';
    
    wp_editor( $content, $editor_id );
    
  2. The $settings array is actually described on the [_WP_Editors::parse_settings()](https://developer.wordpress.org/reference/classes/_wp_editors/parse_settings/) method

    Fill an editor with the content for a particular post

    
    $post_id   = 51;
    $post      = get_post( $post_id, OBJECT, 'edit' );
    $content   = $post->post_content;
    $editor_id = 'editpost';
    
    wp_editor( $content, $editor_id );
    

    Modify the editor’s default settings when initializing it
    You can pass an array of one or more settings to modify for this editor instance, such as hiding the insert media buttons.

    
    $content   = '';
    $editor_id = 'mycustomeditor';
    $settings  = array( 'media_buttons' => false );
    
    wp_editor( $content, $editor_id, $settings );
    

    To edit tinymce Visual Buttons, you should use toolbar instead of TinyMCE documentation’s theme_advanced_buttons attribute:

    $args = array(
        'tinymce'       => array(
            'toolbar1'      => 'bold,italic,underline,separator,alignleft,aligncenter,alignright,separator,link,unlink,undo,redo',
            'toolbar2'      => '',
            'toolbar3'      => '',
        ),
    );
    wp_editor( $content, $editor_id, $args );

    Get the wp_editor through AJAX Call,

    
    add_action( 'wp_ajax_bc_ajax_request', 'bc_ajax_request_fn' ); 
    function bc_ajax_request_fn(){
    $html ='';
    $html .= bc_get_wp_editor('Test Message','primary_editor',array());
    return $html;
    }
    
    function bc_get_wp_editor( $content = '', $editor_id, $options = array() ) {
    	ob_start();
    
    	wp_editor( $content, $editor_id, $options );
    
    	$temp = ob_get_clean();
    	$temp .= \_WP_Editors::enqueue_scripts();
    	$temp .= print_footer_scripts();
    	$temp .= \_WP_Editors::editor_js();
    
    	return $temp;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文