Understanding State in WordPress Multisites

Instead of relying on switch_to_blog or reaching into the site from the root blog/elsewhere, consider instead using a queue/job system. This way you don’t limit the max number of sites you can have based on how many sites PHP can get through before the time execution limit.

In order to do this, you’re going to need a way to store the details of the job to be done ( assign role X to user Y ), as well as a cron job that runs regularly that can pick it up.

This way, the cron job runs in the context of the site, not the root blog, allowing you to use filters that plugins can hook into, specific to that site.

I would recommend using a custom post type to store the job entries, deleting the posts as the work is done by the cron job. This also gives you a free pending job list if you give declare the post type as 'public' => false but give it a UI anyway

tech