2

I'm hoping for a simple way to tell/find/echo what the available arguments are available for use at a given hook.

For example —

function example_function($something){

    //do stuff with $something
    return $something;

}
add_action('some_hook','example_function');

In the above case, is there a quick way to tell what $something is (if there is one at all), or if there are multiple $somethings / arguments I have available to use at that point?

1 Answer 1

4

In general the best solution would be to refer to the documentation for the hook - or the source for the action call within WordPress core's codebase if the documentation proves insufficient.

You can search the documentation for the relevant action or filter here. Many pages actually include the source of the filter/action call which can prove invaluable in determining what variables are passed as arguments.

While some reference pages include elaboration on the mechanism and usage of the hook (such as that of pre_get_posts), many do not. Depending on the hook in question you may be able to find more information and usage examples in the Plugin Developer Handbook or the Theme Developer Handbook, among others.

When that fails, performing a full-text file search of a local WordPress installation or utilizing GitHub's search within the WordPress repository for the respective apply_filters()/apply_filters_ref_array()/do_action()/do_action_ref_array() call to see where it's executed can yield answers.

Examples: Source & Documentation

For example, the apply_filters() call for the map_meta_cap filter's source both has inline documentation describing the possible arguments, and we can scroll further up to read through the function if we need further context:

    /**
     * Filters the primitive capabilities required of the given user to satisfy the
     * capability being checked.
     *
     * @since 2.8.0
     *
     * @param string[] $caps    Primitive capabilities required of the user.
     * @param string   $cap     Capability being checked.
     * @param int      $user_id The user ID.
     * @param array    $args    Adds context to the capability check, typically
     *                          starting with an object ID.
     */
    return apply_filters( 'map_meta_cap', $caps, $cap, $user_id, $args );

These inline comments are also parsed directly into the reference documentation.

Printing Passed Arguments

When all else fails, you can utilize PHP's func_get_args() function to see exactly which arguments were passed:

add_action( 'pre_get_posts', 'wpse40532' );

function wpse40532() {
  $args = func_get_args();

  for( $i = 0; $i < count( $args ); $i++ ) {
    echo "\nArgument $i is:\n;
    var_dump( $args[$i] );
  }
}

Since var_dump() utilizes newlines instead of HTML for output formatting, view the output in the webpage's source (Ctrl+U for most browsers) for a more human-readable display.

However, note that when an action or filter hook is registered via add_action() or add_filter(), one of the arguments for that registration is the number of arguments which the registered callback accepts - or defaults to 1 if no count is specified. If the hook registration specified a number of arguments less than what the action or filter can actually provide, then the mechanism above will not print all possible arguments. If you are in control of the hook registration, you can work around this restriction by registering the hook to accept an absurdly high argument count:

add_action( 'pre_get_posts', 'wpse40532', 10, 100 );

This should only be used as a development aide - be sure to set the argument count to that which you intend to use in production.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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