Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kellenmace/9e6a6fbb92ec75940f23d2a6f01c9b59 to your computer and use it in GitHub Desktop.
Save kellenmace/9e6a6fbb92ec75940f23d2a6f01c9b59 to your computer and use it in GitHub Desktop.
Add unfiltered_html Capability to Admins or Editors in WordPress Multisite
<?php
/**
* Enable unfiltered_html capability for Editors.
*
* @param array $caps The user's capabilities.
* @param string $cap Capability name.
* @param int $user_id The user ID.
* @return array $caps The user's capabilities, with 'unfiltered_html' potentially added.
*/
function km_add_unfiltered_html_capability_to_editors( $caps, $cap, $user_id ) {
if ( 'unfiltered_html' === $cap && user_can( $user_id, 'editor' ) ) {
$caps = [ 'unfiltered_html' ];
}
return $caps;
}
add_filter( 'map_meta_cap', 'km_add_unfiltered_html_capability_to_editors', 1, 3 );
@filak
Copy link

filak commented Apr 7, 2020

This used to work well in WP 4 but in WP 5.3 this capability has not been effective anymore.

Do you heva any tips how to enable this in WP 5 ?

@kellenmace
Copy link
Author

@filak No, I'm not sure. If you figure it out, please drop a comment here and I'll update the blog post that goes along with this gist (https://kellenmace.com/add-unfiltered_html-capability-to-admins-or-editors-in-wordpress-multisite/) with the new method. Thanks.

@innocenat
Copy link

innocenat commented Apr 26, 2020

I am able to get it it work with this code in WP 5.4. Maybe you just need to add administrator? (I don't think my implementation of 4 arguments affect the result)

<?php

function multisite_restore_unfiltered_html( $caps, $cap, $user_id, ...$args ) {
	if ( 'unfiltered_html' === $cap && (user_can( $user_id, 'editor' ) || user_can( $user_id, 'administrator' ) ) ) {
		$caps = array( 'unfiltered_html' );
	}

	return $caps;
}

add_filter( 'map_meta_cap', 'multisite_restore_unfiltered_html', 1, 4 );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment