トイレのうず

高速化のために X サーバーの X アクセラレータ Ver.2 という機能を有効にしようと思ったら PHP 7.2 が必須だったので、 PHP を 7.2 にしたら WordPress が大量に Warning を吐いていたから修正しました。


[11-Aug-2019 05:27:24 UTC] PHP Warning:  Illegal string offset 'url' in /Volumes/XXXXXX/MAMP/htdocs/theme.1010uzu.com/xxx/wp-content/plugins/external-featured-image-pro/public/class-external-featured-image-public.php on line 152
[11-Aug-2019 05:27:24 UTC] PHP Warning:  Cannot assign an empty string to a string offset in /Volumes/XXXXXX/MAMP/htdocs/theme.1010uzu.com/xxx/wp-content/plugins/external-featured-image-pro/public/class-external-featured-image-public.php on line 152
[11-Aug-2019 05:27:24 UTC] PHP Warning:  Illegal string offset 'alt' in /Volumes/XXXXXX/MAMP/htdocs/theme.1010uzu.com/xxx/wp-content/plugins/external-featured-image-pro/public/class-external-featured-image-public.php on line 153
[11-Aug-2019 05:27:24 UTC] PHP Warning:  Cannot assign an empty string to a string offset in /Volumes/XXXXXX/MAMP/htdocs/theme.1010uzu.com/xxx/wp-content/plugins/external-featured-image-pro/public/class-external-featured-image-public.php on line 153
[11-Aug-2019 05:27:24 UTC] PHP Warning:  Illegal string offset 'caption' in /Volumes/XXXXXX/MAMP/htdocs/theme.1010uzu.com/xxx/wp-content/plugins/external-featured-image-pro/public/class-external-featured-image-public.php on line 154
[11-Aug-2019 05:27:24 UTC] PHP Warning:  Cannot assign an empty string to a string offset in /Volumes/XXXXXX/MAMP/htdocs/theme.1010uzu.com/xxx/wp-content/plugins/external-featured-image-pro/public/class-external-featured-image-public.php on line 154

上のが表示されているサムネイル分出てきます。

どれも External Featured Image Pro というプラグインが問題でした。管理画面のプラグイン画面での名前は External Featured Image Pro ですが、プラグインのダウンロードのページは External Featured Image Plus になっています。ややこしいです。

→ Wordpress.org : External Featured Image Plus

Warning の Illegal string offset は変数が暗黙のうちに配列に変換されなくなったことが原因で、 Cannot assign an empty string to a string offset も配列の扱いが変わり空の文字列を配列として定義されていない変数に代入できないことが原因のようです。

→ namaozi ’ s memo : https://scrapbox.io/namaozi/PHP7%E3%81%A7Illegal_string_offset%E3%81%8C%E5%87%BA%E3%82%8B

問題箇所は external-featured-image-pro/public/class-external-featured-image-public.php の 150 行目付近です。


		$post_meta = get_post_meta($post_id, '_' . $this->plugin_slug, true);
		$post_meta['url'] = !empty($post_meta['url']) ? $this->relative_url(esc_url($post_meta['url'])) : null;
		$post_meta['alt'] = (!empty($post_meta['url']) && !empty($post_meta['alt'])) ? esc_attr($post_meta['alt']) : null;
		$post_meta['caption'] = (!empty($post_meta['url']) && !empty($post_meta['caption'])) ? esc_attr($post_meta['caption']) : null;

get_post_meta() で取得した値を代入した変数に三項演算子を使って配列として代入しているのですが、取得した値が空の文字列の場合と配列の場合があり、空の文字列の場合に文字列に配列を代入しようとしているので暗黙の型変換がされず Warning を吐いているようです。

このプラグインの最終更新日は 4 年前でメンテナンスされていないようなので、もうアップデートされないだろうと思い Warning を吐かないようにコードを自分で改変しました。


		$post_meta = get_post_meta($post_id, '_' . $this->plugin_slug, true);
		if(is_array($post_meta)){ //追加
			$post_meta['url'] = !empty($post_meta['url']) ? $this->relative_url(esc_url($post_meta['url'])) : null;
			$post_meta['alt'] = (!empty($post_meta['url']) && !empty($post_meta['alt'])) ? esc_attr($post_meta['alt']) : null;
			$post_meta['caption'] = (!empty($post_meta['url']) && !empty($post_meta['caption'])) ? esc_attr($post_meta['caption']) : null;
		}else{	//追加
			$post_meta = array('url' => null, 'alt' => null, 'caption' => null);	//追加
		}	//追加

まず get_post_meta() で取ってきた値が配列の場合のみ三項演算子の元のコードを実行して、配列でなかった場合は $post_meta に添え字つきの配列を作成してそれぞれに null を代入しています。この条件分岐を三項演算子を使ってやっているような気もしますが、どんな値が入ってくるかわからにので、三項演算子も残しました。

もっとよいコードがあると思いますがとりあえず動いているのでこれでよいかと思います。しかも X アクセラレータ Ver.2 は全然早くならない上に、 PHP のプログラムまでキャッシュするから変更を加えたところがすぐに反映されないというおまけつき。なので X アクセラレータ Ver.1 に戻しました。

関連記事

WordPress の Ktai Style を PHP 7 で動作するように修正した
WordPress
thumbnail
いろいろと検討中
WordPress
thumbnail
Smart Update Pinger の改変に泣く
WordPressMac
thumbnail
PHP: Fatal error: Allowed memory size of XXXXX bytes exhausted. が出た
WordPress
thumbnail
ハッスルサーバーが PHP5 に対応する!
WordPressWeb制作
thumbnail
MAMP で WordPress をローカル環境にインストール
WordPressMac
MAMP 許諾画面