概要

mainwpというWordPressがあり、そのディレクトリ配下に /subwp という階層に別のWordPressが存在したとする。
WordPressはデフォルトではDocumentRoot直下の階層、またはDocumentRootの一つ上の階層のwp-config.phpを読むようになっており、wp-config.phpをDocumentRoot配下に保存したくない場合は、通常は一つ上の階層にwp-config.phpを移動させるだけでWordPressが読みこんでくれる。

しかし、上記の環境のように、mainwp/subwp という階層だと、一つ上にはすでに別のWordPressが存在してしまい、それぞれ一つ上においても、DocumentRoot直下のwp-config.phpを先に読み込んでしまい、おかしなことになる。

解決方法

  1. subwpのwp-configを保存したいディレクトリ(2階層上とか)にコピーする
[root@test.inamuu.com subwp]# ll /home/hogeuser/mainwp/subwp_config/
合計 16
-r--r----- 1 hogeuser www 4342  8月 30 23:46 wp-config.php

2.wp-load.phpを編集する

[root@test.inamuu.com subwp]# diff -u wp-load.php{.bak,}
--- wp-load.php.bak     2018-08-31 00:02:08.147884691 +0900
+++ wp-load.php 2018-08-31 00:03:07.828911588 +0900
@@ -31,10 +31,13 @@
  *
  * If neither set of conditions is true, initiate loading the setup process.
  */
-if ( file_exists( ABSPATH . 'wp-config.php') ) {
+if ( file_exists( '/home/hogeuser/mainwp/subwp_config/wp-config.php') ) {
+/* if ( file_exists( ABSPATH . 'wp-config.php') ) {

        /** The config file resides in ABSPATH */
-       require_once( ABSPATH . 'wp-config.php' );
+       require_once( '/home/hogeuer/mainwp/subwp_config/wp-config.php' );
+       /* require_once( ABSPATH . 'wp-config.php' );
+        */

 } elseif ( @file_exists( dirname( ABSPATH ) . '/wp-config.php' ) && ! @file_exists( dirname( ABSPATH ) . '/wp-settings.php' ) ) {


3.DocumentRoot配下のwp-config.phpを削除する
4.サイトにアクセスできるか確認する

あまり考えたことがなかったけど、やってみるとWordPressがどのようにファイルを読み込んでいるのかが少しだけわかったような気がする。

カテゴリー: PHPWordPress