Support » Plugin: WooCommerce » Product Image linked to External URL with rel=nofollow

  • 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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Stuart Duff

    (@stuartduff)

    Automattic Happiness Engineer

    Hey @pau4ner,

    You have two options for this that I can see.

    Firstly and the most preferable would be to use the woocommerce_single_product_image_html filter from the /woocommerce/templates/single-product/product-image.php file to add no follow to the HTML output.

    The second option would be to copy the file over into a theme and make the changes from there.

    https://docs.woocommerce.com/document/template-structure/

    • This reply was modified 4 years, 4 months ago by Stuart Duff.
    Thread Starter Pau Navarro

    (@pau4ner)

    Thanks Stuart, as you suggested I added the nofollow atribute in /woocommerce/templates/single-product/product-image.php file like this

    echo apply_filters(
    				'woocommerce_single_product_image_html',
    				sprintf(
    					'<a href="%s" itemprop="image" class="woocommerce-main-image zoom" title="%s" data-rel="prettyPhoto%s" rel="nofollow">%s</a>',
    					esc_url( $props['url'] ),
    					esc_attr( $props['caption'] ),
    					$gallery,
    					$image
    				),

    I hope I did it right. However I can’t see the nofollow rel while inspecting the link with Firebug. If it is an internal link being redirected to an external, should I still be able to see the nofollow rel?

    Thanks again

    Thread Starter Pau Navarro

    (@pau4ner)

    Hello Stuart, I finally did it. I placed the rel=”nofollow” in /woocommerce/includes/wc-template-functions.php like this:

    function woocommerce_template_loop_product_link_open() {
    	echo '<a href="' . get_the_permalink() . '" class="woocommerce-LoopProduct-link" rel="nofollow">';
    }

    Thanks for your help!

    Hi Pau,

    Thank you for this question. I just wanted to ask you to make this work did you delete the original code on the functions.php file above and replace it with the no follow in your last comment.

    If woo commerce has a update does it affect this code? Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Product Image linked to External URL with rel=nofollow’ is closed to new replies.