勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

PHP8系で「Warning: Constant xxxxx already defined in」エラーの対応方法

  PHP WordPress CMS

PHP8系&WordPress6.4.3で「Warning: Constant xxxxx already defined in」というエラーログが見受けられた。「/wordpress/wp-content/db.php」「/wordpress/wp-config.php」にて発生している模様。以下に対応方法をメモ。

 

原因

定数が二重に定義されている(定義済みの定数を新たに定義した)場合に発生するエラーとなる。

 

対応方法

definedで定数が定義済みかチェックする。具体的には以下のような形。

if( !defined('xxxxxxxxx') ){
    define('xxxxxxxxx', true );
}

 

エラー詳細と定義ファイルについて

/wordpress/wp-content/db.php

エラー詳細

以下エラーログが出力されていた。

Warning: Constant EZSQL_VERSION already defined in /wordpress/wp-content/db.php on line 43
Warning: Constant OBJECT already defined in /wordpress/wp-content/db.php on line 48
Warning: Constant OBJECT_K already defined in /wordpress/wp-content/db.php on line 53
Warning: Constant ARRAY_A already defined in /wordpress/wp-content/db.php on line 58
Warning: Constant ARRAY_N already defined in /wordpress/wp-content/db.php on line 63

定義ファイル

ソースコードを追ってみると「/wordpress/wp-includes/wp-db.php」内で定義されていた。

どうもこちらのページを見てみるとwp-db.phpの方がコアファイルでdb.phpは拡張機能ファイルにあたるらしい。とはいえdb.phpを作成した覚えが無いんだけど……。

/wordpress/wp-config.php

エラー詳細

以下エラーログが出力されていた。

Warning: Constant WP_POST_REVISIONS already defined in /wordpress/wp-config.php on line 91

こちらはリビジョン数を指定するためにwp-config.phpで定義した覚えがある。

定義ファイル

「/wordpress/wp-includes/default-constants.php」内で定義されていた。

注意点

リビジョン数を指定する場合はwp-config.php内の以下部分より上で設定する必要あり。

/** Absolute path to the WordPress directory. */
if ( !defined('ABSPATH') )
    define('ABSPATH', dirname(__FILE__) . '/');
/** Sets up WordPress vars and included files. */
require_once(ABSPATH . 'wp-settings.php');

上記部分より下で定義してしまうと先にdefault-constants.php側で定義されてしまうようでリビジョン数が指定できなかった(var_dumpで確認済み)。

 

所感

WordPressに関してNotice以外のエラーは全て対応できたので満足。尚、WordPressのエラー対応時はwp-config.phpで以下のように設定し、自分のみデバッグモードにすると良い。

if ( $_SERVER["REMOTE_ADDR"] == "自身の接続IPアドレス" ){
    define('WP_DEBUG', true);
}else{
    define('WP_DEBUG', false);
}

 - PHP WordPress CMS

  関連記事

WordPressの記事をfacebookに投稿する際のOGP設定について

WordPressの記事をfacebookに投稿する際にOGP設定をする必要があ ...

WordPressのサーバー移行で気付いた点

あるWordPressを置いているサーバーのリソースが苦しくなってきたので、新た ...

WordPressで「No URL was~」というエラーの原因と対応方法

WordPressでmp4ファイルをアップロードして記事に埋め込んだものの、記事 ...

WordPress Popular Postsのサムネイルが表示されない

WordPressの人気記事を表示させるWordPress Popular Po ...

Polylangのスイッチャーとページャーの組み合わせ方法

WordPressで多言語サイトを作成するにあたりPolylangというプラグイ ...