Ampersands reformatting to # 038; and breaking links

I am using Woocommerce with affiliate links and the external urls are in the format
http://www.awin1.com/pclick.php?p=3626919167&a=112638&m=6418

The ampersands are being translated into # 038; (http://www.awin1.com/pclick.php?p=3626919167&a=112638&m=6418) which stops the link from working.

How can I stop this happening? In an ideal world I want none of this formatting applying to hyperlinks.

  • Nithin

    Hi sequin,

    Hope you are doing good today. :slight_smile:

    Could you please specify where you are implementing these links? It seems like the section where you are adding the link is being escaped, and hence you are getting this symbol. Could you please enable support access, so that we could see how you have implemented these links, and check whether we could provide a workaround.

    You can grant access from WPMU DEV > Support > Support Access > Grant Access, or check this manual: http://premium.wpmudev.org/manuals/wpmu-dev-dashboard-enabling-staff-login/

    Please let us know once you enable access, so that we could get this sorted. Have a nice weekend. :slight_smile:

    Kind Regards,
    Nithin

  • sequin

    Hi Nithin,
    Thanks for coming back so quickly. Support access is granted.

    I'm trying to redirect all the permalinks for affiliate products directly to the external link rather than having to click through the single page first. I have the following in my functions.php but I've commented it out for the time being until I can resolve the issue:

    // woocommerce: redirect loop links for external products
    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;
    	}
    }

    I'd like the links on image and product name both to open the external link in a new window but I can't figure out where the image link is being called so this is my current workaround (image link still opens in same window, which isn't ideal). Unfortunately calling the url this way seems to cause the ampersands in the link to be escaped and breaks the affiliate links.

    The url I want to call is used on the buy now button which I only use on the single product page.

  • Kasia Swiderska

    Hello sequin,

    This is happening because you are using wp_redirect and it sanitize urls by using https://developer.wordpress.org/reference/functions/wp_sanitize_redirect/

    Maybe here better would be handle this with jQuery and smiliar script:

    add_action( 'wp_footer', 'redirect_external_products' );
    function redirect_external_products() {
    	global $post;
    	if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
    		$link = $product->get_product_url();
    
    		?>
    
    			<script>
    			jQuery( document ).ready(function() {
     				jQuery('.product_title entry-title, .images a').click( function(){
     					var url = <?php echo $link; ?>;
     						window.open(url);
     				})
    			});
    			</script>
    		<?php
    
    	}
    }

    it is your script, but instead of redirect in WP it uses JS to open new window when title or image is clicked.

    let me know if this work for you

    kind regards,
    Kasia

  • Nithin

    Hi sequin,

    Hope you are doing good today. :slight_smile:

    If I'm not mistaken, you are looking to redirect the products in your /shop page, could you please try the following code in your functions.php, and check whether it works:

    add_action( 'template_redirect', 'woo_redirect_external_products' );
    function woo_redirect_external_products() {
    	global $post;
    	if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
    		wp_redirect( htmlspecialchars_decode( $product->get_product_url() ) );
    		exit;
    	}
    }

    The above code should direct to affiliate links, keeping & sign intact.

    To redirect these product pages to a new tab, please try the following code in your functions.php:

    function woo_affiliate_new_tab() {
    ?>
    <script>
    
    	jQuery(document).ready(function( $ ) {
    
    	$('a.woocommerce-LoopProduct-link').each(function(){
    		$(".product-type-external a[href^='http://']").attr("target","_blank");
      });
    
    });
    
    </script>
    
    <?php
    }
    
    add_action( 'wp_footer', 'woo_affiliate_new_tab');

    Would highly recommend that you make changes like this to a theme using a Child theme, so that it won't be overwritten when you update your theme.

    Please let us know if you still need any further assistance. Have a nice day. :slight_smile:

    Kind Regards,
    Nithin

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.