How does this work? Is there functions or hooks for it?
]]>i need to add the rel=”sponsored” attribute to old affiliate links in my websites as google said to avoid algorithmic penalty ,
Most of those links were created as buttons using the Shortcodes Ultimate plugin.
i tried this code in the snippet plugin:
function add_sponsored_rel_attribute($content) {
// Define an array of allowed domain keywords
$allowed_domain_keywords = array(
‘amazon’,
‘jumia’
// Add more keywords as needed
);
// Add rel="sponsored" to external links for domains containing allowed keywords
$content = preg_replace_callback(
'/<a[^>]+href=[\'"]([^\'"]+)[\'"][^>]*>/i',
function ($matches) use ($allowed_domain_keywords) {
$link_url = $matches[1];
// Check if the link is external
$link_domain = parse_url($link_url, PHP_URL_HOST);
// Check if the link's domain contains any allowed keyword
foreach ($allowed_domain_keywords as $keyword) {
if (stripos($link_domain, $keyword) !== false) {
// Add rel="sponsored"
return '<a href="' . $link_url . '" rel="sponsored">';
}
}
return $matches[0];
},
$content
);
return $content;
}
// Hook the function to the_content filter
add_filter(‘the_content’, ‘add_sponsored_rel_attribute’);
add_filter(‘widget_text_content’, ‘add_sponsored_rel_attribute’);
——————————-
the above code works fine for all external links containing the keywords i provided in the code.
but it doesn’t work for the external links were created buttons in from the Shortcodes Ultimate plugin ??
i tried differents codes, but all were unable to add the rel=”sponsored” attribute to the Shortcodes Ultimate Buttons?
please help me.
I tried Rank Math, plugins like WP Links and used the codes below to handle these links automatically, to no avail.
Code 1:
add_filter(‘woocommerce_loop_add_to_cart_link’, ‘add_rel_attribute_to_external_products’, 10, 2);
function add_rel_attribute_to_external_products($link, $product) {
if ($product->is_type(‘external’)) {
$link = str_replace(‘<a ‘, ‘<a rel=”sponsored” ‘, $link);
}
return $link;
}
Code 2:
add_filter(‘woocommerce_loop_add_to_cart_link’, ‘add_rel_attribute_to_external_products’, 10, 2);
function add_rel_attribute_to_external_products($link, $product) {
if ($product->is_type(‘external’)) {
$link = str_replace(‘<form ‘, ‘<form rel=”sponsored” ‘, $link);
}
return $link;
}
Thanks.
]]>I’m using Pretty Links with my demo site (live, but not indexed) and I enabled the sponsored tag option. However, when I inspect the links with Chrome DevTools, the sponsored rel tag isn’t added.
Is there something wrong or did I just misunderstand how the tag is added (screenshot)?
Thank you!
Adam
Normally I view 20 links per page in the screen options, so I decided to drop it down to just 10 links per page to see if that would help. It did not help – still no change.
]]>I have a custom field with the “Data type” “Link” and I need to set the attributes “nofollow” and “sponsored” on the links.
As of now I can only add “target=”_blank” with the “Open in new window” option in the templates editor.
I need it to look like this:
<a href="https://www.example.com" rel="nofollow sponsored" "target="_blank">example link</a>
Is there any way to add nofollow and sponsored to all links used with data type “link”?
Thanks!