0

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.

1 Answer 1

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.

2
  • The "unfiltered html" capability is by default only granted to admins? I am currently an admin and this is a single site (not multisite) and it is stripping out the HTML tags. Does this need to be turned on explicitly @pat-j? Aug 24, 2021 at 1:27
  • 1
    Hmm, good question. I thought Admins were granted unfiltered_html. Can you edit your question, please, to include the code that generates your WP Editor in the backend? (If it's a 3rd-party plugin, you might need to ask their support team for assistance with this.)
    – Pat J
    Aug 24, 2021 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.