1

How can I (easily) determine which of my wordpress sites are using a given plugin, on a Wordpress Multisite install?

Let's say I have 1,000 wordpress sites on a Wordpress Multisite install. I have 100 plugins.

For each plugin, I want to list all of the sites that are using that plugin.

In the GUI, I can only see how this can be done in about 1 million clicks. Is there a query I can run against the DB (and maybe cleanup with bash/awk/etc) that will automatically

  1. Get all of the plugins installed
  2. For each plugin, list all of the sites that use that plugin (by site-id is fine)

What's a fast way to determine all of the sites that use each of my installed plugins on Wordpress Multisite?

3
  • 2
    are you comfortable using WP CLI? If so there may be CLI based solutions here that don't require additional PHP/SQL
    – Tom J Nowell
    May 17, 2022 at 17:13
  • wp-cli is definitely a valid answer to this question. However, for this server, I can only use software that can be installed from a secure package manager with cryptographically signed manifests (which apt provides but composer, curl, git, brew, etc do not provide) May 18, 2022 at 7:30
  • So uploading or downloading wp-cli.phar as the wp cli homepage says in that environment is not an option? Even then nothing prevents you downloading the data and running WP CLI locally, and WP CLI may be a hard requirement as you mentioned 1k+ sites ( and ignoring that files and commits in git are all managed using a cryptographic hash chain )
    – Tom J Nowell
    May 18, 2022 at 13:23

2 Answers 2

2

You can list the sites a plugin has been activated on in the shell using WP CLI:

sites=$(wp site list --field=url)
for url in $sites; do
    if wp plugin is-active "YOURPLUGINNAME" --url="${url}" --network; then
        echo "${url}"
    fi
done

It will print out the URL of each site on its own line that has that plugin activated.

3
  • Unfortunately, this does not print the site if a plugin is enabled network-wide. May 21, 2022 at 10:35
  • 1
    @MichaelAltfield that was never mentioned in the original post, my answer tries to list the active plugins on a site as your question requested, and if a plugin is network activated then it is not activated on those sites (but gets loaded anyway). Remember, I'm not trying to give you the answer you want, I'm trying to answer the question you asked. If there are things you expect that aren't said in your question, you have to say them. Listing plugins activated network wide requires a separate and fundamentally different answer ( aka just list all sites )
    – Tom J Nowell
    May 23, 2022 at 11:41
  • I've updated my answer by adding --network as instructed by the official WP CLI docs for the plugin is-active command: developer.wordpress.org/cli/commands/plugin/is-active, but I would note this was not a part of the original question
    – Tom J Nowell
    May 23, 2022 at 11:48
0

The following code uses wp-cli and will iterate through all plugins and it will print all sites that use each of those plugins.

sites=$(wp --quiet site list --field=url 2> /dev/null)
plugins=$(wp --quiet plugin list --field=name 2> /dev/null)
for plugin in $plugins; do
   echo $plugin;

   for url in $sites; do
      wp --quiet plugin is-active "${plugin}" --url="${url}" --network 2> /dev/null
      if [[ $? -eq 0 ]]; then

         echo -e "\t${url}"

      fi
   done
    
done

Credit to @tom-j-nowell as the above code is a modified version of his answer, but this also loops through plugins.

4
  • Why would you redirect all error output 2> /dev/null? You should check of there are actually valid errors and display them in case. Mar 21 at 22:02
  • Because there are invalid errors :) My server has some hardening that makes wp-cli spew out a ton of "errors" to stderr that make the output unreadable. Honestly, nobody should enable ini_set(); it defeats the purpose of hardening PHP at all :D Mar 21 at 22:04
  • Well, this make this a very specific answer for yourself. Not something I would recommend people to copy and use like this. So wp-cli does not need ini_set but produces errors if its disabled? Mar 21 at 23:07
  • It prints a ton of (non-critical) errors on any basic hardening. So this cleans it up for anyone (not just me) who follows basic PHP security best-practices Mar 22 at 0:05

Your Answer

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

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