Skip to main content

WordPress WP-CLI Automation Scripts

Create WP-CLI automation scripts for WordPress management including deployments, database operations, content migration, and maintenance tasks.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
You are a WordPress DevOps engineer specializing in WP-CLI automation. Help me create a suite of automation scripts for managing a WooCommerce store WordPress site.

Step 1: Create a deployment automation script (deploy.sh) using WP-CLI. The script should: pull the latest code from main, run composer install for plugin dependencies, run wp core update if a new version is available (with minor only (skip major) strategy), run wp plugin update --all (excluding woocommerce, advanced-custom-fields-pro which are managed via Composer), run wp theme update --all, flush all caches (wp cache flush, wp rewrite flush), and run database migrations. Add safety checks: verify WP-CLI is installed, check PHP version compatibility, and create a database backup before any updates.

Step 2: Build a database management toolkit. Create scripts for: wp db export with timestamped filename to /backups/wordpress with 7 backup retention, wp db import with automatic URL replacement (wp search-replace https://example.com https://staging.example.com --precise --all-tables), database optimization (wp db optimize, wp db repair), and a script that safely copies the production database to the staging environment with PII anonymization (replace emails, names, and addresses with faker data).

Step 3: Create a content migration script that transfers products and orders content between environments. Use wp export to generate WXR files filtered by post type, date range, and author. Build the import script using wp import with --authors=mapping.csv for author reassignment. Handle media attachments by downloading them from https://old-site.com and re-attaching to imported posts. Verify migration completeness by comparing post counts and checking for broken internal links.

Step 4: Build a maintenance automation suite. Create a cron-ready script that runs daily at 3 AM: wp cron event run --due-now (process pending scheduled events), wp transient delete --expired (clean expired transients), wp comment spam delete (remove spam comments), wp media regenerate --only-missing (regenerate missing image sizes), and custom cleanup for post revisions older than 30 days, auto-drafts. Log each operation result to /var/log/wordpress/maintenance.log and send a summary to Slack webhook.

Step 5: Create a site health monitoring script. Use WP-CLI to check: wp core verify-checksums (detect modified core files), wp plugin verify-checksums --all (detect modified plugin files), wp option get (verify critical options like siteurl, home, blogname), wp user list --role=administrator (audit admin accounts), wp cron event list (verify scheduled events are running), and test that homepage, shop, cart, checkout, my-account return HTTP 200. Output a JSON health report and exit with non-zero status if any check fails.

Step 6: Build a WP-CLI custom command plugin that adds project-specific commands. Register commands under the wp mysite namespace: wp mysite setup (first-time environment setup), wp mysite sync (pull content from production), wp mysite seed (populate test data using factories), wp mysite cleanup (remove test data), and wp mysite report (generate site statistics). Follow WP-CLI command best practices: use WP_CLI::success(), WP_CLI::error(), progress bars for long operations, and --dry-run support.

What this prompt does

This prompt makes the model a WordPress DevOps engineer producing a suite of WP-CLI automation scripts for a [site_type] site. It covers six scripts: deployment, database management, content migration, scheduled maintenance, health monitoring, and a custom WP-CLI command plugin.

The deployment script pulls from [git_branch], runs Composer and core/plugin/theme updates (with [update_strategy] and [excluded_plugins] left to Composer), flushes caches, and backs up the database first. The database toolkit handles timestamped exports to [backup_directory] with [retention_count] retention, URL search-replace between [production_url] and [staging_url], and PII anonymization when copying to [staging_environment]. Maintenance runs [maintenance_frequency], logging to [log_file] and alerting [notification_channel].

What makes the suite trustworthy is that every script is built to run unattended. The health monitor verifies core and plugin checksums, audits administrator accounts, checks scheduled events, and confirms [critical_pages] return HTTP 200, emitting JSON and a non-zero exit code when anything fails. The custom command plugin registers wp [command_namespace] subcommands for setup, sync, seed, cleanup, and reporting, all following WP-CLI conventions like success and error helpers, progress bars, and --dry-run. Treating WordPress operations as versioned code is what makes them repeatable across environments rather than a series of manual clicks.

When to use it

  • You manage a WordPress site and want deploys scripted, not clicked through.
  • You need safe production-to-staging database sync with PII stripped.
  • You migrate [content_type] content between environments repeatedly.
  • You want unattended maintenance with logging and [notification_channel] alerts.
  • You need health checks that fail loudly when something drifts.
  • You want project-specific commands under a wp [command_namespace] namespace.

Example output

Expect a set of shell and PHP scripts: a deploy.sh with pre-flight checks and a pre-update backup, database export/import scripts writing to [backup_directory] with [retention_count] retention and search-replace from [production_url] to [staging_url], a migration script using WXR export/import, a cron-ready maintenance script logging to [log_file], a health script emitting JSON and a non-zero exit on failure, and a command plugin registering wp [command_namespace] subcommands with --dry-run support.

Pro tips

  • Always create the pre-update backup before touching core or plugins; the deploy script should refuse to continue without it.
  • Keep [excluded_plugins] aligned with whatever Composer manages so auto-updates do not fight your dependency lock.
  • Anonymize PII on every copy to [staging_environment], not just production-to-prod moves.
  • Build --dry-run into the custom wp [command_namespace] commands so destructive ones can be previewed.
  • Set [retention_count] against your disk space; timestamped backups in [backup_directory] add up fast.
  • Route failures to [notification_channel] and write everything to [log_file] so unattended runs are auditable.
  • Pin [git_branch] and [update_strategy] to match your release process, since a deploy script that pulls the wrong branch is worse than no automation.
  • Run the health-check script in CI as well as cron, so checksum drift on [critical_pages] is caught before it reaches production.

Frequently Asked Questions

Does the deployment script protect against a bad update?
It creates a database backup before any core or plugin updates and runs pre-flight checks for WP-CLI, PHP version, and compatibility. If a backup or check fails, the script is meant to stop rather than push a risky update.
How does it keep staging data safe when copying from production?
When copying the production database to `[staging_environment]`, it anonymizes PII by replacing emails, names, and addresses with faker data. It also runs a URL search-replace from `[production_url]` to `[staging_url]` so links resolve correctly.
Can I preview what a destructive command will do first?
Yes. The custom `wp [command_namespace]` commands follow WP-CLI best practices including `--dry-run` support, so cleanup and sync operations can be previewed before they actually change anything. This is what makes them safe to run unattended.
What happens when a scheduled maintenance run fails?
Each operation logs its result to `[log_file]` and a summary is sent to `[notification_channel]`. The health-check script also exits with a non-zero status when any check fails, so a cron monitor can catch and alert on problems.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in WordPress & CMS Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support