1

The function create_function has been deprecated as of PHP 7.2. Worked previously in PHP 5 but after updating PHP crashes.

$func = create_function('$atts, $content = null','return "<div class=\"' . $class_list . '\">" . do_shortcode($content) . "</div>";' );
add_shortcode($shortcode, $func);
}

Can anyone help me in fixing my code?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • You pass in `$atts` but don't use it and you use `$class_list` but you don't pass it in. – AbraCadaver Jul 02 '18 at 19:16
  • 2
    A lot of missing stuff from this example, makes it a bit more confusing than it should be... as it looks like you just want to assign $func an anonymous function. Which is more native now in php 7. – IncredibleHat Jul 02 '18 at 19:17
  • What is the error you get when it crashes? – Barmar Jul 02 '18 at 19:46

2 Answers2

2

Since PHP 5.3, the preferred way to do this has been with anonymous functions. To capture the value of an external variable, use a use declaration. So it should be:

$func = function($atts, $content = null) use ($class_list) {
    return "<div class='$class_list'>" . do_shortcode($content) . "</div>"; 
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

Use an Anonymous Function instead.

In your question, the arguments and the vars used in the code don't seem to match up. Perhaps $class_list is in the parent scope? But what about $atts then? So, I've assumed you actually wanted to pass $class_list. Adjust accordingly.

$func = function($class_list, $content = null)
{
    return "<div class='$class_list'>" . do_shortcode($content) . "</div>"; 
}

add_shortcode($shortcode, $func);
DFriend
  • 8,869
  • 1
  • 13
  • 26
  • Since `$class_list` isn't passed as a parameter, maybe it should be `use($class_list)`? – Barmar Jul 02 '18 at 19:46
  • Yes, it the OP's code is accurate. Between us, we seem to have the bases covered - except for `$atts`. Why is it passed and not used? A better explanation of the context would answer many questions. – DFriend Jul 02 '18 at 20:40