Find all the WordPress we have

How many WordPress do you have? You know it? Sometimes I don’t… and that doubt generates a question for me: how can I know how many I have and if they are updated?

This tutorial has been created on a Clouding.io VPS. You can create your own VPS from 3€/month.

In addition, you have the possibility to create your VPS with the WordPress image in one click.

PARTNERSHIP

To use this tool we must have WP-CLI previously installed, and most likely you need to access the server as root, although a “normal” user should also be able to use it.

The only thing that we will have to update in the file is the complete main path of the server where to start searching, and that is why we should have all the sites restricted in a general folder, type /home/wordpress/, /data/ or /webs/, for example. In this example we are going to use the last one: /webs/.

First we will create our search file.

cd /webs/
touch wordpress-finder.sh
chmod +x wordpress-finder.sh
vim wordpress-finder.sh

And we will include this code:

#!/bin/bash

#####
HOST_PATH="/webs/"
#####

# PONIENDO AL DIA WP-CLI

echo ""
echo "Actualizando WP-CLI a la última versión:"
wp cli check-update --quiet --allow-root
wp cli update --quiet --allow-root
wp cli version --allow-root

# INSTALANDO WP-CLI FIND

echo ""
echo "Revisando el buscador de WP-CLI:"
if [ `wp package list --fields=name --format=csv --allow-root | grep 'wp-cli/find-command'` ]
then
  echo "Buscador de WP-CLI instalado."
else
  echo "Instalando buscador de WP-CLI:"
  wp package install wp-cli/find-command --quiet --allow-root
  if [ -z wp package list --fields=name --format=csv --allow-root | grep 'wp-cli/find-command' ]
  then
  echo "Buscador de WP-CLI instalado."
  else
    echo "Se ha producido un error. No se ha podido instalar el buscador de WP-CLI."
    echo "Prueba a instalarlo manualmente."
    echo ""
    echo "wp package install wp-cli/find-command"
    echo ""
    exit 1
  fi
fi

# BUSCANDO SITIOS

echo ""
echo "Buscando sitios WordPress:"

WP_DATA=`wp find $HOST_PATH --format=csv --fields=wp_path,version --allow-root`
WP_TOTAL=`echo "$WP_DATA" | wc -l`
if [ WP_TOTAL > 0 ]
then 
  let WP_TOTAL=WP_TOTAL-1
else
  WP_TOTAL=0
fi

echo "Se ha encontrado un total de ${WP_TOTAL} WordPress".

for WP_D in $WP_DATA
do
  echo ""
  echo "********************************************************************************"
  echo ""
  echo "WordPress"
  echo ""

  WP_PATH=`echo "$WP_D" | awk -F, 'NR { print $1 }'`
  echo "- Ruta:                ${WP_PATH}"

  WP_SITE_NAME=`wp option get blogname --path="${WP_PATH}" --allow-root`
  echo "- Nombre:              ${WP_SITE_NAME}"

  WP_SITE_URL=`wp option get siteurl --path="${WP_PATH}" --allow-root`
  echo "- URL:                 ${WP_SITE_URL}"

  WP_VERSION=`echo "$WP_D" | awk -F, 'NR { print $2 }'`
  echo "- Versión WP:          ${WP_VERSION}"

  WP_VERSION_UPD=`wp core check-update --path="${WP_PATH}" --format=csv --allow-root | awk FNR-1 | awk -F, 'NR { print $1 }'`
  if [ $WP_VERSION_UPD ]
  then
    echo "- Actualización:       ${WP_VERSION_UPD}"
  fi

  echo ""
  echo "Themes"
  echo ""

  WP_THEMES=`wp theme list --fields=name,update,version,update_version --format=csv --path="${WP_PATH}" --allow-root | awk FNR-1`
  WP_TOTAL_THEMES=`echo "$WP_THEMES" | wc -l`
  echo "- Temas:               ${WP_TOTAL_THEMES} temas".

  for WP_T in $WP_THEMES
  do
    WP_THEME_UPD=`echo "${WP_T}" | awk -F, 'NR { print $2 }'`
    if [ $WP_THEME_UPD == "available" ]
    then
      WP_THEME_NAME=`echo "${WP_T}" | awk -F, 'NR { print $1 }'`
      WP_THEME_ACT=`echo "${WP_T}" | awk -F, 'NR { print $3 }'`
      WP_THEME_NEW=`echo "${WP_T}" | awk -F, 'NR { print $4 }'`
      echo "- Tema actualizable:   ${WP_THEME_NAME} ${WP_THEME_ACT} -> ${WP_THEME_NEW}"
    fi
  done

  echo ""
  echo "Plugins"
  echo ""
  WP_PLUGINS=`wp plugin list --fields=name,update,version,update_version --format=csv --path="${WP_PATH}" --allow-root | awk FNR-1`
  WP_TOTAL_PLUGINS=`echo "$WP_PLUGINS" | wc -l`
  echo "- Plugins:             ${WP_TOTAL_PLUGINS} plugins".

  for WP_P in $WP_PLUGINS
  do
    WP_PLUGIN_UPD=`echo "${WP_P}" | awk -F, 'NR { print $2 }'`
    if [ $WP_PLUGIN_UPD == "available" ]
    then
      WP_PLUGIN_NAME=`echo "${WP_P}" | awk -F, 'NR { print $1 }'`
      WP_PLUGIN_ACT=`echo "${WP_P}" | awk -F, 'NR { print $3 }'`
      WP_PLUGIN_NEW=`echo "${WP_P}" | awk -F, 'NR { print $4 }'`
      echo "- Plugin actualizable: ${WP_PLUGIN_NAME} ${WP_PLUGIN_ACT} -> ${WP_PLUGIN_NEW}"
    fi
  done

  echo ""
  echo "********************************************************************************"

done

echo ""
echo " -- FIN --"
echo ""
echo ""

Once we have it, we can run the file

bash wordpress-finder.sh

Which will give us back something such that:

Actualizando WP-CLI a la última versión:
WP-CLI 2.5.0

Revisando el buscador de WP-CLI:
Buscador de WP-CLI instalado.

Buscando sitios WordPress:
Se ha encontrado un total de 2 WordPress.

********************************************************************************

WordPress

- Ruta:                /webs/example/www.example.com/
- Nombre:              Example
- URL:                 https://www.example.com
- Versión WP:          5.7.2
- Actualización:       5.8

Themes

- Temas:               2 temas.
- Tema actualizable:   twentytwentyone 1.3 -> 1.4

Plugins

- Plugins:             10 plugins.
- Plugin actualizable: autoptimize 2.8.4 -> 2.9.0
- Plugin actualizable: gutenberg 11.0.0 -> 11.1.0
- Plugin actualizable: redis-cache 2.0.18 -> 2.0.21
- Plugin actualizable: flush-opcache 4.1.0 -> 4.1.1
- Plugin actualizable: wp-super-cache 1.7.3 -> 1.7.4

********************************************************************************

********************************************************************************

WordPress

- Ruta:                /webs/example/test.example.com/
- Nombre:              Test example
- URL:                 https://test.example.com
- Versión WP:          5.8

Themes

- Temas:               1 temas.

Plugins

- Plugins:             8 plugins.

********************************************************************************

 -- FIN --

And, with this system, in addition to finding WordPress that we may have forgotten, we will also find those pending updates.

FINAL NOTE: This is one of my first most complex bash scripts I’ve ever done… so please, if you have any suggestions for improvement, please send them to me to improve the code.


About this document

This document is regulated by the EUPL v1.2 license, published in WP SysAdmin and created by Javier Casares. Please, if you use this content in your website, your presentation or any material you distribute, remember to mention this site or its author, and having to put the material you create under EUPL license.