• First I want to thank you for this great plugin!
    Now, it seems the links that the user post in their adverts are “dofollow”. How can we add “nofollow” attribute?
    Thank you!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    thanks i am glad you are liking the WPAdverts so far :).

    Regarding your question, you can add the code below in your theme functions.php file (or even better create a new WP plugin and add it there) it will add nofollow attribute to all links in the description.

    
    function my_nofollow($content) {
        return preg_replace_callback('/<a[^>]+/', 'my_nofollow_callback', $content);
    }
    function my_nofollow_callback($matches) {
        $link = $matches[0];
        $site_link = get_bloginfo('url');
    
        if (strpos($link, 'rel') === false) {
            $link = preg_replace("%(href=\S(?!$site_link))%i", 'rel="nofollow" $1', $link);
        } elseif (preg_match("%href=\S(?!$site_link)%i", $link)) {
            $link = preg_replace('/rel=\S(?!nofollow)\S*/i', 'rel="nofollow"', $link);
        }
        return $link;
    }
    add_filter('adverts_the_content', 'my_nofollow');
    

    Hello,

    I am just curious what is the benefit of making the links a nofollow

    Will this plugin work for this request https://wordpress.org/plugins/wp-external-links/

    Can you also add rel=sponsored and rel=ugc to your code above

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    1. what each “rel” attribute does you can read here https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/rel

    2. to have “rel” set to “nofollow”, “sponsored” and “ugc” in the code above you would need to replace

    
    rel="nofollow"
    

    with

    
    rel="nofollow sponsored ugc"
    

    note there are two occurrences on lines 9 and 11.

    3. i am not sure if https://wordpress.org/plugins/wp-external-links/ will work with Advert description but my guess is it will not as the description of the advert is generated last once all filters already run.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to add nofollow attribute to links’ is closed to new replies.