A safe WordPress workflow: staging, deployment and rollback
In this article10 sections
Most WordPress disasters are self-inflicted, and they happen in the same way. Someone updates a plugin on the live site, the checkout breaks, and for the next hour the choice is between restoring last night’s backup (and losing today’s orders) and trying fixes while customers watch. The cure is not to be more careful on the live site. It is to stop experimenting there at all.
This guide describes a simple workflow that agencies, freelancers and site owners with a business to protect can adopt in a day: a staging copy for testing, code kept in version control, a deployment you can repeat the same way every time, and a rollback that takes minutes. The commands use WP-CLI, rsync and git, on a Linux server with SSH access.
The principle: code and content travel in opposite directions
A WordPress site is two things:
- Code: WordPress core, themes and plugins, plus any custom code. It changes when you develop or update.
- Content: posts, pages, orders, comments and users, stored in the database and the uploads folder. It changes when visitors and editors use the site.
On a live site that takes orders, the production database is the truth: it is where customers’ data lives. So the rule is:
- Content flows from production to staging (so staging looks like the real site and tests are realistic).
- Code flows from staging to production (after it has been tested).
Never push the staging database over production. Doing so overwrites orders, comments and new content.
What goes in git, and what does not
Keep in version control:
- Your custom theme or child theme and any custom plugins.
- A
composer.json(if you manage plugins with Composer) or a list of required plugins and versions. - Configuration templates, deployment scripts, and notes.
Keep out:
wp-config.php(it holds database passwords and keys; each environment has its own).- The
uploadsfolder (it is content, large, and belongs to production). - Caches, backups, logs and the
.envequivalents.
Put these in .gitignore. Third-party plugins from the official directory can be installed by name and version rather than stored in git.
Step 1: Create the staging site
Staging should live on a separate address, ideally a subdomain such as staging.example.com, with its own database. Copy the files and the database:
# on the production server
cd /var/www/example.com
wp db export ~/prod-$(date +%F).sql
# on the staging server (or another folder on the same server)
rsync -az --exclude=wp-config.php --exclude=wp-content/cache -e ssh deploy@production:/var/www/example.com/ /var/www/staging.example.com/
wp --path=/var/www/staging.example.com db import ~/prod-$(date +%F).sql
Create a separate wp-config.php for staging with its own database name, user and password. Then change the site’s address inside the database. Always run the dry run first, and skip the guid column, which should never be changed:
cd /var/www/staging.example.com
wp search-replace 'https://example.com' 'https://staging.example.com' --all-tables --skip-columns=guid --dry-run
wp search-replace 'https://example.com' 'https://staging.example.com' --all-tables --skip-columns=guid
Step 2: Make staging safe
A staging site that behaves like production can do real damage. Three protections are not optional:
- Keep search engines out. Block it with HTTP basic authentication in NGINX or Apache (this also keeps clients and bots out), and set WordPress to discourage indexing:
wp option update blog_public 0
- Stop it sending email to real customers. A copy of a shop that sends “your order has shipped” to real addresses is a disaster. Add a small must-use plugin in
wp-content/mu-plugins/no-mail.phpon staging only:
<?php
add_filter('pre_wp_mail', '__return_false');
Many hosts also offer a mail catcher; the principle is that nothing leaves staging.
- Switch off live payments and external integrations. Put the payment gateway in test mode, disable webhooks and marketing syncs, and set staging API keys. Check the plugin settings, because a copied database carries the live keys.
Also set WP_ENVIRONMENT_TYPE to staging in wp-config.php, so that code and plugins can recognise it:
define('WP_ENVIRONMENT_TYPE', 'staging');
Step 3: Work and test on staging
Do the risky things here: update plugins and themes, change the PHP version, test a new feature. Keep a short test list that you run every time:
- Home page, a few inner pages and search.
- Forms send, and you receive the test message somewhere safe.
- Login, account and the admin area.
- Add to cart, checkout in test mode and the confirmation.
- Anything the update claims to change.
Note exactly what you changed, so that you can repeat it on production.
Step 4: Deploy to production
A deployment should be a short, written procedure that you run the same way every time, not a collection of clicks you remember.
# 1. Back up production (database and files) and note where
cd /var/www/example.com
wp db export ~/pre-deploy-$(date +%F-%H%M).sql
# 2. Put the site in maintenance mode if the change touches the database
wp maintenance-mode activate
# 3. Update the code
git pull --ff-only
composer install --no-dev --optimize-autoloader # only if you use Composer
wp plugin update --all # or install the exact tested versions
# 4. Database updates and caches
wp core update-db
wp cache flush
# 5. Leave maintenance mode and check
wp maintenance-mode deactivate
curl -sI https://example.com/ | head -3
If you update plugins on production, update the same versions you tested on staging: running wp plugin update --all days later can install newer versions than the ones you tested. Pinning versions is safer.
Then run the same test list on production, quickly, and watch the error log for the next hour.
Step 5: Roll back when it goes wrong
Decide the rollback before you deploy. For code:
git log --oneline -5
git checkout <previous-commit> # or: git revert <bad-commit>
wp cache flush
For a plugin update, reinstall the earlier version (wp plugin install plugin-name --version=1.2.3 --force). For a database change, restore the dump you took in step 1:
wp db import ~/pre-deploy-2026-10-30-1015.sql
Remember that restoring a database loses everything written since the backup (orders, comments), so a database rollback is a last resort on a busy shop. This is the strongest argument for testing on staging first, and for keeping risky changes small. For a larger-scale setup, a release-folder approach with a symlink that you switch is even faster (see how that works for Laravel releases).
Automatic updates: decide, do not drift
WordPress can update itself. Decide on purpose:
- Minor core releases (security fixes): leave automatic updates on; they rarely break things.
- Plugins and themes: automatic updates are convenient for small low-risk sites, and risky for shops. For important sites, update on staging first, weekly.
- Major core releases: test on staging, then update by hand.
Whatever you choose, you need backups that you have restored once, and monitoring that tells you when the site is broken (see monitoring a small server).
A checklist to keep
- Code in git, content only on production.
- Staging on its own address, with its own database, closed to search engines, with email and payments disabled.
- Production data copied to staging before each round of tests.
- A written test list.
- A written deploy procedure, with a backup first and maintenance mode if needed.
- A rollback you have decided in advance.
- A note of what changed and when.
When it is worth getting help
Setting this up once, with a clean staging environment, a deploy script and a tested rollback, is the kind of investment that pays for itself the first time an update goes wrong. If you manage client sites as an agency or run a shop, I can build this workflow for you, document it, and train your team on it.
You can see my profile on Upwork, or read about WordPress and Laravel care.