Can I use a method from an existing plugin as an action hook?

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.

2 Answers
2

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.

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
}

Leave a Comment