Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Why, when I want to use

add_filter( 'author_link', 'foo', 10, 3 );
function foo ( $link, $author_id, $author_nicename ) {}

I have to use 3 required parametres? Why cant I do this:

add_filter( 'author_link', 'foo' );
function foo ( $author_id ) {}

If you can, please give me some literature about this.

PS beginner in WP.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You don't have to specify number of arguments, in which case it will default to one.

Essentially this argument gives you control on how many arguments hook will pass to function, which matters in few too many situations to come up with generic explanation.

In a nutshell if hook can pass N arguments, your options are:

  • pass 1 argument (default)
  • pass <=N arguments by specifying number
  • do not pass any arguments by specifying 0 (rare, but highly useful at times)

Note that you cannot change order of arguments passed, if you need Nth you will need to pass as many and then ignore unwanted ones in your function (which only makes sense in few hooks, because first argument tends to be meaningful one, rarely second).

share|improve this answer

If you require only the first argument to fulfil your job then it is not necessary to specify the number of arguments. The number of arguments need to be specified if the function you are writing might need the extra information (arguments).

The first argument is actually what you should be returning back in your function. The other arguments act as helpers to get the desired output.

share|improve this answer
    
and if I need only second argument? –  Reshat Belyalov May 31 '12 at 11:25
    
Then you could try this maybe add_filter( 'author_link', 'foo', 10, 2 ); function foo ( $link, $author_id ) {} But you will have to use the first argument also. Have not tested if the above thing would work. It should work though. –  Joshua Abenazer May 31 '12 at 11:30
    
Yes, it works with two args. Thanks. But where can I find info about this requirements? –  Reshat Belyalov May 31 '12 at 11:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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