There’s a few things to consider here.
1) You should always first check for a third-party plugin like WPML. If, for whatever reason, WPML stops working or gets deactivated, assuming it’s there could result in a fatal error.
function directByLang() {
if( defined( 'ICL_SITEPRESS_VERSION' ) ) {
$current_lang = apply_filters('wpml_current_language', NULL);
if ($current_lang == 'en') {
// English link
echo '<a href="/en/cart/">English Cart</a>';
} elseif ($current_lang == 'fr') {
// French link
echo '<a href="/panier/">French Panier</a>';
} else {
// Default to French if language is not determined
echo '<a href="/panier/">French Panier</a>';
}
} else {
// Default to French if WPML not active
echo '<a href="/panier/">French Panier</a>';
}
}
I haven’t used WPML in a few years so not sure if this is the correct method anymore. I found it here: https://wpml.org/forums/topic/check-if-wpml-is-active/
2) This does check for the language and then depending on the language it outputs the different cart URLs. But just adding this snippet/function alone to your functions.php
won’t do anything. Because it doesn’t get loaded anywhere. This is basically a template tag, so in the file where you want this to appear, you have to add <?php directByLang(); ?>
.
I would assume that you want it in your header.php
.
For example, in your header.php
you’ll have some <div>
and <nav>
elements that organize he logo/site name, navigation, search field, etc…
Somewhere in there, where it makes sense, you’ll want to add <?php directByLang(); ?>
so that the function executes in there.
You’ll probably need some CSS, maybe even wrap it in it’s own container, like so:
<div class="multi-language-cart-link">
<?php directByLang(); ?>
</div>
Hope that helps.