0

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.

Moishy
  • 3,560
  • 3
  • 23
  • 42
  • I'm not sure this works with WP, however so that it could work at all you need to assign the anonymous function to a variable first and then use that variable with add_action() _and_ remove_action(). Have you considered that already or is that this is what you wanted to prevent in the first place? – hakre Dec 13 '22 at 09:54
  • And please consider to use the formatting tools (you can always [edit]), the question would be more easy to read if you'd have code within it formatted as `code`. – hakre Dec 13 '22 at 09:55
  • Further reference: [Remove Actions/Filters added via Anonymous Functions](https://wordpress.stackexchange.com/q/137688/178) – hakre Dec 13 '22 at 09:56
  • Hakre, this functionality is new to me so I wasn't aware of this little pitfall. You're right in that I should avoid using anonymous functions in the first place but now it's there I'm struggling to get rid of it. I already read the discussion you refer to but it doesn't help me AFAI can see. Was hoping others had experience with this as it must be a pretty common problem!? – Johnny Kristensen Dec 14 '22 at 06:26
  • Just an idea that came to mind: Is using named functions instead of anonymous ones not an option? – hakre Dec 14 '22 at 11:33
  • And mind this is all Q&A and also dated. From what I know or remember I'm under the impression anonymous functions do work incl. removal if you keep a variable to the function so that you can refer to it later. Named functions are easier as you know the name to refer to it. AFAIK this is also the documented way in the WP docs. https://developer.wordpress.org/reference/functions/add_action/#usage https://developer.wordpress.org/reference/functions/remove_action/#parameters they miss examples, but here: https://developer.wordpress.org/reference/functions/remove_filter/#user-contributed-notes – hakre Dec 14 '22 at 11:38

1 Answers1

0

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..

Md. Nashir Uddin
  • 730
  • 7
  • 20