Skip to content

Instantly share code, notes, and snippets.

@jhtjards
Last active November 1, 2023 10:21
Show Gist options
  • Save jhtjards/c06eda3db0216b777ce20e3363716f26 to your computer and use it in GitHub Desktop.
Save jhtjards/c06eda3db0216b777ce20e3363716f26 to your computer and use it in GitHub Desktop.

Revisions

  1. jhtjards revised this gist Nov 1, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions wpml_get_current_language_name.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    if ( ! function_exists( 'wpml_get_current_language_name' ) ) :
    /**
    * Returns current active site language array.
  2. jhtjards revised this gist Nov 1, 2023. No changes.
  3. jhtjards revised this gist Nov 1, 2023. No changes.
  4. jhtjards created this gist Nov 1, 2023.
    31 changes: 31 additions & 0 deletions wpml_get_current_language_name.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    if ( ! function_exists( 'wpml_get_current_language_name' ) ) :
    /**
    * Returns current active site language array.
    * If the optional $arg is set, only the corresponding value is returned.
    *
    * $arg can be one of:
    * code, id, native_name, major, active, default_locale,
    * encode_url, tag, translated_name, url, country_flag_url, language_code
    */
    function wpml_get_current_language( $arg = NULL ){
    $wpml_active_languages = apply_filters( 'wpml_active_languages', NULL, NULL );
    if ( !is_array( $wpml_active_languages ) ) {
    return null;
    }

    foreach ($wpml_active_languages as $key => $language) {
    if ( isset($language['active']) && $language['active'] == 1 ) {
    //return language array
    if(!$arg){
    return $language;
    }
    //return only selected language value
    //one of ["code"], ["id"], ["native_name"], ["major"], ["active"], ["default_locale"], ["encode_url"], ["tag"], ["translated_name"], ["url"], ["country_flag_url"], ["language_code"]
    if( $arg && isset($language[$arg]) ){
    return $language[$arg];
    }
    }
    }
    return null; //No active language
    }
    endif;