Replies: 0
Hello, I am trying to redirect the image and product name links to affiliate URLs, as by default they link to the product page.
So far, I came to a solution in which I can redirect them to the affiliate external links adding this piece of code to functions.php:
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
wp_redirect( $product->get_product_url() );
exit;
}
}
However, I would like to make this links nofollowed as they are affiliate links, as well as open them in a new tab, but after trying several coding options and plugins I haven’t found a suitable solution.
Using plugins that would nofollow all external links is not working as they are treated now as internal links (even though redirected to external).
Any help would be much appreciated, thank you.