Bulk activate a theme on multisite

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

Topic bulk mysql multisite database themes Wordpress

Category Web


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

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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.