1

I have a multisite and a new theme that needs to be activated across all subsites. I found an answer by fuxia from 2012 and would like to know (1) if there have been changes in WordPress since that time that would require an update to this filter, and (2) what other methods exist to accomplish this task, like using the switch_theme function or using MySQL if it is about updating an option.

Reference: Changing Multisite themes on mass

0

Fuxia was using update_option in her answer and it may work, but I wouldn't recommend that - it isn't the polite way of setting a theme.

So how I would approach that problem?

First of all, you want to perform this action only once and for all sites. So there is no point in using any hooks. Just run the code once and then delete it.

And that code should loop through all sites and set their theme:

function switch_all_multisite_themes() {
    foreach ( get_sites() as $site ) {
        switch_to_blog( $site->blog_id );

        switch_theme( 'theme' ); // for example 'twentyten'

        restore_current_blog();
    }
}
switch_all_multisite_themes();  // run this function only once
  • Thanks for the reply Krzysiek. Should I put this function in functions.php or make a separate php file to run it? The function will be looping through 200+ subsites so I want to put it where it can only be run once, and also give it enough time to run before deleting it. – cseto Feb 26 at 8:07
  • You can put it in functions.php of active theme or add it as a plugin. The only thing you should remember is that you should remove that code after you run it. – Krzysiek Dróżdż Feb 26 at 8:17
  • For anyone else using this code, I'd check your max execution time for scripts. I ran it and noticed that it stopped halfway through my 200+ subsites. I suspect it's my max execution time. – cseto Mar 2 at 8:23
  • @cseto yes. If there are a lot of sites, you should run it in batches. But you do it only once and manually, so it’s not a problem – Krzysiek Dróżdż Mar 2 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.