11 September 2023

WP-CLI guide to managing WordPress from the command line

An Ideal Hosting for Developers and DevOps: Automation, Control and Efficiency with SSH Access and Pre-Installed WP-CLI

Managing a WordPress site can quickly become complicated, especially when it comes to performing repetitive tasks or making large-scale changes. Fortunately, there is a tool that can make these tasks much easier: WP-CLI, the WordPress command line. First launched in 2011, WP-CLI has become an indispensable tool for WordPress developers. You can find further details and complete documentation on WP-CLI official website.

In this guide, we'll explore how to use WP-CLI to effectively manage your WordPress sites. This tool is especially useful for developers and systems engineers looking to automate tasks or perform advanced operations without accessing the WordPress web interface.

Note: All of our WordPress Hosting plans come with SSH access and the WP-CLI pre-installed, making it even easier to get started with this powerful tool.

What is WP-CLI?

WP-CLI is a set of command line tools for WordPress. It allows you to perform many operations that you would normally do through the WordPress web interface, directly from the command line. For example, you can install and uninstall plugins, create new posts, manage users, and much more. Since it was released in 2011, WP-CLI has seen numerous updates and improvements, making it an increasingly powerful and flexible tool. To stay up to date on the latest features and improvements, you can check out the WP-CLI GitHub repository.

With its wide range of commands and options, WP-CLI has become an indispensable tool for any developer or systems engineer working with WordPress. And with the pre-installation of WP-CLI in our WordPress Hosting plans, you have everything you need to start making the most of this great tool.

Installation and Configuration

If you are using one of our WordPress Hosting plans, WP-CLI is already installed and ready to use. However, if you want to install it manually on another system, you can do so by following these steps:

  1. Download the latest version of WP-CLI from the official website.
  2. Make the file executable and move it to the directory /usr/local/bin for global access.
chmod + x wp-cli.phar sudo mv wp-cli.phar / usr / local / bin / wp

Once you have installed WP-CLI globally (i.e. accessible to all users logged in to SSH) you can verify its correct installation and functioning by typing:

wp --info

You should get output like the one in the following image.

WP-CLI-info

Basic Commands

The power of WP-CLI lies in its simplicity and flexibility. Here is an extensive list of basic commands to help you get started:

  1. Download WordPress: The command wp core download download the latest version of WordPress to the current directory.
  2. Install a Plugin: Using wp plugin install [nome-plugin], you can install a specific plugin directly from the WordPress repository.
  3. Install a Theme: The command wp theme install [nome-tema] install a specific theme from the WordPress repository.
  4. Create a New User: wp user create [nome-utente] [email] creates a new user with the specified username and email address.
  5. List All Plugins: With wp plugin list, you can see a list of all installed plugins, along with their status (active, inactive, etc.).
  6. Update All Plugins: The command wp plugin update --all update all installed plugins to the latest available version.
  7. List All Themes: Using wp theme list, you can see a list of all installed themes, along with their status (active, inactive, etc.).
  8. Update WordPress to the Latest Version: wp core update update WordPress core to the latest version available.
  9. Export the Database: The command wp db export export the WordPress database to a SQL file.
  10. Import a Database: Using wp db import [nome-file.sql], you can import a database from a specified SQL file.

Bonus: Run a SQL Command: With wp db query "SELECT * FROM wp_posts WHERE post_status = 'publish';", you can run a SQL query directly on your WordPress database and get the results.

With these basic commands, you'll have a good starting point for effectively managing your WordPress sites directly from the command line.

The Importance of Bash Scripting and WP-CLI Integration

Bash scripting is one of the fundamental skills for anyone working in the field of Linux systems engineering. Bash (Bourne Again SHell) is a command shell and scripting language used primarily on Unix-like operating systems, such as Linux. Bash scripts allow you to automate a variety of tasks, making system and application management more efficient. When it comes to running WordPress sites on Linux servers, the integration between Bash scripting and WP-CLI opens up a world of possibilities for automating, controlling, updating and migrating sites.

Automation

Imagine having to install and configure WordPress on several sites. Doing everything manually would be a long and tedious process. However, with a Bash script using WP-CLI commands, you can automate the entire process. For example, you could write a script that downloads the latest version of WordPress, creates a new database and user, installs WordPress, and activates a default set of plugins and themes. All of this could be done with a single command, saving hours of work.

Controls

Bash scripts can be used to perform periodic site status checks. For example, you might have a script that uses the WP-CLI to check if there are any updates available for your plugins or themes and perform the updates automatically if needed. This ensures that your site is always up to date, reducing the risk of security vulnerabilities.

Updates

Updates are a critical part of maintaining a WordPress site. With WP-CLI and a Bash script, you can automate the backup process before performing any updates, thus reducing the risk of data loss. Additionally, you could schedule updates to run during off-peak hours, minimizing the impact on users.

Migrations

Migrating a WordPress site from one server to another is a task that can become complex and error-prone if done manually. However, a well-written Bash script can use WP-CLI to export the database, download all the necessary files, transfer them to the new server, and import the database, all in an automated manner.

In conclusion, the integration between Bash scripting and WP-CLI not only makes managing WordPress sites more efficient, but also opens the door to new levels of automation and control.

Example: Automate the installation of a WordPress site.

For example, you can write a bash script that runs a series of WP-CLI commands to set up a new WordPress site from scratch.

Save this script to a file, make it executable, and then run it. In just a few minutes, you will have a new WordPress site set up and ready to use.

#!/bin/bash wp core download wp config create --dbname=db_name --dbuser=db_user --dbpass=db_password wp db create wp core install --url=your-domain.com --title="Title Site" --admin_user=user_name --admin_password=user_password --admin_email=user_email wp theme install twentynineteen wp theme activate twentynineteen wp plugin install hello-dolly wp plugin activate hello-dolly

Conclusion

WP-CLI is an incredibly powerful tool that every WordPress developer should know about. Not only does it make managing WordPress sites easier, but it also offers powerful automation capabilities that can save you hours of manual work. The importance of Bash scripting in this context cannot be understated; it represents an additional layer of flexibility and control, allowing you to automate complex tasks, perform regular checks and manage updates and migrations with precision.

In all our WordPress Hosting plans, we provide not only user-level SSH access, but also pre-installed WP-CLI. This combination makes our hosting an excellent choice for demanding users such as developers and DevOps professionals. SSH access allows you to interact directly with the server, offering granular control that goes far beyond what is possible through a web interface. The presence of WP-CLI, then, means that you can immediately start using this powerful tool without having to go through complicated installation or configuration processes.

In short, if you are looking for a hosting solution that offers you maximum control and flexibility, without compromising on performance and security, our WordPress Hosting plans are the answer you are looking for. With SSH access and the pre-installed WP-CLI, you have all the tools you need to manage, optimize and grow your WordPress sites efficiently and professionally.

Do you have doubts? Don't know where to start? Contact us!

We have all the answers to your questions to help you make the right choice.

Chat with us

Chat directly with our presales support.

0256569681

Contact us by phone during office hours 9:30 - 19:30

Contact us online

Open a request directly in the contact area.

INFORMATION

Managed Server Srl is a leading Italian player in providing advanced GNU/Linux system solutions oriented towards high performance. With a low-cost and predictable subscription model, we ensure that our customers have access to advanced technologies in hosting, dedicated servers and cloud services. In addition to this, we offer systems consultancy on Linux systems and specialized maintenance in DBMS, IT Security, Cloud and much more. We stand out for our expertise in hosting leading Open Source CMS such as WordPress, WooCommerce, Drupal, Prestashop, Joomla, OpenCart and Magento, supported by a high-level support and consultancy service suitable for Public Administration, SMEs and any size.

Red Hat, Inc. owns the rights to Red Hat®, RHEL®, RedHat Linux®, and CentOS®; AlmaLinux™ is a trademark of AlmaLinux OS Foundation; Rocky Linux® is a registered trademark of the Rocky Linux Foundation; SUSE® is a registered trademark of SUSE LLC; Canonical Ltd. owns the rights to Ubuntu®; Software in the Public Interest, Inc. holds the rights to Debian®; Linus Torvalds holds the rights to Linux®; FreeBSD® is a registered trademark of The FreeBSD Foundation; NetBSD® is a registered trademark of The NetBSD Foundation; OpenBSD® is a registered trademark of Theo de Raadt. Oracle Corporation owns the rights to Oracle®, MySQL®, and MyRocks®; Percona® is a registered trademark of Percona LLC; MariaDB® is a registered trademark of MariaDB Corporation Ab; REDIS® is a registered trademark of Redis Labs Ltd. F5 Networks, Inc. owns the rights to NGINX® and NGINX Plus®; Varnish® is a registered trademark of Varnish Software AB. Adobe Inc. holds the rights to Magento®; PrestaShop® is a registered trademark of PrestaShop SA; OpenCart® is a registered trademark of OpenCart Limited. Automattic Inc. owns the rights to WordPress®, WooCommerce®, and JetPack®; Open Source Matters, Inc. owns the rights to Joomla®; Dries Buytaert holds the rights to Drupal®. Amazon Web Services, Inc. holds the rights to AWS®; Google LLC holds the rights to Google Cloud™ and Chrome™; Microsoft Corporation holds the rights to Microsoft®, Azure®, and Internet Explorer®; Mozilla Foundation owns the rights to Firefox®. Apache® is a registered trademark of The Apache Software Foundation; PHP® is a registered trademark of the PHP Group. CloudFlare® is a registered trademark of Cloudflare, Inc.; NETSCOUT® is a registered trademark of NETSCOUT Systems Inc.; ElasticSearch®, LogStash®, and Kibana® are registered trademarks of Elastic NV Hetzner Online GmbH owns the rights to Hetzner®; OVHcloud is a registered trademark of OVH Groupe SAS; cPanel®, LLC owns the rights to cPanel®; Plesk® is a registered trademark of Plesk International GmbH; Facebook, Inc. owns the rights to Facebook®. This site is not affiliated, sponsored or otherwise associated with any of the entities mentioned above and does not represent any of these entities in any way. All rights to the brands and product names mentioned are the property of their respective copyright holders. Any other trademarks mentioned belong to their registrants. MANAGED SERVER® is a trademark registered at European level by MANAGED SERVER SRL, Via Enzo Ferrari, 9, 62012 Civitanova Marche (MC), Italy.

JUST A MOMENT !

Would you like to see how your WooCommerce runs on our systems without having to migrate anything? 

Enter the address of your WooCommerce site and you will get a navigable demonstration, without having to do absolutely anything and completely free.

No thanks, my customers prefer the slow site.
Back to top