By Martin


2018-02-16 19:50:01 8 Comments

I have seen the token %1$ and similar ones more often in the WordPress code lately, but I can't figure out what it means. Here an example:

sprintf( __( '%1$s is deprecated. Use %2$s instead.' ),

Does anyone know what it means?

2 comments

@Howdy_McGee 2018-02-16 20:09:00

Read the PHP docs on sprintf().

  • %s is just a placeholder for a string
  • %d is just a placeholder for a number

So an example of sprintf would look like this:

$variable = sprintf(
    'The %s ran down the %s',   // String with placeholders
    'dog',      // Placed in the first %s placeholder
    'street'    // Placed in the second %s placeholder
);

Which will return a string to our variable $variable:

The dog ran down the street

By numbering the placeholders it's both a developer-friendly way to quickly tell which following string will be placed where. It also allows us to reuse a string. Let's take another example with numbered placeholders:

$variable = sprintf(
    'The %1$s ran down the %2$s. The %2$s was made of %3$s',    // String with placeholders
    'dog',      // Will always be used in %1$s placeholder
    'street',   // Will always be used in %2$s placeholder
    'gravel'    // Will always be used in %3$s placeholder
);

Which will return a string to our variable $variable:

The dog ran down the street. The street was made of gravel

Finally, the __() function let's us translate strings passed to it. By passing __() placeholders, and then passing that whole string to sprintf(), we can translate whatever is passed to the translating function allowing us to make our string and application a bit more dynamic.

@Martin 2018-02-17 05:12:06

Many thanks guys! Great help!

@Pat J 2018-02-16 20:05:45

It's not a WordPress thing, it's a PHP thing. %1$s, %2$s, etc., are placeholders for variables in a formatted string returned by sprintf() (or printed by printf()).

The 1$ indicates it's the first variable, 2$ would be the second, and so forth. The s indicates it's a string variable. Other options exist (eg, d would indicate an integer).

The example you give is incomplete: it's certainly something like this in its entirety:

sprintf( __( '%1$s is deprecated. Use %2$s instead.' ),
$string_1,
$string_2 );

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] WordpPress 4.8.1 php getting commented

  • 2017-09-14 17:44:02
  • An intern has no name
  • 97 View
  • 0 Score
  • 3 Answer
  • Tags:   plugins php

2 Answered Questions

[SOLVED] How to make search for the custom post type?

  • 2012-01-18 08:50:06
  • osos
  • 717 View
  • 1 Score
  • 2 Answer
  • Tags:   posts categories

1 Answered Questions

[SOLVED] Posts with Custom Fields does not appear

  • 2015-07-01 13:09:08
  • Olen
  • 65 View
  • 0 Score
  • 1 Answer
  • Tags:   php

1 Answered Questions

[SOLVED] What does this mean in wordpress? Easy question

1 Answered Questions

[SOLVED] Unexpected token ILLEGAL, even on clean install - jquery-issue?

  • 2014-05-05 16:49:38
  • zero
  • 2578 View
  • 0 Score
  • 1 Answer
  • Tags:   php jquery linux

1 Answered Questions

0 Answered Questions

How does add_action 'comment_post' work?

1 Answered Questions

1 Answered Questions

[SOLVED] What does $2$3 mean?

  • 2012-09-29 00:07:54
  • Dave
  • 353 View
  • 0 Score
  • 1 Answer
  • Tags:   php

1 Answered Questions

Sponsored Content