WordPress一般設定のWordPressURLとサイトURLを誤入力したので設定を戻した

「WordPress アドレス (URL)」と「サイトアドレス (URL)」設定画面

はじめに

テストサイトでWordPressの挙動確認のため設定変更を繰り返していたのだが,WordPress アドレス (URL) とサイトアドレス (URL) の設定ミスでWordPress管理画面に入れなくなったので,その設定の戻し方に「設定ファイル修正」二通りと「MySQLデーターベース修正」二通りの計4手順の復旧方法についてまとめてみた。

復旧手順

復旧の4手順を記載したが,状況に応じて選べば良いと思う。お薦めは「phpMyAdminを使用してDB更新」だがphpMyAdminを使用できない場合は「DB設定を書き換え(update_option)」の手順が簡単。

それぞれの設定項目名が下記のように異なっているので注意

WordPressの設定項目名wp-config.phpの一時的設定項目名DB更新時の項目名
WordPress アドレス (URL)WP_SITEURLsiteurl
サイトアドレス (URL)WP_HOMEhome

設定ファイル修正

WordPressのインストールフォルダーの「wp-config.php」に設定コードを追加する。
設定方法は「データーベースの書き換え」と「一時的な設定変更」がある。
下記に二通りの方法のコードを記載した。(94-95行,108-109行)
実際に設定する際はどちらか一方を指定する。

DB設定を書き換え(update_option)

「wp-config.php」一番最後に下記の2行を書き加える。
追加位置は上記の108行と109行参考
データーベース設定を書き換えるので,管理画面にログイン出来たらこの設定を削除(コメント)する。

/** サイトURL・ホームURLのデーターベース更新*/
update_option( 'siteurl', 'https://example.com/wordpress' );
update_option( 'home', 'https://example.com' );

一時的な設定変更(define)

「wp-config.php」の「編集が必要なのはここまでです」の前に下記の2行を書き加える。
追加位置は上記の94行と95行参考
データーベース設定を残して一時的に書き換えるので,この設定を削除するとデーターベースの設定が有効となって誤入力のURLに戻る。
この設定はWordPress管理画面の設定項目窓がグレーアウトして画面から変更できなくなるので,設定変更されたく無い場合はこの方法が有効。

/** サイトURL・ホームURLの一時的設定*/
define( 'WP_SITEURL', 'https://example.com/wordpress' );
define( 'WP_HOME', 'https://example.com' );

直接データーベースを編集

MySQLデーターベースを直接書き換える方法は,ツール(phpMyAdmin)を使用する方法とコマンドで更新する方法がある。

phpMyAdminを使用してDB更新

「WordPress アドレス (URL)」や「サイトアドレス (URL)」を誤設定して管理画面に入れなくなった場合はphpMyAdminからMySQLデーターベースを開いて,直接編集する。
テーブル「wp_options」-フィールド「option_name」の値「siteurl」又は「home」の「編集」をクリックして編集画面から「options value」の内容を変更する。

テーブル「wp_options」-フィールド画面
フィールド「option_name : siteurl」の編集画面

MySQLコマンドでDB更新

この方法はphpMyAdminが使用できない場合等にのみ使うべきであり,慎重に行う。

MYSQLのインストールフォルダーからコマンドプロンプトを起動。
手順
1.インストールフォルダー「D:\Program Files\MySQL\bin」
2. rootユーザーでログイン      「mysql -u root -p」
3. パスワード入力          「パスワード」
4.更新対象データーベース確認   「show databases;」
5.更新対象データーベース選択   「use データーベース名;」
6.更新対象テーブル確認    「show tables;」
7.更新対象テーブル更新前詳細確認 「show full columns from テーブル名;」
8.更新対象フィールド更新前詳細確認
            「select フィールド名 from テーブル名 where 条件;」
 ”条件”は対象レコードのフィールド名=”値”を指定
  (例:テーブルの先頭レコードの場合はoption_id=”1″やoption_name=”siteurl”) 
9.テーブル内のフィールド更新
     「update テーブル名 set フィールド名=”フィールド値” where 条件;」
10.更新対象フィールド更新後詳細確認
            「select フィールド名 from テーブル名 where 条件;」
11.更新対象テーブル更新後詳細確認 「show full columns from テーブル名;」

データーベース一覧確認
mysql> show databases;

データーベース選択
mysql> use example_db;

テーブル一覧確認
mysql> show tables;

更新対象テーブル詳細確認
mysql> show full columns from wp_options;

更新対象フィールド詳細確認
mysql> select option_value from wp_options where option_name="siteurl";
mysql> select option_value from wp_options where option_name="home";

テーブル(フィールド)更新
mysql> update wp_options set option_value="https://example.com/wordpress" where option_name="siteurl";
mysql> update wp_options set option_value="https://example.com" where option_name="home";

コマンド実行結果

同一内容変更なしで上書き設定してみた。
WordPress アドレス (URL):https://example.com/wordpress
サイトアドレス (URL):https://example.com

Microsoft Windows [Version 10.0.14393]
(c) 2016 Microsoft Corporation. All rights reserved.

D:\Program Files\mysql\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11762
Server version: 8.0.34 MySQL Community Server - GPL

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| sakila             |
| sys                |
| example_db         |
| world              |
+--------------------+
12 rows in set (0.00 sec)

mysql> use example_db;
Database changed

mysql> show tables;
+--------------------------------------+
| Tables_in_example_db                 |
+--------------------------------------+
| wp_actionscheduler_actions           |
| wp_actionscheduler_claims            |
| wp_actionscheduler_groups            |
| wp_actionscheduler_logs              |
| wp_aioseo_cache                      |
| wp_aioseo_crawl_cleanup_blocked_args |
| wp_aioseo_crawl_cleanup_logs         |
| wp_aioseo_notifications              |
| wp_aioseo_posts                      |
| wp_aioseo_writing_assistant_keywords |
| wp_aioseo_writing_assistant_posts    |
| wp_commentmeta                       |
| wp_comments                          |
| wp_fit_post_accesslog                |
| wp_links                             |
| wp_mjuh_datas                        |
| wp_mjuh_logs                         |
| wp_ngg_album                         |
| wp_ngg_gallery                       |
| wp_ngg_pictures                      |
| wp_options                           |
| wp_postmeta                          |
| wp_posts                             |
| wp_ppress_coupons                    |
| wp_ppress_customers                  |
| wp_ppress_forms                      |
| wp_ppress_formsmeta                  |
| wp_ppress_meta_data                  |
| wp_ppress_ordermeta                  |
| wp_ppress_orders                     |
| wp_ppress_plans                      |
| wp_ppress_sessions                   |
| wp_ppress_subscriptions              |
| wp_pz_linkcard                       |
| wp_statistics_events                 |
| wp_statistics_exclusions             |
| wp_statistics_historical             |
| wp_statistics_pages                  |
| wp_statistics_useronline             |
| wp_statistics_visit                  |
| wp_statistics_visitor                |
| wp_statistics_visitor_relationships  |
| wp_term_relationships                |
| wp_term_taxonomy                     |
| wp_termmeta                          |
| wp_terms                             |
| wp_usermeta                          |
| wp_users                             |
+--------------------------------------+
48 rows in set (0.02 sec)

mysql> show full columns from wp_options;
+--------------+-----------------+------------------------+------+-----+---------+----------------+---------------------------------+---------+
| Field        | Type            | Collation              | Null | Key | Default | Extra          | Privileges                      | Comment |
+--------------+-----------------+------------------------+------+-----+---------+----------------+---------------------------------+---------+
| option_id    | bigint unsigned | NULL                   | NO   | PRI | NULL    | auto_increment | select,insert,update,references |         |
| option_name  | varchar(191)    | utf8mb4_unicode_520_ci | NO   | UNI |         |                | select,insert,update,references |         |
| option_value | longtext        | utf8mb4_unicode_520_ci | NO   |     | NULL    |                | select,insert,update,references |         |
| autoload     | varchar(20)     | utf8mb4_unicode_520_ci | NO   | MUL | yes     |                | select,insert,update,references |         |
+--------------+-----------------+------------------------+------+-----+---------+----------------+---------------------------------+---------+
4 rows in set (0.00 sec)

mysql> select option_value from wp_options where option_name="siteurl";
+--------------------------------+
| option_value                   |
+--------------------------------+
| https://example.com/wordpress  |
+--------------------------------+
1 row in set (0.02 sec)

mysql> select option_value from wp_options where option_name="home";
+----------------------+
| option_value         |
+----------------------+
| https://example.com  |
+----------------------+
1 row in set (0.00 sec)

mysql> update wp_options set option_value="https://example.com/wordpress" where option_name="siteurl";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql> update wp_options set option_value="https://example.com" where option_name="home";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 0

mysql>

おわりに

普段は触ることがない設定だが,いざアクセス出来なくなったら慌てることになり,ましてや本番サイトともなれば頭が真っ白となり思考が止まってしまうことになる。手順を記載しておくことで,いざとなった時に役に立つものと思う。今回はテストサイトで設定変更後の影響についてある程度想定済みで,やっぱりかといった感じだった。しかし何度も起こらないほうが良いに越したことは無い。