WordPress Custom Interceptors via Plugins

I am trying to create a hook in one Wordpress plugin that can be used by other plugins. First, is this possible? I am also posting some additional arguments, so this may be 2 questions in one, as I am having trouble finding the final information on how to do this.

Here is what I have tried so far:

In the plugin that creates the hook (call it plugin 1), I added:

do_action('plugin1_hook', $customArg1, $customArg2, $customArg3); 

at the point where I want the crane to fire. Then, in another plugin (plugin 2), I added:

 add_action('plugin1_hook', 'my_function'); 

and

 function my_function($customArg1, $customArg2, $customArg3) { //my code } 

However, this does not seem to work. My confirmation for this was Word_post for Wordpress, which is defined by Wordpress as:

 do_action('comment_post', $comment_ID, $commentdata['comment_approved']); 

and I use like:

 add_action('comment_post', 'my_comment'); function my_comment($comment_id) { //my code } 

The above snippet is working correctly.

+7
php wordpress hook
source share
3 answers

I thought I would post this as an answer, as it is a little understandable to explain :)

When you connect a function, but do not specify the number of arguments, WordPress will always pass one argument.

You will not get errors for something like this:

 function do_my_hook($arg1, $arg2 = '', $arg3 = '') {} add_action('my_hook', 'do_my_hook'); 

But you will be something like this;

 function do_my_hook($arg1, $arg2, $arg3) {} add_action('my_hook', 'do_my_hook'); 

WordPress tries to call do_my_hook() , but it returns only one argument. The first example uses the default arguments to the PHP function , so you can call the function without passing all available arguments, but without errors.

The second example will throw a PHP “missing argument” error, since all three arguments are required.

Correction?

 add_action('my_hook', 'do_my_hook', 10, 3); 

The idea of ​​determining the number of arguments that your function performs is to avoid such errors (although technically they are just as easy to avoid using the default arguments!).

+5
source share

I assume that the second plugin loads after the first, so the quest has already been fired by the time the action is added to it. You can try this for the first plugin:

 function my_custom_hook_insertion($arg1, $arg2, $arg3){ do_action('plugin1_hook', $arg1, $arg2, $arg3); } add_action('plugins_loaded', 'my_custom_hook_insertion'); 

This will wait until all plugins are loaded before the hook starts.

0
source share

Changing my add_action for this fixed the problem:

 add_action('plugin1_hook', 'my_function', 10, 3); 

10 represents priority, and 3 represents the number of arguments that the function executes. I'm not quite sure how matching works, since the default is 1, and I use a lot of hooks without specifying 0 arguments, and I used hooks that pass more than 1 argument, but only 1 arg is used in my function signature. Source: WordPress Codex: Function Link / Add Action

It works, although cross-plugins are possible.

0
source share

All Articles