1

Where the date and time of sending each comment is displayed in the comments, it is also a hyperlink. I want to nofollow this a tag. I was able to do this through this file in this path: wp-includes/class-walker-comment.php Line 448 '<a href="%s"><time datetime="%s">%s</time></a>', converted to: '<a rel="nofollow" href="%s"><time datetime="%s">%s</time></a>',

But this is lost with every WordPress update! I am looking for a way to make this change permanent, like adding a file to the theme (like WooCommerce) or adding a code in the theme function. Thank you for helping me.

1 Answer 1

0

Modifying core WordPress files, like class-walker-comment.php, is generally not recommended, as your changes will be overwritten with each WordPress update. Instead, you can achieve the same effect by using a child theme and custom functions. This way, your modifications remain intact across updates.

I tried a few different things but I was able to get it working using this technique: ( I am sure there are other solutions )

  1. Find your comments.php file in your theme (child theme preferably)
  2. Replace this: (or something similar, it is possible that themes can be slightly different depending.
wp_list_comments(
            [
                'style'       => 'ol',
                'short_ping'  => true,
                'avatar_size' => 42,
            ]
        );

With this:


        // Start output buffering
        ob_start();

        // Output comments
        wp_list_comments([
            'style'       => 'ol',
            'short_ping'  => true,
            'avatar_size' => 42,
        ]);

        // Capture the output
        $comments_list = ob_get_clean();
        // Regular expression to match date/time links and modify the hyperlink
        $pattern = '/<a .*?href="(.*?#comment-\d+)".*?>(.*?)<\/a>/i';
        $comments_list = preg_replace($pattern, '<a href="$1" rel="nofollow">$2</a>', $comments_list);
        // Output the modified list
        echo $comments_list;

5
  • Thanks big person, but this code didn't work, do I need to edit something?
    – Monoor
    Commented Jan 21 at 13:20
  • Not sure, but calling me "big person" seems condescending. I would recommend not speaking to people like that on this board and in general. It will make people not want to help you. Commented Jan 21 at 15:43
  • I was able to get it working by implementing the above changes. Please check. Commented Jan 21 at 16:16
  • 1
    I apologize for making you upset, Im not very good at English culture But in our culture this is a good word. Thank you, I will test it
    – Monoor
    Commented Jan 21 at 18:28
  • Hi, I tested it and it worked successfully, but I don't think it will work on all themes, for example on another of my sites that was using the 2024 theme, there was no such replacement! I don't know but maybe there is a better solution so that others can easily use it, or even a guide that more fully explains the same for all situations. Anyway, my problem is solved, I just wanted to see if it is possible for you to upvote my question? Because I can't do anything
    – Monoor
    Commented Jan 26 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.