Ricerca nel sito web

WP-CLI - Gestione di WordPress dal terminale Linux


Su questa pagina

  1. Prerequisiti
  2. Per iniziare
  3. Installa WP-CLI
  4. Gestisci i plugin con WP-CLI
  5. Gestisci i temi con WP-CLI
  6. Crea e gestisci post e pagine con WP-CLI
  7. Gestisci database con WP-CLI
  8. Aggiorna WordPress con WP-CLI
  9. Conclusione

Se sei un amministratore di sistema e sei responsabile della gestione di centinaia o migliaia di siti Web WordPress, è un processo che richiede molto tempo. Dovrai accedere a ciascun pannello di controllo di WordPress, installare o aggiornare plugin e temi. Questo è il punto in cui entra in scena il WP-CLI. Il

WP-CLI è un potente strumento da riga di comando specificamente progettato per gestire WordPress dalla riga di comando. Puoi gestire più siti WordPress senza accedere al pannello di amministrazione di WordPress. Con WP-CLI, puoi eseguire diverse operazioni tra cui installare e aggiornare plugin, temi, creare contenuti, lavorare con database e altro.

In questo post, mostrerò come installare e utilizzare WP-CLI per gestire i siti WordPress.

Prerequisiti

  • Un server che esegue Ubuntu 20.04 con WordPress installato.
  • Sul server è configurata una password di root.

Iniziare

Innanzitutto, dovrai aggiornare la cache dei pacchetti APT sul tuo sistema. Puoi aggiornarlo con il seguente comando:

apt-get update -y

Una volta aggiornato il sistema, puoi procedere al passaggio successivo.

Installa WP-CLI

Innanzitutto, scarica il binario WP-CLI con il seguente comando:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Una volta scaricato, imposta i permessi appropriati per il file scaricato:

chmod +x wp-cli.phar

Successivamente, copia il file binario scaricato nel percorso di sistema con il seguente comando:

cp wp-cli.phar /usr/bin/wp

Ora verifica la versione di WP-CLI con il seguente comando:

wp cli version --allow-root

Dovresti vedere il seguente output:

WP-CLI 2.5.0

Gestisci i plugin con WP-CLI

In questa sezione impareremo come cercare, installare, aggiornare ed eliminare i plugin su un sito WordPress dalla riga di comando.

Innanzitutto, modifica la directory nel tuo sito Web WordPress con il seguente comando:

cd /var/www/html/wordpress

Per elencare tutti i plugin installati sul tuo sito WordPress, esegui il seguente comando:

wp plugin list --allow-root

Dovresti vedere il seguente output:

+---------+----------+--------+---------+
| name    | status   | update | version |
+---------+----------+--------+---------+
| akismet | inactive | none   | 4.1.9   |
| hello   | inactive | none   | 1.7.2   |
+---------+----------+--------+---------+

Per cercare un plug-in specifico, esegui il seguente comando:

wp plugin search cache --allow-root

Dovresti vedere tutti i plugin relativi alla memorizzazione nella cache nel seguente output:

Success: Showing 10 of 3688 plugins.
+--------------------------------------------------------------------------------+--------------------------+--------+
| name                                                                           | slug                     | rating |
+--------------------------------------------------------------------------------+--------------------------+--------+
| LiteSpeed Cache                                                                | litespeed-cache          | 98     |
| W3 Total Cache                                                                 | w3-total-cache           | 88     |
| WP-Optimize – Cache, Clean, Compress.                                    | wp-optimize              | 96     |
| WP Fastest Cache                                                               | wp-fastest-cache         | 98     |
| WP Cloudflare Super Page Cache                                                 | wp-cloudflare-page-cache | 98     |
| Redis Object Cache                                                             | redis-cache              | 92     |
| WP Super Cache                                                                 | wp-super-cache           | 86     |
| Autoptimize                                                                    | autoptimize              | 94     |
| Hummingbird – Optimize Speed, Enable Cache, Minify CSS & Defer Critical JS | hummingbird-performance  | 96     |
| Cache Enabler                                                                  | cache-enabler            | 88     |
+--------------------------------------------------------------------------------+--------------------------+--------+

Ora, installa il plug-in specifico dall'elenco sopra con il seguente comando:

wp plugin install wp-super-cache --allow-root

Dovresti vedere il seguente output:

Installing WP Super Cache (1.7.3)
Downloading installation package from https://downloads.wordpress.org/plugin/wp-super-cache.1.7.3.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.

Ora, conferma il plugin installato o meno con il seguente comando:

wp plugin list --allow-root

Dovresti vedere il seguente output:

+----------------+----------+--------+---------+
| name           | status   | update | version |
+----------------+----------+--------+---------+
| akismet        | inactive | none   | 4.1.9   |
| hello          | inactive | none   | 1.7.2   |
| wp-super-cache | inactive | none   | 1.7.3   |
+----------------+----------+--------+---------+

Per installare il plugin dalla fonte specifica con il seguente comando:

wp plugin install https://downloads.wordpress.org/plugin/caldera-forms.1.9.4.zip --allow-root

Dovresti vedere il seguente output:

Downloading installation package from https://downloads.wordpress.org/plugin/caldera-forms.1.9.4.zip...
Unpacking the package...
Installing the plugin...
Plugin installed successfully.
Success: Installed 1 of 1 plugins.

Per attivare il plugin installato, esegui il seguente comando:

wp plugin activate wp-super-cache --allow-root

Dovresti vedere il seguente output:

Plugin 'wp-super-cache' activated.
Success: Activated 1 of 1 plugins.

Per disattivare il plug-in installato, esegui il seguente comando:

wp plugin deactivate wp-super-cache --allow-root

Dovresti vedere il seguente output:

Plugin 'wp-super-cache' deactivated.
Success: Deactivated 1 of 1 plugins.

Per attivare tutti i plugin, esegui il seguente comando:

wp plugin activate --all --allow-root

Per aggiornare un plug-in specifico, esegui il seguente comando:

wp plugin update akismet --allow-root

Per eliminare un plug-in specifico, esegui il seguente comando:

wp plugin delete wp-super-cache --allow-root

Per eliminare tutti i plugin, esegui il seguente comando:

wp plugin delete --all --allow-root

Gestisci i temi con WP-CLI

In questa sezione, ti mostreremo come installare, aggiornare, cercare e gestire i temi con WP-CLI.

Per elencare tutti i temi installati nel tuo sito WordPress, esegui il seguente comando:

wp theme list --allow-root

Dovresti vedere il seguente output:

+-----------------+----------+--------+---------+
| name            | status   | update | version |
+-----------------+----------+--------+---------+
| twentynineteen  | inactive | none   | 2.0     |
| twentytwenty    | inactive | none   | 1.7     |
| twentytwentyone | active   | none   | 1.3     |
+-----------------+----------+--------+---------+

Per cercare un tema specifico, eseguire il seguente comando:

wp theme search metro --allow-root

Dovresti vedere tutti i temi che corrispondono alla parola metro:

Success: Showing 4 of 4 themes.
+----------------+----------------+--------+
| name           | slug           | rating |
+----------------+----------------+--------+
| Metrolo        | metrolo        | 100    |
| MetroStore     | metrostore     | 100    |
| Metro Magazine | metro-magazine | 98     |
| Rara Magazine  | rara-magazine  | 0      |
+----------------+----------------+--------+

Per installare e attivare il tema della metropolitana, esegui il seguente comando:

wp theme install metro-magazine --activate --allow-root

Dovresti vedere il seguente output:

Installing Metro Magazine (1.3.5)
Downloading installation package from https://downloads.wordpress.org/theme/metro-magazine.1.3.5.zip...
Unpacking the package...
Installing the theme...
Theme installed successfully.
Activating 'metro-magazine'...
Success: Switched to 'Metro Magazine' theme.
Success: Installed 1 of 1 themes.

Per aggiornare tutti i temi, esegui il seguente comando:

wp theme update --all --allow-root

Per eliminare un tema specifico, eseguire il seguente comando:

wp theme delete metro-magazine --allow-root

Crea e gestisci post e pagine con WP-CLI

In questa sezione, ti mostreremo come elencare, creare e gestire post e pagine con WP-CLI.

Per elencare tutti i post del tuo sito WordPress, esegui il seguente comando:

wp post list --allow-root

Dovresti ottenere il seguente output:

+----+--------------+-------------+---------------------+-------------+
| ID | post_title   | post_name   | post_date           | post_status |
+----+--------------+-------------+---------------------+-------------+
| 1  | Hello world! | hello-world | 2021-06-09 14:51:29 | publish     |
+----+--------------+-------------+---------------------+-------------+

Per eliminare un messaggio numerico specifico, eseguire il seguente comando:

wp post delete 1 --allow-root

Per creare un nuovo post, esegui il seguente comando:

wp post create --post_status=publish --post_title="How to Manage WordPress with WP-CLI" --edit --allow-root

Per creare una pagina invece di un post, esegui il seguente comando:

wp post create --post_title="My new page" --post_status=draft --post_type=page --allow-root

Per generare 30 post con dati fittizi, esegui il seguente comando:

wp post generate --count=30 --allow-root

Per elencare tutti i post generati, esegui il seguente comando:

wp post list --allow-root

Dovresti vedere il seguente output:

+----+-------------------------------------+-------------------------------------+---------------------+-------------+
| ID | post_title                          | post_name                           | post_date           | post_status |
+----+-------------------------------------+-------------------------------------+---------------------+-------------+
| 7  | Post 2                              | post-2                              | 2021-06-09 15:00:57 | publish     |
| 8  | Post 3                              | post-3                              | 2021-06-09 15:00:57 | publish     |
| 9  | Post 4                              | post-4                              | 2021-06-09 15:00:57 | publish     |
| 10 | Post 5                              | post-5                              | 2021-06-09 15:00:57 | publish     |
| 11 | Post 6                              | post-6                              | 2021-06-09 15:00:57 | publish     |
| 12 | Post 7                              | post-7                              | 2021-06-09 15:00:57 | publish     |
| 13 | Post 8                              | post-8                              | 2021-06-09 15:00:57 | publish     |
| 14 | Post 9                              | post-9                              | 2021-06-09 15:00:57 | publish     |
| 15 | Post 10                             | post-10                             | 2021-06-09 15:00:57 | publish     |
| 16 | Post 11                             | post-11                             | 2021-06-09 15:00:57 | publish     |
| 17 | Post 12                             | post-12                             | 2021-06-09 15:00:57 | publish     |
| 18 | Post 13                             | post-13                             | 2021-06-09 15:00:57 | publish     |
| 19 | Post 14                             | post-14                             | 2021-06-09 15:00:57 | publish     |
| 20 | Post 15                             | post-15                             | 2021-06-09 15:00:57 | publish     |
| 21 | Post 16                             | post-16                             | 2021-06-09 15:00:57 | publish     |
| 22 | Post 17                             | post-17                             | 2021-06-09 15:00:57 | publish     |
| 23 | Post 18                             | post-18                             | 2021-06-09 15:00:57 | publish     |
| 24 | Post 19                             | post-19                             | 2021-06-09 15:00:57 | publish     |
| 25 | Post 20                             | post-20                             | 2021-06-09 15:00:57 | publish     |
| 26 | Post 21                             | post-21                             | 2021-06-09 15:00:57 | publish     |
| 27 | Post 22                             | post-22                             | 2021-06-09 15:00:57 | publish     |
| 28 | Post 23                             | post-23                             | 2021-06-09 15:00:57 | publish     |
| 29 | Post 24                             | post-24                             | 2021-06-09 15:00:57 | publish     |
| 30 | Post 25                             | post-25                             | 2021-06-09 15:00:57 | publish     |
| 31 | Post 26                             | post-26                             | 2021-06-09 15:00:57 | publish     |
| 32 | Post 27                             | post-27                             | 2021-06-09 15:00:57 | publish     |
| 33 | Post 28                             | post-28                             | 2021-06-09 15:00:57 | publish     |
| 34 | Post 29                             | post-29                             | 2021-06-09 15:00:57 | publish     |
| 35 | Post 30                             | post-30                             | 2021-06-09 15:00:57 | publish     |
| 36 | Post 31                             | post-31                             | 2021-06-09 15:00:57 | publish     |
| 5  | How to Manage WordPress with WP-CLI | how-to-manage-wordpress-with-wp-cli | 2021-06-09 15:00:39 | publish     |
+----+-------------------------------------+-------------------------------------+---------------------+-------------+

Per generare una pagina con dati fittizi, eseguire il seguente comando:

wp post generate --count=30 --post_type=page --allow-root

Gestisci database con WP-CLI

Puoi anche gestire il database con WP-CLI.

Per eseguire il backup dell'intero database WordPress, esegui il seguente comando:

wp db export --allow-root

Dovresti vedere il seguente output:

Success: Exported to 'mysite-2021-06-09-14d4641.sql'.

Puoi anche importare il database di WordPress con il seguente comando:

wp db import backup.sql --allow-root

Aggiorna WordPress con WP-CLI

Per stampare la versione corrente del tuo WordPress, esegui il seguente comando:

wp core version --allow-root

Dovresti vedere il seguente output:

5.7.2

Per verificare l'aggiornamento di WordPress, esegui il seguente comando:

wp core check-update --allow-root

Dovresti vedere il seguente output:

Success: WordPress is at the latest version.

Ora puoi aggiornare WordPress all'ultima versione disponibile con il seguente comando:

wp core update --allow-root

Conclusione

Nella guida sopra, hai imparato come installare e utilizzare WP-CLI per gestire il sito WordPress. Spero che questo semplificherà molto il tuo lavoro.