0

I have a function for the WordPress settings where the create_function() was used but when I updated the server to PHP7.2, and it says the function is deprecated.

I don't know what alternative to use.

Code

if ( isset( $section['desc'] ) && ! empty( $section['desc'] ) ) {
  $section['desc'] = '<div class="inside">' . $section['desc'] . '</div>';
  $callback = create_function( '', 'echo "' . str_replace( '"', '\"', $section['desc'] ) . '";' );
} elseif ( isset( $section['callback'] ) ) {
  $callback = $section['callback'];
} else {
  $callback = null;
}
yivi
  • 42,438
  • 18
  • 116
  • 138
Maqk
  • 515
  • 9
  • 26
  • There is might help [Function create_function() is Deprecated in PHP 7.2 - How to Migrate?](https://www.tomasvotruba.cz/blog/2018/12/17/function-create-function-is-deprecated-in-php-72-how-to-migrate/) – Tomas Votruba Dec 18 '18 at 01:17
  • Possible duplicate of [PHP 7.2 Function create\_function() is deprecated](https://stackoverflow.com/questions/48161526/php-7-2-function-create-function-is-deprecated) – Tomas Votruba Dec 18 '18 at 01:17

1 Answers1

4

Just create an anonymous function to use as a callback:

Your line:

$callback = create_function( '', 'echo "' . str_replace( '"', '\"', $section['desc'] ) . '";' );

Could be replaced with:

$callback = function() use($section) { echo str_replace ('"', '\"', $section['desc']); };
yivi
  • 42,438
  • 18
  • 116
  • 138