PHP8に備えてcreate_functionを修正する

PHP7.1からPHP7.2にアップデートしたところDeprecatedが出ました。create_function関数が非推奨になったとのことなので早速修正します。

Simplicity2テーマのウィジェットがcreate_functionを使用しているのが原因

検証用WordPressは常にデバッグを有効化しているため、エラーが出るとサイトの上に色々と表示してくれます。今回はこんな感じで出てました。(本当は10行もあります。)

ログをしっかり読んでみると、simplicity2テーマのウィジェット用コードがcreate_function関数を使っているのが原因のようです。

Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/new-popular.php on line 243
Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/new-entries.php on line 139
Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/sns-follow-buttons.php on line 50
Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/mobile-text.php on line 67
Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/pc-text.php on line 68
Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/mobile-ad.php on line 87
Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/pc-ad.php on line 84
Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/pc-double-ads.php on line 79
Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/recent-comments.php on line 141
Deprecated: Function create_function() is deprecated in /var/www/html/wp-content/themes/simplicity2/lib/widgets/classic-text.php on line 61
対策:create_functionの修正

PHPの知識が乏しいのでどうやったら修正できるのか情報を集めて対策します。
するとWordPressの公式ドキュメント「WordPressウィジェットAPI」の中に、PHP5.2用にcreate_functionを使用してフックする方法と、PHP5.3以上でfunctionを利用する方法の記述があったので、これを真似してsimplicity2テーマの該当コードを修正してみたところ、無事Deprecated表記が消えました。

/var/www/html/wp-content/themes/simplicity2/lib/widgets/classic-text.php- add_action('widgets_init', create_function('', 'return register_widget("SimplicityClassicTextWidget");'));
+ add_action('widgets_init', function(){register_widget('SimplicityClassicTextWidget' );});
対策の効果は?

デバッグ有効でもDeprecatedが出なくなりました。それだけです。

スポンサーリンク