Recently I’ve taken to using a simple setup for local WordPress development (which you can read about and watch here: Local WordPress with Herd, DBngin, and WP-CLI).
When you’re not using a local dev tool that’s managed by your hosting company (for me that was usually WordPress VIP or WP Engine), you typically have to set up your own way to pull down any changes from production. You can definitely use a backup/staging plugin, but with WP-CLI running on any good host, why not speed the whole process up and run it with one simple terminal command?
Q: What about pushing changes from local to production? A: I use git! Content/database starts on prod and only comes down. Code goes up.
So here’s the script I’ve been using. Running it triggers a few things:
- Exports the database on production.
- Pulls the entire
wp-content
folder & database export down (using rsync, meaning it should skip any files you already have) - Deletes the remote database dump
- Imports the database locally and runs
search-replace
to swap out the production domain with the staging domain.
That’s it! There’s probably other stuff you’ll have to set up on your local environment, and maybe you can add a workaround for media files and skip the uploads
folder. Up to you.
There’s a few things to note:
- This assumes you already have a local WordPress environment set up.
- This file goes in the root folder of your local WordPress installation. You can run it by simply running
bash pull.sh
in your terminal. - You’ll need to set it up with your SSH credentials, and then make sure the
REMOTE_PATH
points to the root folder of the production site (relative to where you land when you log in with those credentials), while theLOCAL_PATH
points to your localwp-content
folder. - Check out the various
exclude
flags for things I don’t like to download, like hosting-specific plugins or logs. - For non-essential sites, I update plugins / theme on production, so I have this pull down any updates to the
plugins
directory. You could exclude that as well.
That’s it. Any ideas on how to optimize it? I’d love to hear them.
Here’s the full script:
Edit: I did add one additional line to the end of my import script:
wp plugin install --activate create-block-theme
This installs and activates the Create Block Theme plugin on my local environment so that I can export any changes I make to the templates and version control them.
Leave a Reply