7

I get the error

Warning: printf() [function.printf]: Too few arguments 

Looking at the code I see:

function twentyten_posted_on() {
    printf( 
        sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
            get_permalink(),
            esc_attr( get_the_time() ),
            get_the_date()
        )
    );
}

What seems to be the problem?

Himmators
  • 14,278
  • 36
  • 132
  • 223

4 Answers4

10

The printf() and sprintf() functions are identical; the only difference in their behaviour is that one is a statement (it does something) and the other is a function expression (it evaluates to something). (See this StackOverflow answer for a description of the difference.) They both take a format as the first argument, then zero or more additional arguments as replacement strings for special characters within the format string.

Your sprintf() function is well formed. You've numbered your format strings, you have replacement strings as arguments to match the format strings. All is good.

But consider what the printf() function is doing. It gets a string, which happens to be the output of sprintf(). If sprintf()'s contains a % character, then printf() would need a replacement string which is not included in your code.

As others have said, you can probably leave out the sprintf() from your code. But you should also understand WHY this is happening.

For example:

$fmt = "%%d\n";
printf( $fmt );
printf( sprintf($fmt) );
printf( sprintf($fmt), "Hello world" );

The first printf works, and prints a "%d". The second printf fails because its format string implies that it should have a replacement string, but none is provided. The third one prints a zero, because when you try to evaluate "Hello world" as a decimal integer (%d), that's what you get.

Look at your variables, and you'll probably find that at least one of them has a % character in it.

Community
  • 1
  • 1
ghoti
  • 45,319
  • 8
  • 65
  • 104
0

You have to double the % character if you want to use them literally, or refrain from using the printf of course.

Dirk de Man
  • 657
  • 3
  • 9
  • 1
    The replacement arguments are on the sprintf function. If he doubles the % characters, he'll have no formatting characters for sprintf to replace. – ghoti Jun 18 '12 at 11:50
  • You're right... The answer you gave is infinitely better than mine anyway, thank you! – Dirk de Man Jun 18 '12 at 11:55
  • I see nothing wrong with formatter, but this inputs may introduce a %. This is why a formater should be used as a formatter and nothing else. NEVER use input data in formater. – ctrl-alt-delor Jun 18 '12 at 11:58
0

Do not use input-ed data in a formatter.

Do

printf( "%1$s", calculated input value);

Also from a security point of view, as well as a bug point of view. The format is part of your code (not just data). If you allow it to be derived from input, an arbitrary user of your code can influence its execution.

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
-2
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
            get_permalink(),
            esc_attr( get_the_time() )

Here you call sprintf with two arguments, while you have three placeholders, %1$s, %2$s and %3$s.

If you want to print you can leave sprintf out of it:

printf( 
    '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
        get_permalink(),
        esc_attr( get_the_time(),
        get_the_date()
);
Sjoerd
  • 74,049
  • 16
  • 131
  • 175