Implementing Custom Link Translation

By default, Weglot translates links based on specific patterns. However, in some cases, you may want to handle link translations manually. Below, we provide an example of a function to translate a WooCommerce product link. You can customize this method further, such as adding specific languages you wish to translate the link into.

function weglot_get_current_full_product_by_id($product_id) {
	$post = get_post($product_id);
	if ($post && $post->post_type === 'product') {
		$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