Suppress PHP deprecated warnings from wp-cli

This is a follow up to my previous article on automatically updating WordPress using wp-cli. A number of the sites that I maintain run on servers that have been upgraded to PHP 8.x, but some of their plugins and themes include deprecated code. As a result, every time that cron runs to check for updates, I get a load of PHP Deprecated warnings. Searching online suggested a few fixes, but none of them worked for me, so I developed a different workaround.

Things that didn’t work:

  • Putting error_reporting = E_ALL & ~E_DEPRECATED in /etc/php/8.x/cli/php.ini
  • Calling wp-cli with php -d error_reporting=24575 /usr/bin/wp
  • Setting WP_DEBUG = false in the site’s wp-config.php

In the end I customised my wrapper script to filter out the problem lines using grep. Here’s the modified version:

#!/bin/sh
# simple script to update a wordpress install (core, plugins and themes) using wp-cli.
# usually we expect to run this from cron.

if [ $# -ne 1 ]
then
    echo "Usage: $0 </path/to/wordpress/install>" >&2
    exit 1
fi
if [ ! -d "$1" ]
then
    echo "$1 does not appear to be a directory. Exiting." >&2
    exit 2
fi
if [ ! -f "$1"/wp-config.php ]
then
    echo "$1/wp-config.php does not exist. Did you specify the path to a WordPress install?" >&2
    exit 3
fi
wp core update --path="$1" --quiet 2>&1 | grep -v 'PHP Deprecated:'
wp plugin update --all --path="$1" --quiet 2>&1 | grep -v 'PHP Deprecated:'
wp theme update --all --path="$1" --quiet 2>&1 | grep -v 'PHP Deprecated:'
wp language core update --path="$1" --quiet 2>&1 | grep -v 'PHP Deprecated:'
wp language plugin update --all --path="$1" --quiet 2>&1 | grep -v 'PHP Deprecated:'
wp language theme update --all --path="$1" --quiet 2>&1 | grep -v 'PHP Deprecated:'

The key change is to redirect stderr to stdout with 2>&1, and then pipe the output through a reverse grep filter. Once that’s done, the script only produces output when an error occurs that doesn’t include the text 'PHP Deprecated:', and I only get the warnings that I want.

Add new comment

CAPTCHA