plugins - Default Wordpress WP Editor removing style tags and html tag

admin2025-01-07  7

I am working inside the WP Admin area and there is a textarea that is using the default WP Editor, not TinyMCE. The WP Editor is removing the <style> tag and also other HTML tags. I am calling it WP Editor, but maybe it's official know as something different? It's the editor with the two tabs in the right corner (Visual,Text)

Is there a way to prevent this Editor from removing all HTML tags?

The content I am adding in the WP Editor is being used in outgoing emails so it's really a full HTML document which is why I am trying to preserve the formatting and html tags.

I notice when I switch from the "Text" tab to the "Visual" tab many HTML tags are stripped.

I am working inside the WP Admin area and there is a textarea that is using the default WP Editor, not TinyMCE. The WP Editor is removing the <style> tag and also other HTML tags. I am calling it WP Editor, but maybe it's official know as something different? It's the editor with the two tabs in the right corner (Visual,Text)

Is there a way to prevent this Editor from removing all HTML tags?

The content I am adding in the WP Editor is being used in outgoing emails so it's really a full HTML document which is why I am trying to preserve the formatting and html tags.

I notice when I switch from the "Text" tab to the "Visual" tab many HTML tags are stripped.

Share Improve this question asked Aug 23, 2021 at 19:35 user1669296user1669296 1192 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

WordPress has a user capability called unfiltered_html. Any user with the unfiltered_html capability can post any HTML they want, including <script> tags.

Due to security concerns, the unfiltered_html capability is—by default—only granted to Administrators (in WordPress Multisite, it's only granted to Super Administrators).

If you need to add the unfiltered_html capability to a non-(Super-)Admin user, you can use WP_User::add_cap() to add it to a user:

add_action( 'admin_init', 'wpse_393657_grant_user_unfiltered_html' );
function wpse_393657_grant_user_unfiltered_html() {
    $user = get_user_by( 'login', 'my_username' );
    if ( ! user_can( $user, 'unfiltered_html' ) ) {
        $user->add_cap( 'unfiltered_html', true );
    }
}

...or WP_Role::add_cap() to add it to a Role (eg, Editor).

add_action( 'admin_init', 'wpse_393657_grant_role_unfiltered_html' );
function wpse_393657_grant_role_unfiltered_html() {
    $role = get_role( 'editor' );
    if ( ! $role->has_cap( 'unfiltered_html' ) ) {
        $role->add_cap( 'unfiltered_html', true );
    }
} 

(These code snippets are untested, and are meant to provide a starting point for you.)

Note that the capability assignments are permanent (ie, written to the database), and that the user or role will retain the unfiltered_html capability until/unless you revoke it.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262609a827.html

最新回复(0)