LoginSignup
16
21

More than 5 years have passed since last update.

Wordpressのwp-config.phpに本番環境とテスト(ローカル)環境の設定を併記する

Last updated at Posted at 2017-04-11

よくgitで管理してて、設定ファイルだけ管理外に指定してて、たまにサーバー同期して上書きとか消しちゃったりとかやらかしてしまうことがあるので忘備録。

Wordpressの設定ファイル wp-config.php 内に、サーバー変数 $_SERVER["SERVER_NAME"] で分岐して書くだけです。
ifの分岐を増やせば、ローカル環境やテスト環境も増やせます

// Develop(localhost)
if (@$_SERVER["SERVER_NAME"] === 'localhost') {
    define('DB_NAME', 'test_db');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'root');
    define('DB_HOST', 'localhost');
    define('DB_CHARSET', 'utf8');
    define('DB_COLLATE', '');
    //サイトURL
    define('WP_HOME','http://localhost');
    //WordPress URL インストールしたフォルダ
    define('WP_SITEURL','http://localhost/wp');
    //デバック有効化
    define('WP_DEBUG', true);
} else {
    //本番環境
    define('DB_NAME', 'hogehoge');
    define('DB_USER', 'hogehoge');
    define('DB_PASSWORD', 'hogehoge');
    define('DB_HOST', 'hogehoge');
    define('DB_CHARSET', 'utf8');
    define('DB_COLLATE', '');
    //デバック無効化
    define('WP_DEBUG', false);
}

ポイントは、以下の2行。

define('WP_HOME','http://localhost');
define('WP_SITEURL','http://localhost/wp');

Wordpressのダッシュボードの一般設定で定義する箇所ですが、
ここを指定するおかげでバックアップしたSQLを検索置換する手間が省けます。

参考)

16
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
21