For the complete documentation index, see llms.txt. This page is also available as Markdown.

Implementing Custom Link Translation

By default, Weglot translates links based on built-in patterns. In some cases you may want to build a translated link yourself. Below is an example that returns the URL of a WooCommerce product in the current language. You can adapt it further — for instance to target a specific language instead of the current one.

function weglot_get_current_full_product_by_id( $product_id ) {
	$post = get_post( $product_id );
	if ( ! $post || $post->post_type !== 'product' ) {
		return '';
	}
	$product_url = get_permalink( $product_id );
	return weglot_create_url_object( $product_url )->getForLanguage( weglot_get_request_url_service()->get_current_language() );
}
echo weglot_get_current_full_product_by_id( 17 );

Last updated