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

2 Answers 2

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
4
  • 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
    Commented Feb 26, 2019 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. Commented Feb 26, 2019 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
    Commented Mar 2, 2019 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 Commented Mar 2, 2019 at 8:48
2

If you have SSH access to the server, then I highly recommend using the command line Wordpress command line tool (wp cli). You can automate a lot of actions with this. Here are the docs on activating a theme.

Here's the code that I used. Worked through 10-15 sites in about 3 seconds. No need to add/delete code to any files, all done from the command line. Put this in a bash file (e.g. update_theme.sh), and run it with bash update_theme.sh:

SITES=$(wp site list --field=url)
theme="THEME_NAME_HERE"
for s in $SITES
do 
echo "Working on $s..."
wp theme activate "$theme" --url=$s
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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