/    Sign up×
Community /Pin to ProfileBookmark

How to add automatically no follow to specific links?

I have some cloaking affiliate links to which i would like to add the attributes of nofollow

However, i’m looking for a way in coding that will allow me to target links that contain specific string, like **/go/brand**

While searching, i found this code somewhere, but it didn’t work for me in functions.php, so i was hoping some of you guys might provide with something that might do the job for me. That would be much appreciated !

Thank you so much in advance !

[code]add_filter( ‘the_content’, ‘my_string_replacements’);
function my_string_replacements ( $content ) {
if ( ! is_admin() && is_main_query() ) {
return preg_replace(‘/<a(.*href=”.*/link/pluto/?”.*)>/’, ‘<a$1 rel=”nofollow”>’, $content );
}
return $content;
}[/code]

__(Added `[code]…[/code]` tags ~ MOD)__

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmDec 15.2019 — It appears that you are "returning" the result of the call to preg_replace, NOT $content. Have you attempted to output what is being returned to your caller?"

PS - not sure if the NOT operand works when you leave a space after it. Perhaps I am mistaken (happens!) but I always use something more like "!isadmin()"
Copy linkTweet thisAlerts:
@kurosaki4dauthorDec 15.2019 — Thank you for your reply !

I removed the condition, and it still didn't work !

PS: i don't get any error message whatsoever.
Copy linkTweet thisAlerts:
@VITSUSADec 16.2019 — @kurosaki4d#1611856 What is the benefit of No-follow backlinks in this situation?
Copy linkTweet thisAlerts:
@ginerjmDec 16.2019 — You don't get any error messages. Does that mean that you DO have it turned on?
Copy linkTweet thisAlerts:
@NogDogDec 17.2019 — Where is add_filter() defined?

To make sure you see all errors, add this at the top of your script:
<i>
</i>&lt;?php
ini_set('display_errors', true); // set to false before going live
error_reporting(E_ALL);
×

Success!

Help @kurosaki4d spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...