Remove or edit anonymous function in WP hooks

209 Views Asked by At

I've added an anonymous function to an elementor hook in order to process form data:

add_action('elementor_pro/forms/new_record', function ($record, $ajaxHandler) { the code... }), 10,2);

It works as expected, but can't seem to edit or remove this function now that it's registered!?

Maybe I should note that the code is added as a XYZ PHP-snippet that is called via shortcode. I don't know if that makes any difference?

I've tried the following:

remove_action('elementor_pro/forms/new_record','elementor_pro/forms/new_record',10);
remove_action('elementor_pro/forms/new_record',function ($record, $ajaxHandler{},10);
remove_all_actions('elementor_pro/forms/new_record');

(the remove_action functions were both added with an add_action handler).

I also restarted PHP, but the initial function seems to persist regardless. Any ideas. Where is this function even registered, can I remove it from the database somehow?

I'm running PHP Version 7.4 and WP 6.1.1.

1

There are 1 best solutions below

0
On

This is embarrassing: ( I added the same code from the snippet to a plugin just before I got caught up in something else and forgot all about it. That's why it seemed to persist after deletion. Also, I didn't really understand how this feature works in the first place. I haven't looked deep into this but AFAI understands you should just use WP_HOOK::remove_all_actions or WP_HOOK::remove_all_filters in order to delete anonymous hooked functions, should you decide to use them. The caveat to this approach is that this will remove all functions for a given hook, which is why you may consider using named functions instead.

My thanks to hakre for his replies..