Ticker

6/recent/ticker-posts

Ad Code

Responsive Advertisement

How to remove the nofollow attribute from WordPress comments for self-links?

How to remove the nofollow attribute from WordPress comments for self-links?

If you’re a WordPress site owner, you might have noticed that all the links in the comments section of your website have the nofollow attribute.

This is a default behavior of WordPress to prevent spam comments and link manipulation.

However, if you’re a blogger or a business owner who wants to encourage users to link to their own content, you might want to remove the nofollow attribute from self-links in WordPress comments.

In this blog post, we’ll explain how to remove the nofollow attribute from WordPress comments for self-links using a filter function.

Step 1: Open the functions.php file of your theme

To remove the nofollow attribute from WordPress comments, you’ll need to add a filter function to your theme’s functions.php file.

The functions.php file is a PHP file that contains functions and hooks that control the behavior of your WordPress theme.

To open the functions.php file of your theme, log in to your WordPress dashboard, go to Appearance > Theme Editor, and click on the functions.php file from the list of theme files.

Step 2: Add the filter function

Once you’ve opened the functions.php file, add the following code at the end of the file:

function crunchify_remove_nofollow_from_self_links( $content ) {
    global $post;
    $content = preg_replace_callback('/<a[^>]+/', 'crunchify_remove_self_link_nofollow', $content);
    return $content;
}

function crunchify_remove_self_link_nofollow( $matches ) {
    $crunchifyLink = $matches[0];
    $crunchify_site_link = get_site_url();
    if (strpos($crunchifyLink, 'rel') === false && strpos($crunchifyLink, $crunchify_site_link) !== false ) {
        $crunchifyLink = preg_replace("%(href=\S(?!$crunchify_site_link))%i", 'rel="dofollow" $1', $crunchifyLink);
    } elseif (strpos($crunchifyLink, 'rel') !== false && strpos($crunchifyLink, 'nofollow') !== false && strpos($crunchifyLink, $crunchify_site_link) !== false ) {
        $crunchifyLink = preg_replace('/nofollow/', 'dofollow', $crunchifyLink);
    }
    return $crunchifyLink;
}

add_filter('comment_text', 'crunchify_remove_nofollow_from_self_links');

This code uses a regular expression to check for self-links in the comment content and replaces the nofollow attribute with a dofollow attribute.

The function remove_nofollow_from_self_links is hooked to the comment_text filter to apply this change to the comment content.

Step 3: Save the functions.php file

Once you’ve added the code, save the functions.php file.

And that’s it! The nofollow attribute will be removed from self-links in WordPress comments. Users can now link to their own content and get the SEO benefits of dofollow links.

Removing the nofollow attribute from self-links in WordPress comments can be a great way to encourage user engagement and promote your website’s content.

However, it’s important to remember that removing the nofollow attribute can also increase the risk of spam comments and link manipulation.

So, use this feature with caution and keep an eye on your comments section.

The post How to remove the nofollow attribute from WordPress comments for self-links? appeared first on Crunchify.

Enregistrer un commentaire

0 Commentaires