2

To hook my plugin content into anywhere, suppose I offered the theme developers to create their own hooks with a prefix (pre_) anywhere in their theme - the rest of the hook is unknown to me. So what will they do?

Suppose, in single.php they placed a hook like:

do_action( 'pre_kauwa' );

in page.php they placed a hook like:

do_action( 'pre_kala' );

And so on...

How can I get all those do_action() declarations with a prefix pre_ from an active WordPress installation (active theme)
or,
from an active WordPress viewport (post, page, archive bla bla)

...so that I can make a list of hooks dynamically where my content can be hooked using add_action()?

Testing testing...

In wp-includes/plugin.php I found the has_filter() function declaration. From there I took the following line for testing:

$wp_filter = $GLOBALS['wp_filter'];
$has = ! empty( $wp_filter[ $tag ] );

In my dummy theme's index.php after the_content() (within the loop) I placed:

do_action( 'pre_kk' );

Just after the do_action() call I placed:

$t_test = $GLOBALS['wp_filter'];
$hk = 'pre_kk';
var_dump( $t_test[$hk] );

But it's throwing an undefined index.

6
  • 1
    Do you do add_action( 'pre_kk', 'your_function' ) to your do_action( 'pre_kk' )? Because otherwise - if I'm not totally mistaken - it won't be added to $wp_filter. Commented Apr 18, 2015 at 12:09
  • @ialocin you are right. +1 for that. But actually that couldn't solve my problem. It's returning the callback function, not the hook. :( Commented Apr 18, 2015 at 13:49
  • Because you're already addressing the specific element in the array. The first level are the hook names/tags. To get all your »pre_*« hooks you have to iterate and/or search through the array, there are plenty of threads how to do this on Stack Overflow. Or at least I don't know of a build-in functionality to do it with. Commented Apr 18, 2015 at 14:00
  • The first level are the hook names/tags. - though I'm not sure till now. I'll let you know the update. Commented Apr 18, 2015 at 16:12
  • The plugin Debug Objects give you all fired hooks in current page, also in front end. Maybe this helps you.
    – bueltge
    Commented Apr 19, 2015 at 20:24

1 Answer 1

0

pre_ is a terrible prefix for this because it's going to interfere with all of the WordPress internal hooks that are prefixed with pre_.

But if you want to get the name of all the actions and filters called on a certain page you can use the all hook which adds your callback to every hook. Obviously this is terribly expensive so you wouldn't want to do it on a production server.

add_action( 'all', 'wpse_all_actions' );
function wpse_all_actions() {
  static $pre = [];
  $filter = current_filter();
  if( 'pre_' === substr( $filter, 0, 4 ) ) {
    $pre[] = $filter;
  }
  if( 'shutdown' === $filter ) {
    var_dump( $pre );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.