Skip to content

Instantly share code, notes, and snippets.

@junaidtk
Created November 12, 2019 05:13
Show Gist options
  • Save junaidtk/27e724dc73486f8c0e4c24f4b6ed5afe to your computer and use it in GitHub Desktop.
Save junaidtk/27e724dc73486f8c0e4c24f4b6ed5afe to your computer and use it in GitHub Desktop.
WPML translation
WPML plugin is used for making the multi lingual website.
This will allow user to add transalted version of posts, category, page, product etc.
WPML use the parse_query hook to modify query. Using this filter WPML adds query modification.
The argument suppress_filters is used for removing the filter added in the query
like posts_search, posts_search_orderby, posts_where etc. All these type of queries will execute if the suppress_filters is false.
Default value of suppress filter is false in the WP_Query. So it will return only the posts in the current language.
But get_posts() return all the posts in the posts. because suppress_filters is true in this function by default.
In order to modify the query done by the WPML, we can use the filter
$q = apply_filters( 'wpml_post_parse_query', $q );
This is the filter in the wpml-query-parser.php file after modifying the query.
If the get_posts contain 'include' argument then the WPML will modify the query according to the corresponding language.
So in order to modify the filter to as per the supplied in put we can add below filter.
add_filter('wpml_post_parse_query', 'skip_language_specific_product');
function skip_language_specific_product($q){
$q->query_vars['post__in'] = $q->query['include'];
return $q;
}
In order to get all the posts using the wp_query in other languages.
ust need to swicth language to default language then query.
=============================================================
if(class_exists('SitePress')){
$args['suppress_filters'] = true;
global $sitepress;
$current_lang = $sitepress->get_current_language();
$default_language = $sitepress->get_default_language();
$sitepress->switch_lang($default_language);
$products = wc_get_products($args);
$sitepress->switch_lang($current_lang);
}else{
$products = wc_get_products($args);
}
if(class_exists('SitePress')){
$args['suppress_filters'] = true;
}
====================================================================
In order to register the strings dynamically while creating a field or step.
We need to register the string while creating the same.
******************************************************************
function wmsc_wpml_register_string($name, $value ){
$context = 'text domain';
$name = "WMSC - ".$value;
if(function_exists('icl_register_string')){
icl_register_string($context, $name, $value);
}
}
function t($text){
if(!empty($text)){
$otext = $text;
$text = __($text, self::TEXT_DOMAIN);
if($text === $otext){
$text = self::icl_t($text);
if($text === $otext){
$text = __($text, 'woocommerce');
}
}
}
return $text;
}
function icl_t($value){
$name = self::ICL_NAME_PREFIX." - ".$value;
if(function_exists('icl_t')){
$value = icl_t(self::ICL_CONTEXT, $name, $value);
}
return $value;
}
******************************************************************
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment