Pandoma
Pandoma

Reputation: 37

How do I hide referrer link in woocommerce product

I have a link that is in the short description of a woo-commerce product. This link type is there in every product I have (different of course).

I'll need a script to automatically change the links. These are the attempts I tried so far:

  1. WP-HideRefer I have installed the plugin (https://wordpress.org/plugins/wp-hiderefer/), did not work. Did not change any links.

  2. Added scripts I used the script provided in https://anonym.to/ and put in at the end of my footer.php (before ), did not work.

  3. Tried many other Wordpress hide referrer plugins, did not work.

Up until this point, I'm lost. I am not sure.

TL:DR Appending text in front of every external link (link in short desc/normal desc of woocommerce product EXCEPT own website domain/link.

https://www.google.com/ is on my woocommerce product. 
When user loads the product, he sees https://anonym.to/?https://www.google.com/ instead. 
(https://anonym.to/? + link)

Upvotes: 0

Views: 379

Answers (1)

Wesley - Synio
Wesley - Synio

Reputation: 684

This code snippet will replace all URLs on a WooCommerce product page in both the excerpt (short description) as well as the longer description/content if you want.

It uses a regex to replace the URLs.

function so61548594_anonymize_urls($description) {
    $prefix = 'https://anonym.to/?';
    $description = preg_replace(
        '#(<a.*?)href=["\']([^"\']+)["\'](.*?>[^>]*</a>)#i',
        ('$1href="' . $prefix . '$2"$3'),
        $description
    );
    return $description;
}

add_filter('woocommerce_short_description', 'so61548594_anonymize_urls');
add_filter('the_content', function ($description) {
    if (function_exists('is_product') && is_product()) {
        return so61548594_anonymize_urls($description);
    }
    return $description;
});

Let me know if it works for you.

Upvotes: 0

Related Questions