wpml_setting
Filters the WPML settings for language negotiation and other core configurations before they are applied.
add_filter( 'wpml_setting', $callback, 10, 1 );
Description
Filters the value of WPML settings. Developers can use this to dynamically alter WPML configurations, such as changing language negotiation types. The first parameter is typically `false` for core integrations.
Usage
add_filter( 'wpml_setting', 'your_function_name', 10, 1 );
Return Value
The filtered value.
Examples
/**
* Example of filtering the 'wpml_setting' hook to influence language negotiation type.
*
* This function intercepts the 'language_negotiation_type' setting from WPML.
* If the original value is 'false' (which it often is when no specific setting is found),
* this filter will provide a default value of '1' for the negotiation type,
* meaning languages will be appended to URLs.
*
* @param mixed $original_setting The original value of the setting.
* @param string $setting_key The key of the setting being filtered.
* @return int The modified language negotiation type.
*/
add_filter( 'wpml_setting', function( $original_setting, $setting_key ) {
// We only want to modify the 'language_negotiation_type' setting.
if ( 'language_negotiation_type' === $setting_key ) {
// If the original setting is not set or is false, default to type 1.
// Type 1 usually means languages are appended to URLs (e.g., example.com/en/).
// This is a common scenario for SEO and user experience.
if ( false === $original_setting ) {
return 1;
}
// Otherwise, return the original setting as provided by WPML.
return $original_setting;
}
// For any other WPML setting, pass through the original value.
return $original_setting;
}, 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/helpers/shared-functions.php:485
public static function add_group_seats_link( $group_id, $amount_seats ) {
$site = site_url();
$query_args = array(
'modify-group' => 'true',
'modify-group-id' => $group_id,
'new-qty' => $amount_seats,
);
if ( defined( 'WPML_PLUGIN_BASENAME' ) ) {
$language_negotiation_type = (int) apply_filters( 'wpml_setting', false, 'language_negotiation_type' );
$lang_code = apply_filters( 'wpml_current_language', null );
if ( ! empty( $lang_code ) ) {
if ( 1 === $language_negotiation_type ) {
$site .= '/' . $lang_code;
} elseif ( 3 === $language_negotiation_type ) {
$query_args['lang'] = $lang_code;
}
}
}
return add_query_arg( $query_args, $site );
}