返回介绍

add_editor_style()

发布于 2017-07-21 10:24:03 字数 7973 浏览 1191 评论 0 收藏 0

add_editor_style( array|string $stylesheet = 'editor-style.css' )

给默认的 TinyMCE 编辑器添加自定义的CSS 样式文件。


description

The parameter $stylesheet is the name of the stylesheet, relative to the theme root. It also accepts an array of stylesheets. It is optional and defaults to ‘editor-style.css’.

This function automatically adds another stylesheet with -rtl prefix, e.g. editor-style-rtl.css. If that file doesn’t exist, it is removed before adding the stylesheet(s) to TinyMCE. If an array of stylesheets is passed to add_editor_style(), RTL is only added for the first stylesheet.

Since version 3.4 the TinyMCE body has .rtl CSS class. It is a better option to use that class and add any RTL styles to the main stylesheet.


参数

$stylesheet
(array|string)
(Optional)
样式表的名称或其阵列,相对于主题目录。默认值:editor-style.css

源代码

File: wp-includes/theme.php

function add_editor_style( $stylesheet = 'editor-style.css' ) {
	add_theme_support( 'editor-style' );

	if ( ! is_admin() )
		return;

	global $editor_styles;
	$editor_styles = (array) $editor_styles;
	$stylesheet    = (array) $stylesheet;
	if ( is_rtl() ) {
		$rtl_stylesheet = str_replace('.css', '-rtl.css', $stylesheet[0]);
		$stylesheet[] = $rtl_stylesheet;
	}

	$editor_styles = array_merge( $editor_styles, $stylesheet );
}

更新日志

Versiondescription
3.0.0Introduced.

更多信息

Allows theme developers to link a custom stylesheet file to the TinyMCE visual editor. The function tests for the existence of the relative path(s) given as the $stylesheet argument against the current theme directory and links the file(s) on success. If no $stylesheet argument is specified, the function will test for the existence of the default editor stylesheet file, editor-style.css, against the current theme directory, and link that file on success.

If a child theme is used, both the current child and parent theme directories are tested and both the files with the same relative path are linked with this single call if they are found.

To link a stylesheet file from a location other than the current theme directory, such as under your plugin directory, use a filter attached to the mce_css hook instead.


相关函数

Uses

  • wp-includes/theme.php:add_theme_support()
  • wp-includes/load.php:is_admin()
  • wp-includes/l10n.php:is_rtl()

User Contributed Notes

Basic Example

Add the following to the functions.php file of your theme.

/**
 * Registers an editor stylesheet for the theme.
 */
function wpdocs_theme_add_editor_styles() {
    add_editor_style( 'custom-editor-style.css' );
}
add_action( 'admin_init', 'wpdocs_theme_add_editor_styles' );

Next, create a file named custom-editor-style.css in your themes root directory. Any CSS rules added to that file will be reflected within the TinyMCE visual editor. The contents of the file might look like this:

body#tinymce.wp-editor { 
    font-family: Arial, Helvetica, sans-serif; 
    margin: 10px; 
}

body#tinymce.wp-editor a {
    color:

If you want to add styles dynamically (eg. from theme mods) you can use the tiny_mce_before_init filter and add them to the content_style key.

add_filter('tiny_mce_before_init','wpdocs_theme_editor_dynamic_styles');
function wpdocs_theme_editor_dynamic_styles( $mceInit ) {
	$styles = 'body.mce-content-body { background-color:

Note that any new lines or double quotes should be removed or double escaped in your CSS.

Using Google Fonts

Google Fonts API provides a single URL for a CSS file that can include multiple variants of a type face, separated by commas. Commas in a URL need to be encoded before the string can be passed to add_editor_style.

/**
 * Registers an editor stylesheet for the current theme.
 */
function wpdocs_theme_add_editor_styles() {
    $font_url = str_replace( ',', '%2C', '//fonts.googleapis.com/css?family=Lato:300,400,700' );
    add_editor_style( $font_url );
}
add_action( 'after_setup_theme', 'wpdocs_theme_add_editor_styles' );

Choosing Styles Based on Post Type

To link a custom editor stylesheet file based on the post type being edited, you can use the following in the functions.php file of your theme. This assumes the stylesheet files with names in the form of editor-style-{post_type}.css are present directly under your theme directory.

/**
 * Registers an editor stylesheet for the current theme.
 *
 * @global WP_Post $post Global post object.
 */
function wpdocs_theme_add_editor_styles() {
	global $post;

	$my_post_type = 'posttype';

	// New post (init hook).
	if ( false !== stristr( $_SERVER['REQUEST_URI'], 'post-new.php' )
			&& ( isset( $_GET['post_type'] ) === true && $my_post_type == $_GET['post_type'] )
	) {
		add_editor_style( get_stylesheet_directory_uri() . '/css/editor-style-' . $my_post_type . '.css' );
	}

	// Edit post (pre_get_posts hook).
	if ( stristr( $_SERVER['REQUEST_URI'], 'post.php' ) !== false
			&& is_object( $post )
			&& $my_post_type == get_post_type( $post->ID )
	) {
		add_editor_style( get_stylesheet_directory_uri() . '/css/editor-style-' . $my_post_type . '.css' );
	}
}
add_action( 'init',          'wpdocs_theme_add_editor_styles' );
add_action( 'pre_get_posts', 'wpdocs_theme_add_editor_styles' );

Note that the pre_get_posts action hook is used to ensure that the post type is already determined but, at the same time, that TinyMCE has not been configured yet. That hook is not run when creating new posts, that is why we need to use it in combination with the init hook to achieve a consistent result.

The example above can be rewritten simpler:

function value( $ar, $key, $default = '' ) {
	if ( is_array( $ar ) && isset( $ar[ $key ] ) ) { return $ar[ $key ]; }
	//else

	return $default;
}

/**
 * Registers an editor stylesheet for the current theme.
 *
 * @global WP_Post $post Global post object.
 */
function wpdocs_theme_add_editor_styles() {
	global $pagenow;
    global $post;
    $my_post_type = 'posttype';
 
    if ( ( ( 'post-new.php' === $pagenow ) && ( value( $_GET, 'post_type' ) == $my_post_type ) ) ||   // New post (init hook)
         ( ( 'post.php' === $pagenow ) && is_object( $post ) && ( get_post_type( $post->ID ) == $my_post_type ) ) ) {  // Edit post (pre_get_posts hook).
        add_editor_style( get_stylesheet_directory_uri() . '/css/editor-style-' . $my_post_type . '.css' );
    }
}

add_action( 'admin_init',    'wpdocs_theme_add_editor_styles' );
add_action( 'pre_get_posts', 'wpdocs_theme_add_editor_styles' );

Reusing Your Theme Styles

You can reuse the styles from your theme stylesheet file in your custom editor stylesheet file using the @import CSS rule. Working on the previous example, put the following instead into the custom-editor-style.css file.

@import url( 'style.css' );

/* Add overwrites as needed so that the content of the editor field is attractive and not broken */
body { padding: 0; background: #FFF; }

If necessary, change ‘style.css’ to the path to your theme stylesheet, relative to the custom-editor-style.css file.

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

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

发布评论

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