Post

Essential WPCli Commands

WP-CLI (WordPress Command Line Interface) is a powerful command-line tool that allows you to manage various aspects of a WordPress site directly from the command line. It provides a set of commands that can be used to perform tasks such as installing, updating, and managing WordPress installations, themes, plugins, users, and more, without the need to use a web browser.

Here are some common tasks you can accomplish using WP-CLI:

  1. Installing WordPress: You can create a new WordPress installation with just a few commands.
  2. Managing Plugins and Themes: WP-CLI allows you to install, activate, deactivate, and delete plugins and themes from the command line.
  3. Updating WordPress Core, Plugins, and Themes: You can update WordPress core, plugins, and themes with a single command.
  4. Managing Users: You can create, delete, and modify user accounts directly from the command line.
  5. Importing and Exporting Content: WP-CLI provides commands for importing and exporting content from your WordPress site.
  6. Database Management: You can perform various database operations such as optimizing, repairing, and searching the database.
  7. Performing Maintenance Tasks: WP-CLI allows you to perform various maintenance tasks such as generating thumbnails, cleaning up the database, and more.
  8. Developing and Debugging: It provides tools for developers to scaffold plugins and themes, run unit tests, and debug WordPress sites.

Using WP-CLI can significantly speed up your WordPress development and maintenance tasks, especially for tasks that would otherwise require navigating through the WordPress admin interface. It’s especially useful for developers and administrators who are comfortable working in a command-line environment.

Prerequisites

WPCLI installed

Create child theme

1
wp scaffold child-theme sample-theme --parent_theme=twentysixteen

Database commands

Delete database

This command drops the database. No name is required as wpcli looks in the wp-config file to find the database name. You can run this command from anywhere within your Wordpress file structure.

1
wp db drop

Export database

Will export an SQL file to your Wordpress root directory.

1
wp db export

Import database

Ensure to provide the correct path to the file.

1
wp db import wordpress_dbase.sql

Search and Replace database

Find all occurences of a string in a database and replace it with another value. This can be really useful when you are migrating a site to a different domain.

1
wp search-replace 'http://example.test' 'http://example.com'

Plugin commands

List plugins

1
wp plugin list

Delete plugin

1
wp plugin delete akismet

Install Plugin

Here we install the query monitor plugin

1
wp plugin install query-monitor

Update All Plugins

1
wp plugin update --all

Update WP Core

1
wp core update
1
wp rewrite flush

User commands

Create a wp user account

1
wp user create bob bob@example.com --role=author

Get the ID of the user

This command is important for running other user related functions that take the user id as a requirement.

1
wp user list

Update the password of the user

Once you have the user ID then you can update the users password

1
wp user update 7 --user_pass=new_pass

Generate posts

Generate dummy / demo content

1
wp post generate --count=10 --post_type=page --post_date=1999-01-04

Regenerate Image sizes

As you may already know Wordpress supports custom image sizes. Now when you add a new custom image size your site, you will need to regenerate the images so that your new custom image files in the new size exists.

1
wp media regenerate --image_size=large

Troubleshooting

If you are getting some errors when trying to update plugins it could be due to permissions. Some plugins also require a valid license to update.

To fix a permission related issue. See the link below to reset the Wordpress file and directory permissions.

see wp file permissions commands

Switch to non root user

If you have installed WpCLI on a webserver. Before running any wpcli commands you probably want to switch to a non-root so that you are not flagged with warnings when running these commands as root.

Switch to non-root user

1
su non-root

Install WordPress with WpCLI

WpCLI Plays an important role when you installing WordPress on a barebones VPS. As there is no control panel there is no user interface for creating and installing the database. WpCLI steps into to help out and makes installation of Wordpress on a barebones VPS efficient and easy.

Install WordPress website on a VPS using WPCli

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.