• Skip to main content
  • What We Do
  • Our Work
  • Contact

Mackey

October 7, 2014 by Mackey

Get current language code or name using WPML Plugin for WordPress

We needed to output the language code for a form and needed to use a shortcode.  We are using WPML (WordPress Multi Language) plugin and thus have access to Language constants, ie ICL_LANGUAGE_CODE

We needed to not only get the current language of the user, which was easy enough using a template tag, but needed it in shortcode format so that we could use it in widgets, gravity forms, etc.

Basically you set up a shortcode as you would normally in functions.php using ICL_LANGUAGE_CODE.

Here is the final snippet:

// Add Shortcode for Language
function user_lang() {

 // Code
 return ICL_LANGUAGE_CODE;
}
add_shortcode( 'userlang', 'user_lang' );

 

Now use the shortcode within your posts, etc. You can see more about ICL_LANGUAGE_CODE at https://wpml.org/documentation/support/wpml-coding-api/ or check this thread out too if you are looking for language conditionals: https://wpml.org/forums/topic/conditional-language-shortcode/

Filed Under: Code Snippets

Mackey Creative Lab
Web Design and Development, Branding, Graphic Design and Marketing Strategy
680 East Colorado Blvd., Suite 180 & Second Floor, Pasadena, CA 91101
info@mackeycreativelab.com // 626 214 5093
© 2014-2021 Mackey, LLC, All Rights Reserved

Want to join the Lab? Become a lab partner

Need Wordpress Performance & Security Management? Learn More