How to retain HTML5 Attributes on Markup

I have a page with certain html markup and element attributes as shown in the snippit below:

<ul class="accordion" data-accordion="">

When I edit the page as a user with the WP Administrator Role the <ul> remains intact upon page update. Perfect.

Yet when a user with WP Editor Role edits the page, the ul tag saves as follows:

<ul class="accordion">

Notice the data-accordion attribute is removed.

How can I get WordPress to retain markup tag attributes for users with the Editor role?

1 Answer
1

It is enough to add unfiltered_html capability to Editor role.

Add the following code into your current theme’s functions.php:

function wpse_change_capability() {
    $role = get_role( 'editor' );
    if ( ! $role->has_cap( 'unfiltered_html' ) )
        $role->add_cap( 'unfiltered_html' );
}
add_action( 'init', 'wpse_change_capability', 10 );

Login as the user with Editor role. Test it by editing any post / page. HTML markup will be preserved. Remove the code above from functions.php.

If you decide to remove unfiltered_html capability from Editor role, repeat steps described above, using this code:

function wpse_change_capability() {
    $role = get_role( 'editor' );
    if ( $role->has_cap( 'unfiltered_html' ) )
        $role->remove_cap( 'unfiltered_html' );
}
add_action( 'init', 'wpse_change_capability', 10 );

Leave a Comment