Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • 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 );
@ggedde
Copy link

ggedde commented Jun 10, 2024

For some reason the above didn't work, I think it might have something to do with the user_can function, or that I am using WP Multi-Site, or it doesn't work when using WP Gutenberg blocks. However the below does work for me with Gutenberg blocks on WP 6.4.3

add_filter(
	'map_meta_cap',
	function ($caps, $cap) {
		$user = wp_get_current_user();
		if ($cap === 'unfiltered_html' && !empty($user->roles) && (in_array('administrator', $user->roles, true) || in_array('editor', $user->roles, true))) {
			$caps = array('unfiltered_html');
		}
		return $caps;
	},
	10,
	2
);

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