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.

1
  • "that requires I create my function names dynamically"y tho…?! Commented May 28, 2019 at 12:37

2 Answers 2

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

Source - Use a variable to define a PHP function

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.