What is the best strategy to switch from localhost to another environment in wordpress?

Viewed 75

I have a wordpress which works fine in localhost. Now we need to deploy it on our testing environment using docker. Deploy php files with docker was fine. I configured worpdress to use environment variables instead hardcoded values in wp-config.php.

docker run -d --name wordpress -it --rm -p 80:80 \
-e DB_HOST=10.10.10.10:3306 \
-e DB_USER=root \
-e DB_PASSWORD=secret \
-e DB_NAME=wordpress \
-e AUTH_KEY=$RANDOM_KEY \
-e SECURE_AUTH_KEY=$RANDOM_KEY \
-e NONCE_KEY=$RANDOM_KEY \
-e LOGGED_IN_KEY=$RANDOM_KEY \
-e AUTH_SALT=$RANDOM_KEY \
-e SECURE_AUTH_SALT=$RANDOM_KEY \
-e LOGGED_IN_SALT=$RANDOM_KEY \
-e NONCE_SALT=$RANDOM_KEY \
-e WP_DEBUG=true \
-e DISABLE_WP_CRON=true \
-e TZ=America/Lima wordpress:5.7.2

The problem is in the database deployment. I exported a dump from my localhost and imported to my new testing database. I saw that in the database dump, all the urls are hardcoded with localhost:

table wp_options
'siteurl','http://localhost'

table wp_posts
'you should go to <a href=\"http://localhost/wp-admin/...''

table wp_users
1,'admin','****','admin','admin@mail.com','http://localhost'

When I try to access to my testing domain, assets are being downloaded from localhost, ignoring my domain. I understand that this behavior is due to the hardcoded "localhost" in the exported db from developer machine.

errors

Research

Questions

  • What is the best strategy to move from localhost to another environment in wordpress for a devops pipeline in which all is handled with variables and human intervention is not required?
  • Would a simple string replacement (localhost > wordpress.testing.com) in the mysql dump work?
1 Answers

The wordpress domain is controlled by setting 2 database options:

  • site_url
  • home

If you don't want to change it in database you can do it in wp-config.php

define('WP_SITEURL', $_ENV["USER"]);
define('WP_HOME', $_ENV["HOME"]);

You can make it read from an environment variable and then set that inside the container.

Related