Filtering User Capabilities in WordPress

WordPress multisite user permissions are much more restrictive than on WordPress single site. One such change is that on multisite only super-admin users have the ‘unfiltered_html’ capability. It is possible however to restore our desired user permissions on multisite using a filter on the 'map_meta_cap'hook.

map_meta_cap

The map_meta_cap hook can be used to provide additional capabilities to users who would otherwise not have them. There is further documentation on WordPress user roles and capabilities here: https://wordpress.org/documentation/article/roles-and-capabilities/.

Let’s say we would like for any user who can edit posts to also have the ability to use ‘unfiltered_html’ (not necessarily recommended, just the example used here). We can do this by adding a filter to 'map_meta_cap':

/**
 * Enable unfiltered_html capability for some users
 *
 * @param  array  $caps    user's capabilities
 * @param  string $cap     capability type
 * @param  int    $user_id the user ID
 * @return array  $caps    The modified user capabilities
 */
function nwd_unfiltered_html_capability( $caps, $cap, $user_id ) {

  if ( 'unfiltered_html' === $cap && user_can( $user_id, 'edit_posts' ) ) {
    $caps = array( 'unfiltered_html' );
  }

  return $caps;
}

add_filter( 'map_meta_cap', 'nwd_unfiltered_html_capability', 1, 3 );

Additional Resources

If WordPress filters seem mysterious (as they once did to me), have a look at this article https://neilsonwebdesign.com/wordpress-add-filter-and-apply-filters/, which explains filters in more detail.

the author profile picture

Ryan Neilson is a software developer from Nova Scotia, Canada, where he lives with his wife and their menagerie of pets. He has worked extensively with PHP, WordPress, JavaScript, React, SCSS and CSS. Ryan can be found professionally at X-Team, where he works as a lead software engineer.