13

From a Q&A Thread I've came to meet with some old things, but I'm afraid, I found myself in a condition that, I still don't know the meaning of them and don't know why to use them.

  • %1$s - (found to load widget id)
  • %2$s - (found to load widget class/classes)
  • %s - (found here)

And how the following things work? I mean what's the explanation of the code-format?

  • !Website::getThemeOption("format_post/{$post_format}/content/hide")): - (found here)
  • if( !wp_verify_nonce( $_POST['my_noncename'] ) plugin_basename( __FILE__ ) ) return; - (found here)

This question can go on and on..., I know. But things like these are not clearly explained anywhere. I, not just want their meaning and purposes, but want some inner/core explanation so that I can understand their motives and how they works.

And also, if such things are already clearly defined in Codex, I'd love to read them. And would love to have a list of such curious things if such a list is already sorted out.

closed as off-topic by Mayeenul Islam, s_ha_dum, fuxia Sep 27 '13 at 15:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions should be specific to WordPress within defined scope (merely happening in its context, such as generic PHP/JS/HTML/CSS, is insufficient). Might be better asked at Stack Overflow or other appropriate site of Stack Exchange network." – Mayeenul Islam, s_ha_dum, fuxia
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    This is all plain PHP. See sprintf(). – fuxia Sep 27 '13 at 14:23
  • The Codex exists to provide WordPress-specific documentation. It is not intended to teach PHP programming, and all of your questions are about PHP programming. PHP documentation can be found at php.net, and lots of PHP beginner tutorials can by found using Google. – Ben Miller Sep 27 '13 at 14:50
  • 2
    @BenMiller: Be patient please. Before toscho's comment, I's even unaware about the question's position. It's almost clear now with Dave's answer. – Mayeenul Islam Sep 27 '13 at 14:56
  • @MayeenulIslam, I was simply attempting to explain why you don't find such explanations in the Codex. I'm sorry if I came across as impatient. – Ben Miller Sep 27 '13 at 15:08
11

This is really more of a general PHP programming question and might get closed for that reason. But before that happens, let's see if I can't clear some of this up for you.

Those strings that start with % signs are in "printf format". For example, %2$s translates to "replace this token with the second parameter, and treat it like a string".

When you see a variable in curly braces inside a string, like "Today is {$day}", that's the same as putting the variable there without curly braces, like "Today is $day". But formatting the variable like this makes it stand out, so it's more clear there's a variable there. It also prevents other characters next to the variable name from getting interpreted as part of the name. Without curly braces, would PHP know what to do with "Your robot name would be {$firstName}Number5"? It would see "$firstNameNumber5" and look for a variable named $firstNameNumber5 instead of simply $firstName.

I'm not sure what you're asking about in the last example. Is it the __FILE__ you're confused by? That's a PHP Magic Constant that gets replaced with the full path to the file that bit of code is in.

  • Before getting closed just the last simple one: !Website::. Why the double colon there? – Mayeenul Islam Sep 27 '13 at 14:44
  • 2
    The :: has to do with object-oriented programming -- in the case of Website::getThemeOption, you're calling the function getThemeOption belonging to the class Website. – Pat J Sep 27 '13 at 14:55
  • 2
    Also: {$var} helps PHP distinguish between something like {$complete_variable_name} and {$prefix}_rest_of_name, for example. – Pat J Sep 27 '13 at 14:57
  • FIY double colon is used to access only static or overridden methods and properties of a class, with regular methods you use -> e.g. $myObject->someMethod(); – ed-ta Jun 11 '15 at 16:56

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