Last Updated on November 19, 2024 EST by Jordan
Content Error or Suggest an Edit
Notice a grammatical error or technical inaccuracy? Let us know; we will give you credit!
Introduction
I posted a link to a blog post that provided an interesting method to run wp-cli and skip all enabled WordPress plugins except for one. The wp-cli command allows you to use –skip-plugins which skips all WordPress plugins from loading, you can also specify specific WordPress plugins to skip by adding them to –skip-plugins like so
wp user list --skip-plugins=hello-dolly
However, there isn’t a method to run wp-cli and only load a single WordPress plugin. However, I found a blog article online that provided a clever approach to achieve this, and I posted this article in the Managing WP facebook group There were lots of comments about how to improve this method, and as such I’m going to detail all of them below including the original method.
Why is it important to skip all WordPres plugins except one?
Unfortunately, wp-cli is prone to errors due to WordPress plugins and themes not supporting wp-cli and just really silly reasons. Therefore, skipping WordPress plugins and themes allows wp-cli to operate successfully without error, and this is important when using wp-cli in scripts orautomating tasks.
Method 1: The Original Method by trepmal.com
The original method was from a blog post on trepmal.com and provided three ways to skip all available plugins but one.
One-liner Method
It’s a fairly simple one-liner method. It will run a sub shell command listing all available plugins and then grep out the plugin of your choice before removing all new lines for a comma.
wp user list --skip-plugins=$(wp plugin list --field=name | grep -v ^DONTSKIP$ | tr '\n' ',')
Text File Method
You can also generate and pass a text file of plugins you want to skip, and then reference the text file using another sub shell command to cat the text file.
wp plugin list --field=name | grep -v ^DONTSKIP$ | tr '\n' ',' > skipplugins.txt wp user list --skip-plugins=`cat skipplugins.txt`
This is great. However, if a bad plugin or theme exists, the sub-shell command will fail. That’s where Method 2 comes into play.
Permanent config file Method
You can define a list of plugins to skip by using a wp-cli configuration file, this file has to reside in your users home directory under .wp-cli in a file named config.yml as show in the following example.
#~/.wp-cli/config.yml skip-plugins: - skip-me - skip-me-too
Method 2: Fix Method 1 when Encountering Broken Plugins and Themes
So in Method 1, there is an issue with the sub-shell command to get a list of plugins such as a broken plugin, theme or package. The wp-cli command fais to run. Aon Shah mentioned this and provided an improved method
Aon Shah
Minor issue with the guide above
If wp cli is broken it wont get plugin list either So
wp user list –skip-plugins=$(wp plugin list — field=name –skip-plugins –skip-themes –skip-packages | grep -v ^DONTSKIP$ | tr ‘\n’ ‘,’)
The new one-liner code changed how the sub-shell command gathers the list of available plugins, it will skip plugins, themes, and packages to get the list of available plugins. So if there is a WordPress plugin or theme that is generating an error, it will be skipped and the list of plugins will be returned correctly.
wp user list --skip-plugins=$(wp plugin list -- field=name --skip-plugins --skip-themes --skip-packages | grep -v ^DONTSKIP$ | tr '\n' ',')
Here are the other Method 1 options updated to use the same technique.
Text File Method Updated
wp plugin list --field=name | grep -v ^DONTSKIP$ | tr '\n' ',' > skipplugins.txt wp user list --skip-plugins=`cat skipplugins.txt`
Permanent config file Method Updated
#~/.wp-cli/config.yml skip-plugins: - skip-me - skip-me-too
Method 3 – Allow Single or Multiple Plugins to Load
Another method which was provided by Wes Tatters can be used either on a one-liner or within your shell scripts. Here’s the example he provided.
SKIPPLUGINS='^litespeed-cache$\|^object-cache-pro$\|^redis-cache$' SKIPLIST=$(wp --skip-plugins --skip-themes --skip-packages --quiet plugin list --field=name 2>/dev/null | grep -v $SKIPPLUGINS | tr '\n' ',' ) wp --skip-plugins="$SKIPLIST" --skip-themes --skip-packages --quiet user list
Here’s a breakdown of who the code above works.
- The SKIPPLUGINS variable lists all the WordPress plugins you want to load when running wp-cli
- The SKIPLIST variable gets the list of available WordPress plugins on the WordPress instance, and removes the SKIPPLUGINS list of WordPress plugins.
- The last command is the wp-cli command that will take the the SKIPLIST variable as the list of WordPress plugins to not load.
You can put this into a one-liner, here is an example
wp --skip-plugins=\"$(wp --skip-plugins --skip-themes --skip-packages --quiet plugin list --field=name 2>/dev/null | grep -v '^litespeed-cache$\|^object-cache-pro$\|^redis-cache$' | tr '\n' ',' )\" --skip-themes --skip-packages --quiet user list
Conclusion
Ultimately it would be great to see wp-cli have an option to specify what plugins to load, introducing the following options –use-plugin or –load-plugin which has been presented to the wp-cli team and shot down.
Changelog
Last Updated on November 19, 2024 EST by Jordan