WordPress
A Repeatable WP-CLI Workflow for WordPress Agencies
A WP-CLI workflow for agencies is a scripted, repeatable sequence of commands that handles setup, database migration, cache flushes and deployment without manual clicks in wp-admin. In my builds I keep it in version-controlled shell scripts so every site follows the same tested path. Saif Al Jahan uses this approach from Mymensingh to ship WordPress work with fewer mistakes and faster handoffs.
What a WP-CLI workflow replaces
Most agencies still move WordPress sites by hand: download a database dump, edit SQL in a text editor, upload files over FTP, then click through settings to clear caches. That path is fast once and fragile every time after. A WP-CLI workflow replaces those clicks with named commands you can review, version and run identically on staging and production. Because each command is explicit, a new developer can read the script and know exactly what happens to the database and files. I keep the scripts next to the project so the deployment history travels with the code instead of living only in a terminal scrollback.
- One script covers local, staging and production runs
- Every destructive command sits behind an explicit flag
- Commands are reviewable in version control
- A new developer can reproduce a deploy without asking
Repeatable local setup with wp core and wp config
Repeatable setup starts before any content exists. I install WordPress core with wp core download, write wp-config.php with wp config create, then run wp core install with fixed values for the site URL and admin user. Plugins and themes come from a pinned list using wp plugin install with explicit versions, followed by wp plugin activate. Doing this in a script means a fresh local environment matches staging in minutes rather than an afternoon of clicking through wizards. The config keys — DB_NAME, DB_USER, DB_HOST and the table prefix — are generated the same way each time.
- wp core download and wp config create for a clean base
- wp core install with explicit --url and --admin_user
- wp plugin install with a pinned --version for reproducibility
- A fixed table prefix defined once, never changed later
- wp theme activate to set the project theme deterministically
Safe search-replace across environments
Moving a database between domains is where most manual migrations break, because WordPress stores URLs inside serialized PHP strings. Running raw SQL against wp_options or wp_postmeta corrupts those strings. wp search-replace understands serialization and rewrites values safely. I always run it with --dry-run first to see a count of affected tables and rows, then run the real command. Two flags matter in agency work: --skip-columns=guid leaves globally unique identifiers untouched, and --all-tables covers custom plugin tables that a default run would miss. I run the same command on staging before touching production, so the row counts are already known.
- wp search-replace old.test new.test --dry-run to preview
- --skip-columns=guid to preserve stable identifiers
- --all-tables to include plugin and WooCommerce tables
- Compare row counts between dry run and staging before production
- Run wp cache flush after any URL rewrite completes
Cache flushes that actually clear everything
A deployment is not finished when the files land. Stale caches are the usual reason a correct deploy looks broken. wp cache flush clears the object cache, but it does not purge a page cache, a reverse proxy or a CDN. I treat those as separate steps: flush the object cache with WP-CLI, clear the page cache with a plugin-level command or API, then purge the CDN by URL or tag. I also run wp rewrite flush after changing permalinks or registering a post type, because rewrite rules are stored in the database and will not update on their own.
- wp cache flush for the object cache
- Plugin-level page cache purge after content changes
- CDN purge by URL or cache tag, never a blind full purge
- wp rewrite flush after permalink or post type changes
- Verify with a fresh, unauthenticated request
A safe deployment checklist
The deployment checklist turns the individual commands into a safe sequence. I work from a staging run first: pull a production copy, apply the search-replace, run the same cache and rewrite flushes, and confirm the site loads with a clean browser session. Only then do I repeat on production. The production run starts with a file and database backup I can restore in one command, then a short maintenance window, the code deploy, the database migration, the cache flushes and a smoke test of the homepage, login and checkout if it is a store. Each step has a rollback before the next begins.
- Take a restorable database and file backup first
- Deploy code, then migrate the database, then flush caches
- Smoke-test homepage, wp-admin login and any cart or checkout
- Keep a one-command rollback path until the smoke test passes
- Log the commit SHA deployed alongside the timestamp
Where the workflow fits, and where it stops
The trade-off is discipline versus speed. A scripted workflow takes longer to write once, but it removes the variation that causes weekend incidents. I keep destructive commands explicit rather than hidden inside a wrapper, so the person running a deploy can see the exact command before it executes. Where a step needs judgement — a large WooCommerce migration or a site with a fragile CDN configuration — I leave it manual and document it instead of pretending it is safe to automate. I build this kind of repeatable process in my custom WordPress projects; the approach is documented on webdev-saif.com.
Frequently asked questions
What is a WP-CLI workflow for an agency?
It is a set of command-line scripts that perform routine WordPress tasks — install, search-replace, cache flush and deploy — the same way every time. For an agency it replaces ad-hoc clicks in wp-admin with reviewed, version-controlled commands. The benefit is repeatability: staging and production run identical steps, so an error found on staging is caught before it reaches a live site.
Is it safe to run wp search-replace on a live database?
It is safe when you take the right precautions. Always run the command with --dry-run first to see how many rows in which tables will change, and use --skip-columns=guid so permanent identifiers are left alone. Take a fresh backup before the real run. Because WP-CLI handles serialized PHP data correctly, it is considerably safer than editing SQL by hand, which can corrupt theme options and widget settings.
Does wp cache flush clear the CDN and page cache too?
No. wp cache flush clears the WordPress object cache only. A page cache from a caching plugin, a server-level cache such as Redis or Varnish, and a CDN each need their own purge step. I run them in sequence after a deploy — object cache, page cache, then CDN by URL or tag — and finish by checking the site with an unauthenticated request so I see what a visitor sees.
Can WP-CLI replace a deployment platform?
Not entirely. WP-CLI handles the WordPress-specific steps well: migrations, cache flushes, rewrite rules and user management. It does not give you build artifacts, deploy history or automatic rollback on its own. In my builds I pair WP-CLI scripts with a simple deployment tool or Git-based workflow that records the commit SHA, so each release is traceable and reversible rather than just a list of commands.
What should an agency put in a deployment checklist?
At minimum: a verified backup, a staging run of the exact commands, a maintenance window, code deploy, database search-replace, object and page cache flushes, a rewrite flush, and a smoke test of the homepage, admin login and checkout. Each step should have a rollback path before the next one starts. Recording the commit SHA and timestamp makes later debugging far easier.