wp_dropdown_languages

Language selector.

Description

(string) wp_dropdown_languages( (array) $args = array() ); 

Returns (string)

HTML content

Parameters (1)

0. $args — Optional. (array) => array()
Array or string of arguments for outputting the language selector.

Options

  • id (string) => ''

    ID attribute of the select element.

  • name (string) => ''

    Name attribute of the select element.

  • languages (array) => array()

    List of installed languages, contain only the locales.

  • translations (array) => result of wp_get_available_translations()

    List of available translations.

  • selected (string) => ''

    Language which should be selected.

  • echo (bool|int) => 1

    Whether to echo the generated markup. Accepts 0, 1, or their boolean equivalents.

  • show_available_translations (bool) => true

    Whether to show available translations.

array(

    /**
     * ID attribute of the select element.
     *
     * @type string
     * @default ''
     */
    'id' => '',

    /**
     * Name attribute of the select element.
     *
     * @type string
     * @default ''
     */
    'name' => '',

    /**
     * List of installed languages, contain only the locales.
     *
     * @type array
     * @default array()
     */
    'languages' => array(),

    /**
     * List of available translations.
     *
     * @type array
     * @default result of wp_get_available_translations()
     */
    'translations' => result of wp_get_available_translations(),

    /**
     * Language which should be selected.
     *
     * @type string
     * @default ''
     */
    'selected' => '',

    /**
     * Whether to echo the generated markup. Accepts 0, 1, or their boolean equivalents.
     *
     * @type bool|int
     * @default 1
     */
    'echo' => 1,

    /**
     * Whether to show available translations.
     *
     * @type bool
     * @default true
     */
    'show_available_translations' => true
);        


Usage

  1. if ( !function_exists( 'wp_dropdown_languages' ) ) { 
  2. require_once ABSPATH . WPINC . '/l10n.php'; 
  3.  
  4. // Optional. Array or string of arguments for outputting the language selector. 
  5. $args = array( 
  6. 'id' => '', 
  7. 'name' => '', 
  8. 'languages' => array(), 
  9. 'translations' => result of wp_get_available_translations(), 
  10. 'selected' => '', 
  11. 'echo' => 1, 
  12. 'show_available_translations' => true 
  13. ); 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = wp_dropdown_languages($args); 
  17.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/l10n.php  
  1. function wp_dropdown_languages( $args = array() ) { 
  2.  
  3. $args = wp_parse_args( $args, array( 
  4. 'id' => '',  
  5. 'name' => '',  
  6. 'languages' => array(),  
  7. translations => array(),  
  8. 'selected' => '',  
  9. 'echo' => 1,  
  10. 'show_available_translations' => true,  
  11. 'show_option_site_default' => false,  
  12. ) ); 
  13.  
  14. // English (United States) uses an empty string for the value attribute. 
  15. if ( 'en_US' === $args['selected'] ) { 
  16. $args['selected'] = ''; 
  17.  
  18. $translations = $args[translations]; 
  19. if ( empty( $translations ) ) { 
  20. require_once( ABSPATH . 'wp-admin/includes/translation-install.php' ); 
  21. $translations = wp_get_available_translations(); 
  22.  
  23. /** 
  24. * $args['languages'] should only contain the locales. Find the locale in 
  25. * $translations to get the native name. Fall back to locale. 
  26. */ 
  27. $languages = array(); 
  28. foreach ( $args['languages'] as $locale ) { 
  29. if ( isset( $translations[ $locale ] ) ) { 
  30. $translation = $translations[ $locale ]; 
  31. $languages[] = array( 
  32. 'language' => $translation['language'],  
  33. 'native_name' => $translation['native_name'],  
  34. 'lang' => current( $translation['iso'] ),  
  35. ); 
  36.  
  37. // Remove installed language from available translations. 
  38. unset( $translations[ $locale ] ); 
  39. } else { 
  40. $languages[] = array( 
  41. 'language' => $locale,  
  42. 'native_name' => $locale,  
  43. 'lang' => '',  
  44. ); 
  45.  
  46. $translations_available = ( ! empty( $translations ) && $args['show_available_translations'] ); 
  47.  
  48. $output = sprintf( '<select name="%s" id="%s">', esc_attr( $args['name'] ), esc_attr( $args['id'] ) ); 
  49.  
  50. // Holds the HTML markup. 
  51. $structure = array(); 
  52.  
  53. // List installed languages. 
  54. if ( $translations_available ) { 
  55. $structure[] = '<optgroup label="' . esc_attr_x( 'Installed', translations ) . '">'; 
  56.  
  57. if ( $args['show_option_site_default'] ) { 
  58. $structure[] = sprintf( 
  59. '<option value="site-default" data-installed="1"%s>%s</option>',  
  60. selected( 'site-default', $args['selected'], false ),  
  61. _x( 'Site Default', 'default site language' ) 
  62. ); 
  63.  
  64. $structure[] = sprintf( 
  65. '<option value="" lang="en" data-installed="1"%s>English (United States)</option>',  
  66. selected( '', $args['selected'], false ) 
  67. ); 
  68.  
  69. foreach ( $languages as $language ) { 
  70. $structure[] = sprintf( 
  71. '<option value="%s" lang="%s"%s data-installed="1">%s</option>',  
  72. esc_attr( $language['language'] ),  
  73. esc_attr( $language['lang'] ),  
  74. selected( $language['language'], $args['selected'], false ),  
  75. esc_html( $language['native_name'] ) 
  76. ); 
  77. if ( $translations_available ) { 
  78. $structure[] = '</optgroup>'; 
  79.  
  80. // List available translations. 
  81. if ( $translations_available ) { 
  82. $structure[] = '<optgroup label="' . esc_attr_x( 'Available', translations ) . '">'; 
  83. foreach ( $translations as $translation ) { 
  84. $structure[] = sprintf( 
  85. '<option value="%s" lang="%s"%s>%s</option>',  
  86. esc_attr( $translation['language'] ),  
  87. esc_attr( current( $translation['iso'] ) ),  
  88. selected( $translation['language'], $args['selected'], false ),  
  89. esc_html( $translation['native_name'] ) 
  90. ); 
  91. $structure[] = '</optgroup>'; 
  92.  
  93. $output .= join( "\n", $structure ); 
  94.  
  95. $output .= '</select>'; 
  96.  
  97. if ( $args['echo'] ) { 
  98. echo $output; 
  99.  
  100. return $output;