WordPress Logo

WordPress Multisite unfiltered_html Capability

User roles other than Super Admin cannot be assigned the unfiltered_html capability in WordPress Multisite. You can set it, but then WordPress disables the capability after the fact.

This creates a unique WordPress challenge. Your site admins will not have the ability to add a hard coded iframe or embed to a post.

Solution

WordPress does not offer an easy function to find a users role. I have provided a function to achieve that as part of the solution.

/**
 * Simulate assigning the unfiltered_html capability to a role.
 *
 * @return void
 */
add_action('admin_init', 'tsg_kses_remove_filters');
function tsg_kses_remove_filters(): void
{
    if (tsg_user_has_role('editor', wp_get_current_user())) {
        kses_remove_filters();
    }
}

/**
 * Check if a user has a role.
 *
 * @param string $role
 * @param null|WP_User $user
 * @return bool
 */
function tsg_user_has_role(string $role = '', $user = null): bool
{
    if (is_object($user)) {
        $user = $user->ID;
    }

    $user = $user ? new WP_User($user) : wp_get_current_user();

    if (empty($user->roles)) {
        return false;
    }

    if (in_array($role, $user->roles)) {
        return true;
    }

    return false;
}