Wordpress WP-CLI

Wordpress WP-CLI

Pre svega, da ne zaboravim:

Ne moraš da se trudiš da instaliraš WP CLI na remote serveru. Tačno je sa mora da ima SSH podršku, ali ti instalacija ne treba:

WP-CLI v2 – Managing WordPress From the Terminal Savršeno dakle!


Really important WP-CLI Notes

Run a WP-CLI command one or more sites on WordPress multisite - Daniel Bachhuber https://make.wordpress.org/cli/handbook/references/config/#global-parameters

Detect whether WP-CLI is running - Daniel Bachhuber Always require a specific file when running WP-CLI - Daniel Bachhuber Update all sites on WordPress Multisite to https using WP-CLI - Daniel Bachhuber 10 advanced WP-CLI tricks - Daniel Bachhuber


Install WP-CLI globally
1
2
sudo curl https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar --output /usr/local/bin/wp
sudo chmod +x /usr/local/bin/wp

Test installation with: wp --info or update by typing wp cli update.

How to properly run WP CLI / Composer

It is very important to run those tools as the right user and not to have permission problems later on. Idea came from this article but I’m using in much simplified variation.

File and folder permissions are usually set to “no-login” user and that also presents an obstacle. Best way to switch to proper user (www-data for example) is to execute U=web2; runuser $U -s /bin/bash and there are also alternative ways like sudo -u $U -s sh or su $U -s /bin/bash

But the final solution, and the best one is to execute this:

runuser $(stat -c %U .) -s /bin/bash

This will first run stat -c %U . to get the owner of the current folder, then it will pass this to runuser to execute the command as this user.

WP-CLI Packages

Packages should be used if you don’t want to burden your site with a lot of plugins, for example debug plugins. WP-CLI package is a great solution as WP-CLI places installed packages in ~/.wp-cli/packages/, a hidden subdirectory for the user’s home directory, which means they have no relation to the WordPress installation.

Almost every important package can be found in Package Index, but any package with a URL can be installed even if it’s not in this index. Packages are installed with wp package install <packagename> and then run as a normal WP-CLI command.

In case the Linux user for the site doesn’t have a proper home folder, such as in ISPConfig, you’ll have to install and run the package as another user, but not as root as it’s too risky.

For example, command wp query-debug will count the database queries, and can help you determine which query may be causing performance issues.

Some interesting WP-CLI packages:

You can discover more of those at Packagist


Exhaustive check using Doctor

wp package install wp-cli/doctor-command --debug=false 
wp doctor list --debug=false
wp doctor check --all --debug=false 

WP-CLI Packages: Supercharge Your WordPress Development


Odličan WP CLI početak instalacije i podešavanje celog sistema - wp cli default


Tun Off PHP Warnings

Prilikom pokretanja CLI a kada mi je uključena opcija WP_DEBUG, često dobijam gomilu warninga koji mi smetaju. To isključujem dodavanjem opcije --debug=false na komandu:

wp plugin list --debug=false

Manage Plugins

List of active plugins with updates ready:

wp plugin list --field=name --status=active --update=available

Spisak pluginova mogu da sačuvam, nešto kao “plugin snapshot”:

# create a plugin snapshot
wp plugin list --field=name --status=active > pluginlist-active.txt
wp plugin list --field=name --status=inactive > pluginlist-disabled.txt

# restore state
wp plugin install $(cat pluginlist-active.txt) --activate --debug=false 
wp plugin install $(cat pluginlist-disabled.txt) --debug=false 

Ili još bolje:

echo "wp plugin activate $(wp plugin list --debug=false --status=active --field=name | xargs)" > plugins_restore.sh

možeš onda:
wp plugin deactivate --all

a kasnije:
bash plugins_restore.sh

Dobra je i ideja da uključim auto-update za sve disabled plugins, jer oni ionako ne mogu ništa da zeznu za sajtu:

 wp plugin auto-updates enable $(wp plugin list --status=inactive --field=name)

 # odnosno ako ima mnogo PHP warninga
 wp plugin --debug=false auto-updates enable $(wp plugin --debug=false list --status=inactive --field=name ) 

Sledeća interesantna procedura je nije često potrebna, kojom ustvari zahtevaš refresh svih tema i pluginova, da se ponovo instaliraju.

wp plugin install $(wp plugin list --field=name) --force
wp theme install $(wp theme list --field=name) --force

Ovo je takođe invaluable:

wp plugin verify-checksums  --all

Usage: Clean Post Revisions

Top WP-CLI v2 Commands to Manage WordPress Like a Pro | FastComet

trepmal/wp-revisions-cli

wp revisions clean 0

wposs/snapshot-command: Backup / Restore WordPress installation using WP-CLI je vrlo interesantno

a ima i drugih predloga: mbovel/wp-backup-restore-command mbovel/example-wp-setup za koje ne znam da li rade


Installing, Updating, and Managing WordPress Plugins With WP-CLI WP-CLI Packages: Supercharge Your WordPress Development

The WP-CLI Package Index is a goldmine of incredibly useful commands, but it’s also pretty much dead as it has been deprecated in favor of WP-CLI packages at Packagist


wpify / Clean Uploads · GitLab


Reset WordPress to Default


A5hleyRich/wp-cli-unsplash-command


WP-CLI Examples repository for an example of a WP-CLI script for processing posts on your site, such as the update-content.php script.

date 21. Dec 2016 | modified 17. Aug 2024
filename: Wordpress » Env » WP-CLI