Skip to content

Instantly share code, notes, and snippets.

@retlehs
Last active January 26, 2022 03:48
Show Gist options
  • Save retlehs/aa47003249623e52b95a3fcd6f69b8fb to your computer and use it in GitHub Desktop.
Save retlehs/aa47003249623e52b95a3fcd6f69b8fb to your computer and use it in GitHub Desktop.
WP-CLI aliases sync example
read -r -p "Would you really like to reset your development database and pull the latest from production? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
wp @development db reset --yes &&
wp @production db export - > sql-dump-production.sql &&
wp @development db import sql-dump-production.sql &&
wp @development search-replace https://example.com https://example.dev
fi
path: web/wp
@production:
ssh: [email protected]/srv/www/example.com/current
@development:
ssh: [email protected]/srv/www/example.com/current
@gitowiec
Copy link

Ok I'm done ! :D it is great script! Here's my version

#!/usr/bin/env bash
read -r -p "Would you really like to reset your development database and pull the latest from production? [y/N] " response

if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
    wp @staging db reset --yes &&
    wp db export sql-dump-dev.sql --add-drop-table &&
    wp @staging db import - < sql-dump-dev.sql &&
    wp @staging search-replace http://strefatradera.l http://strefatradera.ufff.pl
fi

@ttcremers
Copy link

I've always been a bit fuzzy on this, why use the search-replace on the sites url when it's much easier to just set WP_HOME and WP_SITEURL in the wp config

@taylorgorman
Copy link

I would guess because the WordPress editor inserts the site URL when adding internal links and img tags. So you have to change those strings in the post_content field for every post.

Those URLs also exist in the wp_options table, which is from where all the WordPress core functions get them (I'm mostly sure). You could use the wp-config constants in your own themes and plugins, but other plugin devs will still use them. ..Although I suppose you could hook into those functions and switch them to pull from the constants. But you'd still have the post_content problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment