Opened 4 years ago
Closed 4 years ago
#50483 closed enhancement (duplicate)
WYSIWYG editor: Use feature detection instead of User-Agent string
Reported by: | mallorydxw | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 5.0 |
Component: | Editor | Keywords: | |
Focuses: | Cc: |
Description
In both Gutenberg and the Classic Editor, the JavaScript for the WYSIWYG/visual editor is only loaded if user_can_richedit()
returns true. This function takes into account a user option, a filter, and the User-Agent
string the browser sends:
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 ( get_user_option( 'rich_editing' ) == 'true' || ! 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 ) && intval( $match[1] ) >= 534 ); } elseif ( $is_IE ) { $wp_rich_edit = ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Trident/7.0;' ) !== false ); } 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 ); }
We recently encountered this error when testing a new WordPress hosting environment which uses CloudFront. CloudFront by default overrides the User-Agent
header meaning we found that no users were able to edit posts except by editing raw HTML.
We were able to resolve this quickly, but I think WordPress should move on from User-Agent strings and on to using feature detection.
Feature detection is the best practice for this kind of thing. It gives more accurate results. And if you Google "cloudfront wordpress visual editor" or "cloudflare wordpress visual editor" you'll see that this is making WordPress harder to configure for a lot of people.
We've seen this problem at Human Made too with CloudFront stripping the user agent. It would definitely be preferable use feature detection instead of user agent sniffing.