Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: figuring out which hook called a function

Tags:

wordpress

hook

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?

like image 311
Rohit Avatar asked Oct 10 '13 19:10

Rohit


People also ask

How do I find my WordPress hook?

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.

How do I use the hook function in WordPress?

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.

Where are WordPress hooks stored?

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.

How do I filter a hook in WordPress?

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.


1 Answers

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;
    }
}
like image 165
brasofilo Avatar answered Nov 15 '22 09:11

brasofilo