WPML language switcher. Link to custom page when no translation - wordpress

I have a question. I am using WPML plugin. In WPML->languages->Language switcher options i checked "Link to home of language for missing translations". Now when i click language switcher country flags and translation is missing it will redirect to homepage. My question is how to redirect to custom page when translation is not found. I want to create page with text "Sorry translation is missing. Please contact us for more info..."
Thank you for your time

First make sure you have WPML->languages->Language switcher options "Link to home of language for missing translations" checked. Then create your custom translation not found page in main language and translate it to other languages using WPML. Then add this code to your functions.php
add_filter('wpml_ls_language_url', 'redirect_link',10,2);
function redirect_link($url,$lang){
if($lang['missing'] == 1) {
$permalink_to_translation_not_found_page_in_main_language = get_the_permalink(40); //40 is page id of a custom translation not found page in main language
$lang['url'] = apply_filters( 'wpml_permalink', $permalink_to_translation_not_found_page_in_main_language , $lang['language_code'] );
}
return $lang['url'];
}
I hope this is useful for someone

Related

How does __() decide which language to use (WordPress)

I'm using __("english text", "textdomain") in a WordPress theme template.
The base language of the site is English.
I've successfully added a .pot file for German.
If I set the language in Settings, General to German, I get the German translations on the front end no problem.
I'm trying to implement a language selector, so on the front end the site visitor can select a language.
I thought it was just a case of setting the lang attribute in the html tag appropriately. So when the user selects German, I output <html lang="de-DE">. But still __() uses the language from the WordPress admin settings.
I guess I'm approaching this in the wrong way. Does __() only ever use the language set in WP Admin? Or can I force it to use a different translation file depending on what the visitor selects on the front end?
Thanks.
You might need to use the WordPress filter locale https://developer.wordpress.org/reference/hooks/locale/
You can do something like this in your function.php
//set desired locale
add_filter( 'locale', function($locale) {
if ( !is_admin() )
$locale = "de-DE";
return $locale;
});

Wordpress: Polylang and ACF Options page on ajax calls takes default language value

I'm working on a website, which is multilingual.
We're using Polylang and the ACF custom fields plugin.
Works fine in general, the issue is with the ACF Option pages.
The option pages are translated also in different languages.
The content we are taking from there is being displayed according to translation - in english on the english version of a page, french on the french etc.
The problem: We have a contact us form, where we take the recipients email address from the ACF option pages.
(We want to send it to a different recepient when its a different language.)
Here it always takes the email address from the default language option page and I don't understand why.
We are taking the email recipient for the ajax call with get field command, like on pages displaying content:
get_field('service_email', 'option' );
Anyone got an idea what could cause this? Or where to look?
In the end we found the solution. It took a bit of digging, but I hope this helps if anyone encounters the same issue.
We needed to add the following setup in the functions.php of our theme to have the ACF options pages also translated for the each language:
// Translating Options Page Compatibility
// add filter with the path to your acf installation
add_filter('acf/settings/default_language', 'my_settings_default_language');
add_filter('acf/settings/current_language', 'my_settings_current_language');
function my_settings_default_language( $lang ) {
if($lang == "") {
$lang = pll_default_language(); // pll_ is a polylang function
}
return $lang;
}
function jfrog_settings_current_language( $lang ) {
$lang = pll_current_language();
return $lang;
}
Side Note: We're using a theme installed version of ACF.
hope this helps, Cheers

Woocommerce mini-cart language keeps showing the same default language

I have been trying to change the language of mini cart without success. It keeps showing the same default set language, even though I choose a different language using polylang.
I have tried the below code in functions.php, but it keeps returning the same French version of the page.
function modify_cart_url($wc_get_page_permalink) {
$setLocale = pll_current_language();
echo $cart_page_id = pll_get_post(2374,"$setLocale");
$wc_get_page_permalink = get_permalink($cart_page_id);
return $wc_get_page_permalink;
}
add_filter( 'woocommerce_get_cart_url', 'modify_cart_url',10, 1);
2374 is the page ID of the French cart page.
So in case if anyone facing the same problem,
You can copy the complete woocommerce - mini-cart.php code inside the header.php of your theme.
After that you can get the correct locale set whenever you change the language using polylang.
And the get the cart page url of that particular language.
$curr_set_lan =get_locale();
$woo_cart_page_id = get_option( 'woocommerce_cart_page_id' );
the_permalink(pll_get_post($woo_cart_page_id, $curr_set_lan));

WPML same slug(url) different language

I have this site :
example.com/watches (watches is a custom post type)
There are two available languages : EN(default language), GR.
When i change to GR (while on page example.com/watches)
it redirects to example.com/el/watches which is right.
When i am on example.com/watches/rolex and i try to change language it redirects to example.com/el/taxwatches/rolex-el but i want it just to be example.com/el/watches/rolex/.
taxwatches is my custom taxonomy.
rolex-el is the slug of the term inside that taxonomy.
Has anyone experienced an issue like this before?
I've tried to save the permalinks again and check my WMPL settings but i can't see something wrong.
EDIT 1 : If i manually go to example.com/el/watches/rolex it will work fine.
Both example.com/el/watches/rolex and example.com/el/taxwatches/rolex-el work.
EDIT 2: From what i understand WPML takes the slugs, is there a way to change it so it takes the names?
Ok so for anyone having problems with multi language sites - but want to have the "same slugs" in the urls (will never be same slugs - but going to look like that) read the below.
WPML uses slugs which are created from the wordpress core structure - you can't have two slugs with the exactly same name. If default language is EN and you have a secondary, in my case GR, then one slug is going to be "myslug" and the other "myslug-gr" (you can customize this "-gr" through WPML settings).
In functions.php you can filter the "wpml_ls_filter" function which is fired when your Language switcher is created.
Code as below :
add_filter('icl_ls_languages', 'wpml_ls_filter');
function wpml_ls_filter($languages) {
foreach ($languages as $lang_code => $language) {
$mTempString = $languages[$lang_code]['url'];
echo $mTempString; // HERE
// If "tax" is found in that string, replace it with "" - remove it.
if (strpos($mTempString, "tax") !== false) {
$languages[$lang_code]['url'] = str_replace("tax", "", $mTempString);
}
}
return $languages;
}
The above echo is going to show you the url that is created (and you can manipulate it) of each language button when pressed whenever you visit a page/post.
I haven't wrote a complete solution because that really depends on what you want to do. For example the above code helped me achieve to remove "tax" if found in the url.
This answer is providing an alternative way to achieve multiple languages with same slug
Polylang with Polylang Slug these 2 plugins can do
https://wordpress.org/plugins/polylang/
https://github.com/grappler/polylang-slug/
You can use my plugin, it is based on the newest Polylang Pro module and does what you need: https://github.com/lufton/polylang-share-slug

add custom field template to theme admin page

in WP admin how to add the custom field template plugin to a theme page?
it automatically shows in posts and pages but i want this in the theme page. the theme am using is from iwak "creations" portfolio page.
what files do i need to modify to add this?
It's very hard to say what you need to modify without being able to look at the code. Being a premium theme, we can't just download it and take a look.
Having said that, the theme may use the WordPress custom post type functionality. Search the code for a call to the register_post_type function. If it's used, you may be in luck. Either
add 'custom-fields' to the supports argument in that call, or
call add_post_type_support after the post type is registered. The $post_type parameter will be the first value passed to the register_post_type function, and the $supports parameter will be 'custom-fields'.
Daniel, are you using this Custom Post Type Plugin - http://wordpress.org/extend/plugins/custom-field-template/screenshots/? I've used it before, and the guy who created it is Japanese, so his personal page isn't very useful as far as support for english goes.
I had some trouble with this at first. But what I think you're trying to do is add the custom fields to your new pages you've created, correct?
It's not very straightforward, but once it works it's pretty awesome.
Basically, when you set up the plugin you create these different "Custom fields," right? Well part of that should look like this:
[Custom Field Name]
type = textarea
rows = 4
cols = 40
tinyMCE = true
htmlEditor = true
Ok, so once you've created those "Custom fields" keep note of the part in brackets. You'll add this to your template pages:
<?php getCustomField('Custom Field Name'); ?>
Now when you enter the info in the pages or posts, the content should appear as you've entered it.

Resources