基于WordPress用户配置的WPML自动语言重定向问题求助
WordPress/WPML: Auto-Redirect to Language Based on User Profile Settings
Problem Summary
I'm building a WooCommerce wholesale store with WPML, configured with 5 language flags for currency purposes: Danish (DKK, main language da), German (EUR, de), English (EUR, en), Swedish (SEK, se), Norwegian (NOK, no). Note that Swedish and Norwegian flags point to English content—only currencies differ.
Need to auto-redirect logged-in users based on their WordPress profile language:
- Danish profile → Danish (
da) pages - Norwegian profile → Norwegian flag (
no) pages - Swedish profile → Swedish flag (
se) pages - German profile → German (
de) pages - All others → English (
en) pages
The original code from WPML forums wasn't working; looking for a fix.
Fixed Solution Code
Replace your existing code with this adjusted version, which addresses timing, mapping, and redirect logic issues:
function wpml_auto_redirect_user_language() { // Only run for logged-in users and non-admin pages if ( ! is_user_logged_in() || is_admin() ) { return; } // Get user's profile language locale (e.g. da_DK, nb_NO, sv_SE) $user_locale = get_user_locale( get_current_user_id() ); // Extract the 2-letter language code (fallback to full locale if needed) $user_lang = substr( $user_locale, 0, 2 ); // Define mapping of user languages to your WPML site language codes $lang_mapping = [ 'da' => 'da', 'de' => 'de', 'no' => 'no', 'nb' => 'no', // Handle Norwegian Bokmål locale 'nn' => 'no', // Handle Norwegian Nynorsk locale 'sv' => 'se', ]; // Determine target language (default to English for all unlisted languages) $target_lang = isset( $lang_mapping[ $user_lang ] ) ? $lang_mapping[ $user_lang ] : 'en'; // Get current WPML language code $current_lang = apply_filters( 'wpml_current_language', null ); // Only redirect if current language doesn't match target if ( $current_lang !== $target_lang ) { // Get the current page URL (properly escaped) $current_url = home_url( add_query_arg( [], $GLOBALS['wp']->request ) ); // Convert URL to target language using WPML $redirect_url = apply_filters( 'wpml_permalink', $current_url, $target_lang ); // Safe redirect with exit to stop execution wp_safe_redirect( $redirect_url ); exit; } } // Use template_redirect hook (better timing for front-end redirects) add_action( 'template_redirect', 'wpml_auto_redirect_user_language' );
Key Fixes & Explanations
- Hook Timing: Switched from
pre_get_poststotemplate_redirect—this hook runs later in the load process, avoiding conflicts with query setup and ensuring redirects happen before any content is rendered. - Language Mapping: Added explicit mapping for Norwegian (
no,nb,nn) and Swedish (sv) to your WPML language codes (no,se), ensuring these users get directed to the correct flag/currency pages even though content is English. - User Check: Added
is_user_logged_in()to only apply redirects to logged-in users (since guest users don't have a profile language set). - URL Handling: Used
home_url()and$GLOBALS['wp']->requestinstead of raw$_SERVERvariables for more reliable, WordPress-native URL generation. - Exit After Redirect: Added
exit;right afterwp_safe_redirect()to prevent any subsequent code from running, which fixes cases where redirects might be ignored. - Fallback Logic: Defaults to English (
en) for any unlisted user languages, aligning with your requirements.
Additional Notes
- Ensure your WPML language codes (
da,de,en,no,se) match exactly what's configured in WPML → Languages → Language Codes. - Test with different user profiles to confirm redirects work as expected. For example, a user with
sv_SElocale should be sent to theselanguage version of the current page. - If you need to handle guest users, you could extend this code to use browser language detection (via
$_SERVER['HTTP_ACCEPT_LANGUAGE']) as a fallback, but that's outside the original scope.
内容的提问来源于stack exchange,提问作者Frederic
相关产品推荐
相关产品推荐

