This article describes how to use the WP-CLI (WordPress Command Line Interface) tool to administer WordPress sites, as well as how to use it in cron jobs to automate tasks.
It is often quicker and easier to do many tasks at the command line, and WordPress administration is no exception. You can use the WP-CLI (WordPress command-line interface) tool to back up and restore WordPress sites, install plugins, and much more. Additionally, you can use WP-CLI in cron jobs to automate tasks. WP-CLI provides similar functionality to the Drush command-line tool for Drupal.
WP-CLI has many commands, and the rest of this article is only an introduction to its capabilities. However, to view WP-CLI's online help at any time, type the following command:
wp help
Alternatively, to view the online help for a specific WP-CLI subcommand, add it to the help command. For example, to view the online help for the wp plugin command, type the following command:
wp help plugin
WP-CLI makes it easy to work with plugins. For example, to list all of the plugins that are currently installed on your site, type the following command:
wp plugin list
To search for all plugins relating to a particular term, you can use the search option. For example, to search for all plugins related to SEO (search engine optimization), type the following command:
wp plugin search seo
Similarly, installing and activating a new plugin is much quicker than using the web administration interface. To do this, type the following command, replacing name with the name of the plugin that you want to install:
wp plugin install name --activate
To update all of your plugins at once, type the following command:
wp plugin update
Updating WordPress is simple with WP-CLI. To do this, type the following command:
wp core update
Whenever you update WordPress, you should also update the database. To do this, type the following command:
wp core update-db
To verify the WordPress version that your site is running, you can type the following command:
wp core version
It is a good idea to periodically back up your WordPress database. By combining WP-CLI's backup functionality with a cron job, you can automatically back up your database on a set schedule.
To back up a WordPress database using WP-CLI, type the following command:
wp db export
When this command finishes, you have a .sql file that you can store in a safe location. If you need to import a database into WordPress at some point, type the following command. Replace filename with the name of the database backup file:
wp db import filename.sql
In addition to backing up and restoring WordPress databases, you can optimize and repair them. By periodically optimizing databases, you can help ensure that your WordPress sites run efficiently. To do this, type the following command:
wp db optimize
Additionally, if you encounter MySQL errors when running WordPress, you can use WP-CLI to try and repair the database tables. To do this, type the following command:
wp db repair
In addition to backing up and restoring databases with the wp db command, you can back up and restore site content. WP-CLI exports site content, including posts, pages, comments, and categories, to a WXR (WordPress eXtended RSS) file.
To back up a WordPress site's content, type the following command:
wp export
When this command finishes, you have a .xml file that you can store in a safe location. If you need to import a WXR file into WordPress at some point, type the following command. Replace filename with the name of the backup file:
wp import filename.xml --authors=create
You can use WP-CLI to quickly and easily change a user's password (for example, if you forget the administrator password). To do this, type the following command. Replace username with the name of the user (for example, admin), and password with the new password:
wp user update username --user_pass=password
wp user list
WP-CLI provides an easy way to manage WordPress options. For example, to change the admin e-mail address, type the following command, replacing [email protected] with the e-mail address you want to use:
wp option update admin_email [email protected]
If you have set a custom login URL and need to retrieve this information, type the following command:
wp option get rwl_page
By combining WP-CLI's functionality with cron jobs, you can easily and automatically do administrative tasks that from the WordPress administration interface are time consuming and repetitive.
For example, the following cron line demonstrates how to back up a WordPress site every day at 2:15 AM using WP-CLI:
15 2 * * * cd /home/username/public_html; /usr/bin/wp export >/dev/null 2>&1
Similarly, the following cron line demonstrates how to automatically check for and install any available WordPress updates every day at 3:15 AM:
15 3 * * * cd /home/username/public_html; /usr/bin/wp core update >/dev/null 2>&1
For more information about WP-CLI, please visit http://wp-cli.org.
Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.
We use cookies to personalize the website for you and to analyze the use of our website. You consent to this by clicking on "I consent" or by continuing your use of this website. Further information about cookies can be found in our Privacy Policy.