Redirecting external product feature image to an external site in new tab - wordpress

hi have a WordPress woocommerce site…
i want to redirect the featured image of the product to an external website without going to the product page.
i achieved that by using this code in function file

**<?php // Do not include this if already open!**

** * Code goes in theme 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;**
**	}**

The problem is that it redirects in the same tab…i want it to redirect to a new tab

How can i achieve it…please help

You can’t do that. A redirect will always open in the current tab, no way around that.

If you want something to open in a new tab you can either make it a link <a> with target="_blank" or use in javascript.

Hi rpkamp,

Thanks for your help…as i am a bit new to coding. Can you help me where to add
<a> with target="_blank" in the above code which i have or where is the javascript located in my WordPress installation where i can add

You can’t add it to the code above.

Can you maybe give more context to the problem? What are you trying to achieve, functionally?

Hi rpKamp

I am trying to create a affiliate ecommerce website using woocomerce in WordPress where
if some one clicks on a featured image of a product in shop it opens the affiliate website link of the product in new tab rather than opening the product description page on my website

an exact example is here

Then that is where you need to tell the browser to open a new tab, upon clicking the image.

Seems to me that’s already what it’s doing. Has this issue been resolved?

1 Like

Not yet resolved…that’s an example website i have shared…i am making something like that…

What does your code for the image look like?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.