I'm trying to figure out if the same function is hooked onto multiple actions, can I figure out which action calls it?
I'd like to send out an API call when a user is created and deleted; the functionality in both cases is the same except one data point would be different based on if its created or deleted. It doesn't feel right making two identical functions with only one difference, but I'm not sure how else I can do it.
Advice?
Go to Plugins > Add New. Find the \”WP Hooks Finder\” Plugin you wish to install. Click Install Now to install the WordPress Plugin. The resulting installation screen will list the installation as successful or note any problems during the install.
To use either, you need to write a custom function known as a Callback , and then register it with a WordPress hook for a specific action or filter. Actions allow you to add data or change how WordPress operates. Actions will run at a specific point in the execution of WordPress Core, plugins, and themes.
Actions and hooks are not stored, this is the whole beauty of them. You register a new action with add_action() , adding a function to the list of functions that will be executed with the declared action. Then, the action is executed with do_action() , anywhere you want, even in multiple places.
WordPress offers filter hooks to allow plugins to modify various types of internal data at runtime. A plugin can modify data by binding a callback to a filter hook. When the filter is later applied, each bound callback is run in order of priority, and given the opportunity to modify a value by returning a new value.
That's the function current_filter()
:
add_action( 'plugins_loaded', 'common_action' );
add_action( 'admin_init', 'common_action' );
function common_action()
{
switch( current_filter() )
{
case 'plugins_loaded':
// do_something( 'Plugins loaded' );
break;
case 'admin_init':
// do_another_thing( 'Admin init' );
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With