Administrator role in WordPress multisite is getting content stripped out when saving pages
Asked Answered
C

3

5

I have a WordPress multisite set up and am trying to save page content with data-attributes on some html tags. Super Users are able to save with no issue, however when Administrators or lower roles save, it strips out the data-attributes that are in the tag. Is there any way to allow other user roles to save data-attributes in the html?

Just to be clear, it's not the html tags themselves that get stripped, but the data attributes, like so:

<p data-item="1">String</p>

The above gets saved as:

<p>String</p>

This isn't a tinymce issue either, I scan switch back and forth between the WYSIWYG and the source view and it stays, it's only when I save the page that it gets stripped out, and only for users that are lower than Super User in a multisite.

Any help is appreciated, thanks!

Crispa answered 31/7, 2018 at 1:5 Comment(0)
G
5

What you're having an issue with is the unfiltered_html capability. If you read that codex link you'll notice the following:

Note: In WordPress Multisite, only Super Admins have the unfiltered_html capability.

To get around this you'll need to add the unfiltered_html capability to the administrator role. If you're unaware how to do that keep reading:

You'll need to make use of the add_cap() function. Something like the following should suffice. If your entire network uses one theme, you can stick the following code in the functions.php file of the active theme.

Otherwise, you may want to make use of a Must-Use Plugin - basically create a file like custom-functions.php, paste the following code in it, and put it in /wp-content/mu-plugins/ (create it if it doesn't already exist). This will make it a "Must-Use Plugin" which is always loaded no matter what, and can't be activated/deactivated.

function so_51604149_add_cap(){
    $role = get_role( 'administrator' );

    if( $role ){
        $role->add_cap( 'unfiltered_html' ); 
    }
}
add_action( 'init', 'so_51604149_add_cap' );

Alternatively, there are a medley of "user permission" and "user role" type plugins out there that may be able to help. The gist of you issue is just that Super Admins are the only role on WordPress MultiSite with the unfiltered_html capability.

Guild answered 31/7, 2018 at 2:30 Comment(0)
C
1

Xhynk led me to this solution, thanks!

The unfiltered_html capability is deprecated, so in order to simulate what that capability did, I added the following in my code:

add_action( 'init', 'kses_unfiltered_html' );
function kses_unfiltered_html() {
    $user = wp_get_current_user();

    if ( current_user_can('edit_pages') )
        kses_remove_filters();
}

This is what I needed, it allows any users who can edit pages to save content unfiltered. Others out there may need a role in the place of edit_pages, such as:

add_action( 'init', 'kses_unfiltered_html' );
function kses_unfiltered_html() {
    $user = wp_get_current_user();

    if ( current_user_can('administrator') )
        kses_remove_filters();
}
Crispa answered 31/7, 2018 at 10:31 Comment(0)
E
0

This filter worked for me:

// Add the unfiltered_html capability back in to WordPress 3.0 multisite. o(8MNTW9B2WUi(ITf8N&0rc$
function allow_unfiltered_html_multisite( $caps, $cap, $user_id, $args ) {
    if ( $user_id !== 0 && $cap === 'unfiltered_html' ) {
        $user_meta = get_userdata($user_id);
        if ( in_array( 'administrator', $user_meta->roles, true ) ) {
            // Re-add the cap
            unset( $caps );
            $caps[] = $cap;
        }
    }
    return $caps;
}
add_filter('map_meta_cap', 'allow_unfiltered_html_multisite', 10, 4 );```
Electrolier answered 4/3, 2019 at 16:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.