brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: wp plugin is-active gives a WordPress script a yes or no

WordPress
~/tips/tip-wordpress-plugin-is-active-exit

if wp plugin is-active woocommerce; then

Checks whether one plugin is active and answers with exit code 0 or 1.

When a script should only run if a certain plugin is on, parsing wp plugin list is clumsy. There is a command that only answers that question.

if wp plugin is-active woocommerce; then
  wp wc tool run clear_transients --user=admin
fi

The exit code is 0 when the plugin is active and 1 when it is not. Nothing is printed, so it fits straight into an if or an && chain. To test it by hand, run echo $? right after it.

On a multisite network there are two kinds of active. wp plugin is-active akismet --network asks whether the plugin is network-activated. Without --network, and with --url pointing at a subsite, you ask about that one site.

The name is the plugin slug, the folder name you see in wp plugin list, not the title on the Plugins screen. And “not active” includes “not installed”. If a plugin is missing but still listed in the active plugins option, WP-CLI prints a warning, which I find a nice hint that someone deleted a plugin folder by hand.

Further reading: wp plugin is-active

More WordPress tips wp user application-password create anna backup-script --porcelain define( 'MEDIA_TRASH', true ); wp cli check-update wp theme delete --all wp media image-size All WordPress tips (85) All topics

Link