1

I want to use a method from a plugin as a hook, not the callback. I want to use a custom function I wrote as the callback, that gets triggered when a particular method from a plugin runs. Essentially something like :

add_action( array( "NAME OF CLASS", NAME OF METHOD" ), "MY CUSTOM FUNCTION" ) )

I can't figure out how to do this, any help or direction would be greatly appreciated!

I've verified the method and class exist in functions.php with the method_exists() function.

EDIT:

I'm using plugins called Groups and Groups_File_Access to handle file access and downloads on my site. The class is "Groups_File_Access"" and the method inside is "groups_file_served". I did not write this plugin. That method gets triggered when someone accesses a file and I want to run a custom function when "groups_file_served" get called by hooking onto it. Was trying to avoid editing the plugin itself, but looks like I'm going to need to.

  • What are you trying to do that requires this? It's a little difficult to follow your question, and some context would make it easier to understand – Tom J Nowell Jan 8 '18 at 21:34
4

It sounds like you are looking for do_action() In your method that you wrote add do_action and it will trigger your new custom action. Then you can use add_action() in the same way you use the build in actions.

https://developer.wordpress.org/reference/functions/do_action/

Example of your method in the class...

public function some_method() {
    $foo = 'puppy';
    $bar = 'bunny';
    do_action( 'my_nifty_action', $foo, $bar );
}

Somewhere else you can then add the call to your action...

add_action( 'my_nifty_action', 'my_custom_function', 10, 2 );

function my_custom_function( $foo, $bar ) {
    // ... your code here
}
3

So you want to run code when another function runs?

No. You can't do that with actions/filters. For it to work, the function would need to fire a hook/filter that you could hook into. If it does not do this then you're going to have to fork the plugin

  • Ok, was trying to avoid editing the plugin, but good to know I'll need to do that. That helps a lot, thanks! – Anna Villani Jan 9 '18 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.