plugins – What exactly happens to function argument availability when using a filter?
The goal of this question is:
- to be able to interpret WP docs better
- to understand what’s exactly happening in this scenario
- to verify what I’m thinking is correct
I came across this code.
function register() {
...
add_filter( "plugin_action_links_$this->plugin", array( $this, 'settings_link' ) );
}
public function settings_link($links) {
$settings_link = '<a href="admin.php?page=alecaddd_plugin">Settings</a>';
array_push( $links, $settings_link );
return $links;
}
When I debug settings_link($links)
, I see $links is an array with a ‘deactivate’ key and value.
Looking at wp dev docs, it seems $links
relates to the $actions parameter.apply_filters( "plugin_action_links_{$plugin_file}", string[] $actions, string $plugin_file, array $plugin_data, string $context )
I noticed though, that the other parameters, $plugin_file, $plugin_data, $context
, were not available in the debugger.
I then changed the code as follows.
function register() {
...
add_filter( "plugin_action_links_$this->plugin", array( $this, 'settings_link' ), 10, 4 ); ); // change 1
}
public function settings_link($links, $plugin_file, $plugin_data, $context ) { // change 2
$settings_link = '<a href="admin.php?page=alecaddd_plugin">Settings</a>';
array_push( $links, $settings_link );
return $links;
}
Low and behold, now the other parameters $plugin_file, $plugin_data, $context
are now available in my function.
I suspect calling add_filter
while only passing a callback, is some form of workflow to make things faster & easier.
Can somebody clarify further, or just confirming, what’s exactly happening here?
Is this behavior documented somewhere in the official docs?
Leave an answer