0

I'm creating a dynamic function in WordPress that requires I create my function names dynamically.

In this instance, I need to generate a unique function name for this gravityforms code:

add_filter( 'gform_validation_message', 'change_message', 10, 2 );
function change_message( $message, $form ) {
    return "<div class='validation_error'>Failed Validation - " . $form['title'] . '</div>';
}

So, I have a variable (which will change from time to time) that I want to use for my function name...

$newitem = 'new_item';

...and tried this...

add_filter( 'gform_validation_message', $newitem, 10, 2 );
function $newitem( $message, $form ) {
    return "<div class='validation_error'>Failed Validation - " . $form['title'] . '</div>';
}

...but obviously that does not work. But I think you get the idea of what I am trying to achieve.

Is anything like this even possible?

Any help is appreciated.

User_FTW
  • 504
  • 1
  • 16
  • 44
  • *"that requires I create my function names dynamically"* – [y tho](https://i.kym-cdn.com/entries/icons/original/000/022/978/yNlQWRM.jpg)…?! – deceze May 28 '19 at 12:37

2 Answers2

0
$thing = 'some_function';
$$thing = function() {
   echo 'hi';
};
$some_function();

Source - Use a variable to define a PHP function

Dorian Mazur
  • 514
  • 2
  • 12
0

You can simply pass Anonymous function to add_filter function as parameter:

$newitem = function($message, $form) {
    return "<div class='validation_error'>Failed Validation - " . $form['title'] . '</div>';
};

add_filter('gform_validation_message', $newitem, 10, 2);

Arthur Shveida
  • 447
  • 2
  • 8