Useful Snippets

Useful Snippets


This blog is used to collect useful snippets related to Linux, PHP, MySQL and more. Feel free to post comments with improvements or questions!

July 2020
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  

Categories


Dynamically control user capabilities on multisite with WordPress

Stanislav KhromovStanislav Khromov

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 );
PHP

Web Developer at Aftonbladet (Schibsted Media Group)
Any opinions on this blog are my own and do not reflect the views of my employer.
LinkedIn
Twitter
WordPress.org Profile
Visit my other blog

Comments 2