Skip Navigation

Resolved

Topic Tags: Language Switcher

Overview of the issue

Using the language switcher to navigate to the secondary language(s) page does not work on the user’s page. Furthermore, the links on the left sidebar does not direct to the right language.

Workaround

To resolve this issue add the snippet below to the functions.php file found in your theme’s folder.

bbPress user language switcher fix
//Code for fixing the Language switcher
add_filter( 'icl_ls_languages', 'bbpress_user_ls_fix' );

function bbpress_user_ls_fix( $languages ) {
        global $wp_query, $sitepress_settings;
	if ($sitepress_settings['language_negotiation_type'] != 3 ) {
		if ( bbp_is_single_user_profile() ) {
			foreach ( $languages as $code => $language ) {
				$home = trailingslashit( get_home_url() );
				do_action( 'wpml_switch_language', $code );
				$lang_url                  = trailingslashit( apply_filters( 'wpml_home_url', $home ) );
				$url                       = str_replace( $home, $lang_url, bbp_get_user_profile_url() );
				$languages[ $code ]['url'] = $url;
			}
			do_action( 'wpml_switch_language', ICL_LANGUAGE_CODE );
		}
	} else {
		if ( bbp_is_single_user_profile() ) {
            $current_language = apply_filters( 'wpml_current_language', NULL );
            $default_language = $sitepress_settings["default_language"];
			foreach ( $languages as $code => $language ) {
                if ( $code != $default_language ) {
	                $url = bbp_get_user_profile_url();
	                $parsed = parse_url($url);
	                unset($parsed["query"]);
	                $parsed["query"] = "lang=" . $code;
	                $url = http_build_url($parsed);
	                $languages[ $code ]['url'] = $url;
                } elseif ( $code == $current_language ) {
	                $home =  get_home_url() ;
	                do_action( 'wpml_switch_language', $code );
	                $lang_url                  =  apply_filters( 'wpml_home_url', $home ) ;
	                $url                       = str_replace( $home, $lang_url, bbp_get_user_profile_url() );
	                $languages[ $code ]['url'] = $url;
                } else {
                    $url = bbp_get_user_profile_url();
	                $parsed = parse_url($url);
	                unset($parsed["query"]);
	                $url = http_build_url($parsed);
                    $languages[ $code ]['url'] = $url;
                }
			}
			do_action( 'wpml_switch_language', ICL_LANGUAGE_CODE );
		}
    }
        return $languages;
}

//Code for redirecting the sidebar links to the right language
add_filter( 'bbp_get_user_topics_created_url', 'bbpress_user_urls_fix' );
add_filter( 'bbp_get_user_replies_created_url', 'bbpress_user_urls_fix' );
add_filter( 'bbp_get_user_engagements_url', 'bbpress_user_urls_fix' );
add_filter( 'bbp_get_favorites_permalink', 'bbpress_user_urls_fix' );
add_filter( 'bbp_get_subscriptions_permalink', 'bbpress_user_urls_fix' );
add_filter( 'bbp_get_user_edit_profile_url', 'bbpress_user_urls_fix' );

function bbpress_user_urls_fix( $url ) {
	global $sitepress_settings;
	if ($sitepress_settings['language_negotiation_type'] == 3 ) {
		$default_language = $sitepress_settings["default_language"];
		$current_language = apply_filters( 'wpml_current_language', null );
        $length = strlen ("?lang=" . $current_language);
		$parsed           = parse_url( $url );
		if ( $current_language == $default_language ) {
			unset( $parsed["query"] );
		} else {
			$parsed["path"]  .= substr( $parsed["query"], $length );
			$parsed["query"] = "lang=" . $current_language;
		}
		$url = http_build_url( $parsed );
	}
	return $url;
}

23 Responses to “bbPress - Language switcher not working on user pages”