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.

I was looking at the TwentyEleven code and found this:

esc_attr__( 'Permalink to %s', 'twentyeleven' )

This is come code that came out of the title tag for a post; the full code within the title tag being:

printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) );

I checked out the documentation for the esc_attr function (appears to be the same as the esc_attr__ function?) and it states it only takes one parameter, so how is this passing it two parameters?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

General difference between esc_attr and esc_attr_*

The difference between esc_attr() and esc_attr__(), esc_attr_e(), esc_attr_x() is that the later three are just "wrappers" or in other words higher level API functions.

When you look at the source of the later three, then you'll see that those put in a single argument wrapped in a call to translate() (or translate_with_gettext_context() for esc_attr_x().

That means that you're just throwing two arguments in: The string to translate and the textdomain that helps WP determining which language file to take for the translation.

Everything else you see is the sprintf() around it, which is default php function that replaces %s-parts and %d-parts in a string (string/digit).

Minetrap! Be aware!

If you're not passing a second argument to the later three versions of esc_attr(), then you'll run into the following problem: WP now thinks that your strings are part of core and tries to translate them. Which can lead to very odd results.

Another thing that might happen is that the translation string (the 2nd arg) is already used by a plugin or theme. In this case, the later loaded translation file wins the race and is taken instead of your file. Again: Very odd results. Fix: Prefix your textdomain, as you'd do it with custom globals or function names.

Conclusion

If you want to avoid a second argument, then simply go with esc_attr( 'your string' ) and avoid the other three versions.

share|improve this answer
    
Sorry, what is a textdomain? I'm new to theming WP and have noticed several instances of WP passing in the 'twentyeleven' argument. Does this have to do with the theme name or something else? –  Brett Sep 26 '12 at 11:53
1  
The text-domain is the domain you're using for internationalization. More info: codex.wordpress.org/Translating_WordPress and codex.wordpress.org/I18n_for_WordPress_Developers –  Otto Sep 26 '12 at 11:54
1  
@Otto As you might have noticed: There's an "edit"-button. And afaik you get +2 rep points if the question you edited gets upvoted. Not everybody reads comments. –  kaiser Sep 26 '12 at 11:55
1  
@kaiser: I don't edit other people's answers, and I usually get more than a little annoyed when other people edit mine. It's rude. And if somebody doesn't read comments, then they can just not get their answers as far as I care. –  Otto Sep 26 '12 at 15:23
    
@Otto I tend to not discuss too much in comments, as the community user bot catches it pretty quickly. Maybe you want to join us in chat? –  kaiser Sep 26 '12 at 15:25

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.