What is the difference between "double underscore" and "underscore x"?

In PHP, especially Wordpress, what's the difference between __('string')

and _x('string')


I am reading the Wordpress documentation and am confused. The following good example of confusion is illustrated with the Wordpress doc code example for register_post_type()


$labels = array(
    'name'               => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ),
    'singular_name'      => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ),
    'menu_name'          => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ),
    'name_admin_bar'     => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ),
    'add_new'            => _x( 'Add New', 'book', 'your-plugin-textdomain' ),
    'add_new_item'       => __( 'Add New Book', 'your-plugin-textdomain' ),
    'new_item'           => __( 'New Book', 'your-plugin-textdomain' ),
    'edit_item'          => __( 'Edit Book', 'your-plugin-textdomain' ),
    'view_item'          => __( 'View Book', 'your-plugin-textdomain' ),
    'all_items'          => __( 'All Books', 'your-plugin-textdomain' ),
    'search_items'       => __( 'Search Books', 'your-plugin-textdomain' ),
    'parent_item_colon'  => __( 'Parent Books:', 'your-plugin-textdomain' ),
    'not_found'          => __( 'No books found.', 'your-plugin-textdomain' ),
    'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' )



source to share

2 answers

In both of WordPress _x()

and __()

are transfer functions. _x()

allows you to specify the context for translation:

<?php _x( $text, $context, $domain ); ?>


While __()


<?php __( $text, $domain ); ?>


The context option is useful for a variety of situations where literal translation may not produce the desired result.

Since the string "Read" itself can have one of several meanings in English, the context is provided so that translators know they must provide a short deadline, which stands for "Books I Read".

Some other examples might be: "date", "type", "right", "leaves", etc.

Source: WordPress Codex




performs a direct translation from string A to string B


allows you to provide context. For example, say you have two strings in English that mean the same thing, but may not mean the same thing in another language, then you can use _x

to provide context so that although your strings are the same in English , you can translate them to two different strings in another language.

So, as an example in your question, you

__( 'All Books', 'your-plugin-textdomain' ),

which is using __

because the author should have thought, “All books” will always mean “All books” in every context used by that term. ”However, consider a usecase for _x


_x( 'Books', 'post type general name', 'your-plugin-textdomain' )

So the author was thinking, "Books" is a very general term, depending on the context in which we used the term "Books" in our application, we might translate this term differently "- and therefore additional parameters are used as context. When translating "Books" into another language from the above line, we would know that we are talking about books in the context of our textdomain plugin.

Source: http://wpengineer.com/2237/whats-the-difference-between-__-_e-_x-and-_ex/



All Articles