Dynamically control user capabilities on multisite with WordPress

Got an interesting question about dynamically altering capabilities depending on which site the user was on – the goal was to have a global site where everyone had some access, without having to manually add every user to the site in question.

This snippet below will give everyone trying to access blog id = 1 on the multisite the Contributor capabilities. You can easily tweak which site ID and which Role you want to give all users.

add_filter( 'user_has_cap', function($allcaps, $cap, $args) {

  //ID of the "global" site
  if(get_current_blog_id() === 1) {

    //Give everyone admin capabilities
    $admin_caps = get_role('contributor')->capabilities;
    $allcaps = array_merge($allcaps, $admin_caps);
  }

  return $allcaps;
}, 10, 3 );

2 thoughts on “Dynamically control user capabilities on multisite with WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *

Markdown is allowed in comments.