Useful Snippets

Welcome!


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

Are your smart devices spying on you? Make better purchasing choices and find products that respect your privacy at Unwanted.cloud

RSS Latest posts from my personal blog


Most viewed posts


Subscribe to RSS feed


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