Google announce sometime back in September 2019 about rel=”sponsored” change into rel attribute.
rel=”sponsored”: Use the sponsored attribute to identify links on your site that were created as part of advertisements, sponsorships or other compensation agreements.
In order to add rel=”sponsored” metadata to link please add WordPress filters to functions.php file.
add_filter('the_content', 'crunchify_tips_on_add_rel_sponsored'); add_filter('the_excerpt', 'crunchify_tips_on_add_rel_sponsored');
Also, add below two functions PHP code to your WordPress theme’s functions.php file.
rel=”sponsored” code:
function crunchify_tips_on_add_rel_sponsored($content) { return preg_replace_callback('/<a[^>]+/', 'crunchify_add_sponsored_all_external_links', $content); } function crunchify_add_sponsored_all_external_links($crunchifyMatches) { $crunchifyLink = $crunchifyMatches[0]; $crunchifySiteLink = get_bloginfo('url'); if (strpos($link, 'rel') === false) { $crunchifyLink = preg_replace("%(href=\S(?!$crunchifySiteLink))%i", 'rel="sponsored" $1', $crunchifyLink); } elseif (preg_match("%href=\S(?!$crunchifySiteLink)%i", $crunchifyLink)) { $crunchifyLink = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="sponsored"', $crunchifyLink); } return $crunchifyLink; }
rel=”nofollow” code:
Just replace sponsored
with nofollow in above code. Here is an updated code.
function crunchify_tips_on_add_rel_sponsored($content) { return preg_replace_callback('/<a[^>]+/', 'crunchify_add_sponsored_all_external_links', $content); } function crunchify_add_sponsored_all_external_links($crunchifyMatches) { $crunchifyLink = $crunchifyMatches[0]; $crunchifySiteLink = get_bloginfo('url'); if (strpos($link, 'rel') === false) { $crunchifyLink = preg_replace("%(href=\S(?!$crunchifySiteLink))%i", 'rel="nofollow" $1', $crunchifyLink); } elseif (preg_match("%href=\S(?!$crunchifySiteLink)%i", $crunchifyLink)) { $crunchifyLink = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $crunchifyLink); } return $crunchifyLink; }
I hope this WordPress tutorial helps you add rel=”sponsored” attribute to outgoing links.