WordPress

WP-CLI
Cheat Sheet

Complete reference for WordPress CLI: core, plugins, themes, database, users, search-replace, cache, cron, WooCommerce.

WP-CLI 2.x
Getting Started

Quick Install

1

Download

wp core download --locale=vi
2

Config

wp config create --dbname=wp_db --dbuser=root --dbpass=pass
3

Install

wp db create && wp core install --url=site.com --title="My Site" --admin_user=admin --admin_email=a@b.com
Reference

Core & System

Core

wp core versionShow WP version
wp core updateUpdate WordPress
wp core update-dbUpdate database tables
wp core verify-checksumsVerify file integrity
wp core is-installedCheck if installed

Config

wp config get DB_NAMEGet database name
wp config listList all constants
wp config set WP_DEBUG true --rawEnable debug
wp config set WP_MEMORY_LIMIT "256M"Set memory limit
wp config shuffle-saltsNew security salts

CLI Info

wp cli versionWP-CLI version
wp cli infoSystem info
wp cli updateUpdate WP-CLI
wp help <command>Command help

Global Flags

--path=/path/to/wpWP root directory
--url=https://site.comOverride site URL
--user=1Run as user
--skip-pluginsDisable all plugins
--format=jsonOutput format
--dry-runPreview without changes
--yesSkip confirmations
Extensions

Plugins & Themes

Plugins

wp plugin install hello --activateInstall & activate
wp plugin install ./plugin.zipInstall from ZIP
wp plugin activate helloActivate
wp plugin deactivate helloDeactivate
wp plugin delete helloDelete
wp plugin list --status=activeList active
wp plugin update --allUpdate all
wp plugin search ecommerceSearch repo
wp plugin verify-checksums helloVerify integrity

Themes

wp theme install astra --activateInstall & activate
wp theme activate astraActivate
wp theme delete twentytwentyfourDelete
wp theme listList all themes
wp theme update --allUpdate all
wp theme mod listList customizations
Data

Database

Backup & Restore

wp db export backup.sqlExport database
wp db export - | gzip > bk.sql.gzExport compressed
wp db import backup.sqlImport database
wp db reset --yesDelete all tables & recreate

Maintenance

wp db checkCheck integrity
wp db repairRepair tables
wp db optimizeOptimize tables
wp db sizeDatabase size
wp db tablesList all tables
wp db cliOpen MySQL console

Query & Search

wp db query "SELECT..."Execute SQL
wp db search "keyword"Search all tables
wp db prefixGet table prefix

Options

wp option get siteurlGet site URL
wp option get blognameGet site title
wp option update siteurl "url"Update site URL
wp option list --search=*cache*Search options
Migration

Search-Replace & Migration

Domain Migration
wp db export backup-before.sql
wp search-replace "http://old.com" "https://new.com" --skip-columns=guid
wp cache flush
wp rewrite flush

Luon backup truoc. Skip guid de khong lam hong post IDs.

Preview (Dry Run)
wp search-replace "old" "new" --dry-run

Xem truoc thay doi ma khong ap dung.

Specific Tables
wp search-replace "old" "new" wp_posts wp_postmeta wp_options

Chi thay the trong cac bang cu the.

Export Changes
wp search-replace "old" "new" --export=changes.sql

Xuat SQL thay vi ap dung truc tiep.

Content

Users & Posts

Users

wp user create admin a@b.com --role=administratorCreate admin
wp user list --role=administratorList admins
wp user update 1 --user_pass=newpassReset password
wp user set-role 1 editorChange role
wp user delete 2 --reassign=1Delete & reassign
wp user generate --count=10Generate test users

Posts & Pages

wp post create --post_title="Hello" --post_status=publishCreate post
wp post list --post_type=pageList pages
wp post update 1 --post_title="New"Update post
wp post delete 1 --forcePermanently delete
wp post generate --count=100Generate test posts
wp post meta update 1 key "val"Update meta

Media

wp media import /path/image.jpgImport media
wp media regenerate --yesRegenerate thumbnails
wp media regenerate --only-missingMissing sizes only
wp media image-sizeList registered sizes

Export / Import

wp exportExport WXR file
wp export --post_type=postExport posts only
wp import file.xml --authors=createImport WXR
Performance

Cache & Cron

Cache

wp cache flushFlush all caches
wp transient delete --allDelete all transients
wp rewrite flushFlush rewrite rules
wp rewrite flush --hardHard flush (recreate)
wp cache typeShow cache type

Permalinks

wp rewrite structure '/%postname%/'Post name
wp rewrite structure '/%category%/%postname%/'Category + name
wp rewrite listList all rules

Cron

wp cron testTest if WP-Cron works
wp cron event listList scheduled events
wp cron event run hook_nameRun specific event
wp cron event delete hook_nameDelete event
wp cron schedule listList intervals

Scaffold

wp scaffold plugin my-pluginGenerate plugin
wp scaffold child-theme child --parent_theme=astraChild theme
wp scaffold post-type moviesCustom post type
wp scaffold block my-blockGutenberg block
Workflows

Common Patterns

Full Install:

wp core download --locale=vi
wp config create --dbname=wp --dbuser=root --dbpass=pass
wp db create
wp core install --url=site.com --title="Site" --admin_user=admin --admin_email=a@b.com

Deploy with Plugins:

wp plugin install woocommerce yoast-seo --activate
wp theme install astra --activate
wp media regenerate --yes
wp rewrite flush

Reset WordPress:

wp db reset --yes
wp core install --url=site.com --title="Fresh" --admin_user=admin --admin_email=a@b.com

Generate Test Content:

wp user generate --count=5 --role=editor
wp post generate --count=100
wp term generate category --count=20