Skip Navigation
Updated
April 27, 2016

All hooks listed in this API are available for WPML versions >=3.2

Retrieving Language Information for Content

wpml_post_language_details – Get the language of a post

availability:
WPML Version: 3.2
description:

Retrieve language information of a post by its ID.

The language information that is returned includes:

  • the post language 2-letter code,
  • the post locale,
  • the language text direction (True for RTL, False for LTR),
  • the post language translated name and native name and
  • whether the current language is different to the post language (True/False)
type:
filter
category:
Retrieving Language Information for Content
parameters:
apply_filters( 'wpml_post_language_details', mixed $empty_value, int $post_id )
$empty_value
(mixed) (Required) This is normally the value the filter will be modifying. We are not filtering anything here so set this to NULL. This for the filter function to actually receive the full argument list
$post_id
(int) (Optional) The post id to retrieve information of (post, page, attachment, custom post) Defaults to current post ID.
hook example usage:

In the example below we use the filter to retrieve the language information for the “Hello World” post.
The original language of the post is English and we have translated it to German.

Example

$my_post_language_details = apply_filters( 'wpml_post_language_details', NULL, 1 ) ;

The above will return the following on the English version of the site:

array (
 'language_code' => 'en',
 'locale' => 'en_US',
 'text_direction' => false,
 'display_name' => 'English',
 'native_name' => 'English',
 'different_language' => false,
)

If we switch to the German version of the site we will see the following:

array (
 'language_code' => 'de',
 'locale' => 'de_DE',
 'text_direction' => false,
 'display_name' => 'German',
 'native_name' => 'Deutsch',
 'different_language' => true,
)
Back to the top

wpml_switch_language – Control language in WordPress queries

availability:
WPML Version: 3.2.2
description:

This action hook allows controlling the language in WordPress queries. It overrides the WPML language global variable used to filter database query requests.

Note: The action hook must be passed to the pre_get_posts WordPress hook. Use with caution since it affects the WPML language variable globally. To avoid unexpected results, use the hook in combination with WordPress conditional tags to only alter the specific query on the page you want.

type:
action
category:
Retrieving Language Information for Content
parameters:
do_action( 'wpml_switch_language', string $language_code )
$language_code
(string) (Required) The 2-letter language code to switch to. If set to null it restores the original language. If set to ‘all’ it will query content from all active languages. Defaults to null
hook example usage:

In the example below, please note the use of conditional tags to control the specific WordPress query we want. For additional information on the use of conditionals please refer to the pre_get_posts page in the WordPress Codex.

Example

// using the hook on a custom query on the WordPress index front end page
function my_custom_something($query) {
    if ( !is_admin() && is_index() && !$query->is_main_query() ) {
        do_action( 'wpml_switch_language', "de" );
    }
}
add_action('pre_get_posts', 'my_custom_something');
Back to the top

wpml_element_language_code – Get the language code for a translatable element

availability:
WPML Version: 3.2.2
description:

This filter hook will retrieve the language code for a translatable element by querying WPML’s icl_translations database table.

Also look at wpml_element_language_details if you are interested in more information such as the element’s trid

type:
filter
category:
Retrieving Language Information for Content
parameters:
apply_filters( 'wpml_element_language_code', string $language_code, array $args )
$language_code
(string) (Required) A 2-letter language code. Defaults to null.
$args
(array) (Required) An array of arguments to be used.

  • element_id(bool) Use term_taxonomy_id for taxonomies, post_id for posts
  • element_type(string) The type of element to check. Can be a post type: post, page, attachment, nav_menu_item, {custom post key} or taxonomy: category, post_tag, nav_menu {custom taxonomy key}
hook example usage:

Example

$args = array('element_id' => 10, 'element_type' => 'category' );
$my_category_language_code = apply_filters( 'wpml_element_language_code', null, $args );
Back to the top

wpml_element_language_details – Get the trid, language code and source language code for a translatable element

availability:
WPML Version: 3.2.2
description:

This filter hook will retrieve language information for a translatable element. It queries WPML’s icl_translations database table and returns an object with the element’s trid, source language code and language code.

If you are only looking for the element’s language code you might prefer to use wpml_element_language_code

type:
filter
category:
Retrieving Language Information for Content
parameters:
apply_filters( 'wpml_element_language_details', object $element_object, array $args )
$element_object
(object) (Required) A WordPress object. Defaults to null.
$args
(array) (Required) An array of arguments to be used.

  • element_id(bool) Use term_taxonomy_id for taxonomies, post_id for posts
  • element_type(string) The type of element to check. Can be a post type: post, page, attachment, nav_menu_item, {custom post key} or taxonomy: category, post_tag, nav_menu {custom taxonomy key}
hook example usage:

Example

$args = array('element_id' => 10, 'element_type' => 'category' );
$my_category_language_info = apply_filters( 'wpml_element_language_details', null, $args );
Back to the top

wpml_switch_language_for_email – Allows you to switch email language to user's preferred language

availability:
WPML Version: 3.2.0
description:

This action hook switches the language of your emails to the user’s preferred language. This is helpful when you need to send emails in different languages. Make sure to reset the language back by using wpml_restore_language_from_email. Also, see the extended documentation: Sending Emails with WPML.

If the given email is not registered, nothing will happen.

type:
action
category:
Retrieving Language Information for Content
parameters:

$email
(string)
The user email.

hook example usage:

Example

Send an email to all users in their language.

$all_users = get_users();

foreach( $all_users as $user ) {
	// Switch language to user language.
	do_action('wpml_switch_language_for_email', $user->user_email );

	$subject = __('Hello, World!', 'text-domain');
	$message = __('This is our newsletter', 'text-domain');

	wp_mail( $user->user_email, $subject, $message);

	// Switch language back.
	do_action('wpml_restore_language_from_email');
}
Back to the top

wpml_restore_language_from_email – Restore the language that was active before calling wpml_switch_language_for_email

availability:
WPML Version: 3.2.0
description:

Restore the language that was active before calling wpml_switch_language_for_email.

type:
action
category:
Retrieving Language Information for Content
parameters:
hook example usage:

See wpml_switch_language_for_email

Back to the top