WP-CLI Chinese Getting Started Tutorial: WordPress Command Line Operation Guide

WP-CLI The full name is WordPress Command Line InterfaceThis is the official WordPress recommended command line tool that allows you to use the WordPress The website operates without relying on a backend interface.

Using WP-CLI to manage websites is faster and more flexible than mouse operations, and is suitable for batch processing, automated processes and efficient maintenance.

Image[1]-WP-CLI Chinese complete beginner's guide: command line operation of WordPress efficient tools

What can WP-CLI do?

The WP-CLI performs most everyday tasks such as:

Type of operationExample of command
Plug-in Managementwp plugin install contact-form-7 --activate
WordPress Updateswp core update
Create Articlewp post create --post_title="Hello CLI"
Delete commentwp comment delete 123 --force
Database Exportwp db export backup.sql
Create Userwp user create testuser test@example.com
Clearing the cachewp cache flush
Check site informationwp site list(for multi-site)

Benefits of WP-CLI

Faster operation

One command completes multiple steps, reducing mouse clicks and increasing work efficiency.

Support for automation

Commands can be written to Shell scripts, CI/CD processes, timed tasks or release automation.

Fewer security dependencies

No reliance on graphical backend interfaces to avoid loading resources and reduce the chance of errors.

Installation of WP-CLI

Linux / macOS:

  1. Open a terminal and download the core file: curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  2. Test for document validity: php wp-cli.phar --info
  3. Added as a global command:chmod +x wp-cli.phar sudo mv wp-cli.phar /usr/local/bin/wp
  4. importation wp --infoIf you have not done so, the message is displayed to indicate that the installation has been successful.

Windows:

Recommended Use WP-CLI for Windows Installer or through the Windows Subsystem for Linux Implementation.

start using

go into WordPress After the site root directory, enter:wp

will display all command categories, for example:

  • wp plugin: Plug-in operation
  • wp theme: Theme management
  • wp post: Article Operation
  • wp option: Website Setup
  • wp db: Database management
  • wp user: User handling

If it's not in the WordPress root directory, you'll be prompted with a missing wp-config.phpThe

Summary of common commands

Plug-in Management

wp plugin list
wp plugin install akismet --activate
wp plugin deactivate contact-form-7

Theme management

wp theme install astra --activate
wp theme update astra

database operation

wp db export backup.sql
wp db import backup.sql

Article Management

wp post create --post_title="new post" --post_status=publish
wp post delete 123 --force

user operation

wp user list
wp user create tom tom@example.com --role=editor

Cache Cleanup

wp cache flush

WP-CLI Works with Plugins

Quite a few plugins support WP-CLI commands, for example:

  • WooCommerce
    Support for adding products, updating orders, etc.
    wp wc product create --name="New Product"
  • Yoast SEO
    Building an Indexing Library
    wp yoast index
  • WP Rocket / W3 Total Cache
    Clearing the cache
    wp rocket clean maybe wp w3-total-cache flush

After installing the plugin, perform wp help You can see if CLI support is included.

Developer-defined CLI commands

Plugin developers can add the following snippet to the code to register the command:

if ( defined( 'WP_CLI' ) && WP_CLI ) {
    WP_CLI::add_command( 'myplugin', 'MyPlugin_CLI' );
}

class MyPlugin_CLI {
    public function hello( $args, $assoc_args ) {
        WP_CLI::success( "Hello, WP-CLI!" ); }
    }
}

Run the following command:

wp myplugin hello

A prompt is displayed in the terminal, "Hello, WP-CLI!"

common problems

Q: WordPress not found?

A: Entry contains wp-config.php in the root directory, and then run the command.

Q: Not enough permissions?

A: Some commands may require sudo privileges, or adjusting folder permissions.

Q: Can it be operated remotely?

A: Available through SSH Log in to the remote server and use the WP-CLI commands normally.

summarize

WP-CLI Yes WordPress It is a command line tool suitable for scenarios that require frequent management, script processing or deployment automation. It supports plug-in extensions , compatible with most common operations , is an efficient assistant in the development and management process .


Contact Us
Can't read the tutorial? Contact us for a free answer! Free help for personal, small business sites!
Customer Service
Customer Service
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
This article was written by: thieves will be rats and mice courage
THE END
If you like it, support it.
kudos15 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments