1

I've got a multisite install and wp-cli set up and working. Optimally, what I'd need is a script to run that will generate a list of all of the sites on the multisite install along with the name of the active theme.

I can figure out how to run this on a single-site basis, but really what I need is the ability to generate the list.

We've got about 400+ sites within our network, growing weekly. Has anyone tackled this problem yet?


Update: I've got this mostly working now, though I feel like this could be better optimized.

I'm saving this as list_active_themes.sh and am outputting it as a JSON file by running sh list_active_themes.sh > active_themes.json.

The last piece is to remove the trailing comma that is the result of the 9th line below-- and me not knowing how to count the returned site results in order to do an if statement to not output the comma if it is the final one in the for loop.

Here's my code:

echo "{"
for site in $(wp site list --domain=sample.domain.com --field=url --quiet)
    do
        echo -e '\t"site":{'
            printf '\t\t"url":"%s",\n' "$site"
            echo -e '\t\t"theme":"'|tr '\n' ''
            wp theme list --status=active --field=name --url=$site|tr '\n' ''
            echo '"'
        echo -e '\n\t},'
    done
echo "}"
frogg3862
  • 491
  • 1
  • 4
  • 19
  • I'm just a bash guy, no idea of wp-cli. But if you can do something about a single site, you can surely do the same thing with multiple sites in a loop. – 4ae1e1 Nov 11 '15 at 00:44

1 Answers1

3

We can split the task in two basic operations:

  1. List all sites in a multisite install: wp site list
  2. Get a list of themes: wp theme list

With this information we can get a list of all site URLs in the network:

wp site list --field=url

Knowing the site URL, we're able to list the associated active theme:

wp theme list --status=active --url="<site_url>"

Now we can setup a basic bash script to loop through each site in the network and get the associated theme:

#!/bin/bash
for site in $(wp site list --field=url)
do
    wp theme list --status=active --url=$site
done

Save this to a file (e.g. list_active_themes.sh) and run it from the terminal:

sh list_active_themes.sh

So now let's return our own custom list containing site URL + theme name with a structure like:

Site: <site_url> Theme: <theme_name>

The according bash script could look something like this:

#!/bin/bash
for site in $(wp site list --field=url)
do
    echo Site:|tr '\n' ' '
    echo $site|tr '\n' ' '
    echo Theme:|tr '\n' ' '
    wp theme list --status=active --field=name --url=$site
done

(The expression |tr '\n' ' ' replaces the newline with a space, |tr -d '\n' completely drops the newline.)


Update:

Using --format=count option will return the total number of sites; we can use this value as a condition within the loop and generate a JSON-like structure (as suggested from your comment):

#!/bin/bash
COUNTER=1
SITES=$(wp site list --format=count)
echo "{"
for site in $(wp site list --field=url)
    do
        echo '\t"site":{'
            printf '\t\t"url":"%s",\n' "$site"
            echo '\t\t"theme":"'|tr -d '\n'
            wp theme list --status=active --field=name --url=$site|tr -d '\n'
            echo '"'
        echo '\n\t}'|tr -d '\n'
        if (($COUNTER != $SITES)); then
            echo ,
        fi
        COUNTER=$[$COUNTER +1]
    done
echo "\n}"

The script above will return something like this:

{
    "site":{
        "url":"http://site_one.dev/",
        "theme":"theme_one"
    },
    "site":{
        "url":"http://site_one.dev/",
        "theme":"theme_two"
    }
}
Community
  • 1
  • 1
Sven
  • 1,450
  • 3
  • 33
  • 58