0

Let's say I have the following hook function:

function my_function() {
  $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
  if( !isset( $_REQUEST['mode'] ) )
    $_REQUEST['mode'] = ( $post_type == 'my_post_type' ) ? 'excerpt' : 'list';
}

add_action( 'load-edit.php', 'my_function', 10, 1 );

The fourth argument is (from WP Codex):

(int) (optional) The number of arguments the hooked function accepts. In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. For example, the action comment_id_not_found will pass any functions that hook onto it the ID of the requested comment.

Default: 1

The default value is 1, however I do not pass any arguments to the hook function.

Should I add an action like that?

add_action( 'load-edit.php', 'my_function', 10, 0 );

1

The accepted arguments value is used by WordPress as $length argument for array_slice when performing the action.

It means that, yes, if you pass 0 no argument will be passed to your callback.

However, you should care of that only if your function acts differently if an argument is passed or not.

A function like the one in OP, is defined without any argument, nor any function like func_get_args() or func_get_arg() is used in it, so it means that you can pass any number of arguments to that function, but it will not change its behaviour.

For that reason you can just ignore the 4th argument of add_action like so:

add_action( 'load-edit.php', 'my_function', 10 );

also consider that 10 is default priority, so previous line has same effect of

add_action( 'load-edit.php', 'my_function' );

It's not worse nor better from a code quality point of view, avoid to pass irrelevant arguments just saves you to type some chars.

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.