What on Earth does a double underscore then parenthesis mean in PHP? __()
I was taking my first steps at really digging-below-the-surface of the code in WordPress, (trying to re-order the way my Page List was being shown on the sidebar), when I spotted a really strange piece of code in one of the examples:
(Copied April 2007 - Note: WP syntax may have changed by the time you read this!)
If you wanted to sort the list by Page Order and display the word “Prose” as the list heading (in h2 style) on a Sidebar, you could add the following code to the sidebar.php file:<ul>
<?php
wp_list_pages(’sort_column=menu_order&title_li=<h2>’ . __(’Prose’) . ‘</h2>’ ); ?>
</ul>
Q: What the Gibbering SpiceWeasels is __(’string’) all about? Eh!
In all my 7 years of PHP coding, I had never seen such a syntax before - two underscores and then a string within parenthesis. How strange.
After a search on Google for “php __()” I arrived at a WebMasterWorld forum post where some comrade called AthlonInside had asked a similar question.
A kind soul had offered an answer (as best they could). However, according to my ’state of mind’, their answer was way off.
I was thinking its a special thing in PHP. Surely this sequence must be doing something fancy like the Heredoc syntax. The Heredoc syntax in PHP contains a sequence of three “less than signs” (”<<<”). If you’ve ever tried to search for info on it you’ll know how hard it can be!
I proceeded to do a quick scan of the PHP documentation that explains about Magic Methods. This proved to be a “wild goose chase”.
I, then, tried a different approach and searched Google again. This time I asked for “wordpress __(” and the top page in the results gave me my answer….
A: __(’string’) is just a call to a very weirdly named function in WordPress
As the info regarding Localization explains:
__($message) Searches the localization module for the translation of $message, and passes the translation to the PHP return statement. If no translation is found for $message, it just returns $message
So there we have it. A double underscore then parenthesis means nothing special in PHP. Its just another user defined function in WordPress. And quite a nifty way of dealing with multiple languages to boot!
UPDATE
I’ve just discovered that this seems to be quite a common practice for writing code for translation.
The ZEND framework translate functionality uses a similar single underscore then the string in parenthesis.
July 22nd, 2007 at 9:22 pm
[…] Further to my previous post on the double underscore __() … […]
March 14th, 2009 at 2:24 pm
Thank you! As someone who writes plugins and hacks the code for wordpress (from time to time as needed), it can be really hard to find someone who knows the answers to tough Wordpress questions (there are less advanced coder types than not after all).
Your post saved me a bunch of searching, thanks!
June 17th, 2009 at 9:50 pm
The CakePHP framework uses it too. Initially, I’d thought it was a sort of shortcut for echo or print, but interestingly enough, it’s a lot more. Thanks for the explanation.
September 23rd, 2009 at 1:01 am
Thanks mate! I was trying to figure out what the @@&%* was those underscores.
I think it should be better explained on codex.
[]’s!
November 3rd, 2009 at 4:54 am
thanks - i couldn’t figure out what the double underscore was doing
November 7th, 2009 at 6:52 pm
I was weired, when I saw this in wordpress, too.
But searching in google only for “php __”, the first results gave me the answer.
But just your blog gave me the answer that is is user-defined^^
Thx!
November 22nd, 2009 at 7:08 am
Thanks! I’ve always wondered what’s with the double underscore.
January 7th, 2010 at 2:26 am
Wow, thank so much for this post. You’ve saved me a lot of searching.
February 7th, 2010 at 12:07 pm
Thanks too, I was contracted to write my first wordpress widget and those __(something) was really bugging me because I kept wondering if there was some obscure PHP language construct I’ve been missing for years.
April 19th, 2010 at 5:42 am
not sure if this is relevant after all this time but FYI ..
from the website :
http://webdevelopmenttutorials.com/phpvariablestutorial.php
Magic Constants
Magic constants, as they are called, are not acutally constants but effectively behave like constants and are pre-defined. Magic constants use the same naming convention as PHP constants.
The full list of magic constants can be found here.
There are 7 magic constants that change depending on where they are used. They are listed below.
name description
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)
__FUNCTION__ The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__ The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__METHOD__ The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
__NAMESPACE__ The name of the current namespace (case-sensitive). This constant is defined in compile-time (Added in PHP 5.3.0).
April 23rd, 2010 at 4:17 pm
If you’re looking for syntax information about the use of the __() function in WordPress, you can find it here:
http://codex.wordpress.org/Function_Reference/_2
August 25th, 2010 at 12:03 pm
Doh! I actually read this in the Wordpress bible, and then forgot! Thanks to your post I re-discovered the answer. Nice bit of digging :-)
September 6th, 2010 at 4:05 pm
I was able to get lucky with googling for: wordpress double underscore and your site came up… I have always wondered that myself.
When making your own plugins you may also use the built in function called load_plugin_textdomain($path_to_lang_files);
The language files can be .mo / .po / .pot files.