user_can_richedit()
云策文档标注
概述
user_can_richedit() 函数用于判断用户是否可以访问可视化编辑器,基于用户设置和浏览器支持情况。
关键要点
- 检查用户是否启用富文本编辑(通过 get_user_option('rich_editing'))和浏览器是否支持
- 返回布尔值:true 表示用户可访问可视化编辑器,false 表示不可访问
- 内部使用全局变量(如 $wp_rich_edit, $is_safari 等)和服务器环境变量(HTTP_USER_AGENT)进行判断
- 提供 apply_filters('user_can_richedit', $wp_rich_edit) 钩子,允许开发者过滤结果
- 与 wp_is_mobile(), get_user_option(), is_user_logged_in() 等函数相关
代码示例
if ( user_can_richedit() ) {
wp_editor( $content, 'mycustomeditor' );
} else {
echo __( 'You cannot use the rich editor', 'textdomain' );
}
原文内容
Determines whether the user can access the visual editor.
Description
Checks if the user can access the visual editor and that it’s supported by the user’s browser.
Source
function user_can_richedit() {
global $wp_rich_edit, $is_gecko, $is_opera, $is_safari, $is_chrome, $is_IE, $is_edge;
if ( ! isset( $wp_rich_edit ) ) {
$wp_rich_edit = false;
if ( 'true' === get_user_option( 'rich_editing' ) || ! is_user_logged_in() ) { // Default to 'true' for logged out users.
if ( $is_safari ) {
$wp_rich_edit = ! wp_is_mobile() || ( preg_match( '!AppleWebKit/(d+)!', $_SERVER['HTTP_USER_AGENT'], $match ) && (int) $match[1] >= 534 );
} elseif ( $is_IE ) {
$wp_rich_edit = str_contains( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0;' );
} elseif ( $is_gecko || $is_chrome || $is_edge || ( $is_opera && ! wp_is_mobile() ) ) {
$wp_rich_edit = true;
}
}
}
/**
* Filters whether the user can access the visual editor.
*
* @since 2.1.0
*
* @param bool $wp_rich_edit Whether the user can access the visual editor.
*/
return apply_filters( 'user_can_richedit', $wp_rich_edit );
}
Hooks
- apply_filters( ‘user_can_richedit’, bool $wp_rich_edit )
-
Filters whether the user can access the visual editor.
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |
Skip to note 2 content
Codex
Example
Display an editor if the user can use it:
if ( user_can_richedit() ) { wp_editor( $content, 'mycustomeditor' ); } else { echo __( 'You cannot use the rich editor', 'textdomain' ); }