Hello! Pavel here. In this article, I want to show you how to “hide” the links of your blog’s commenters from being indexed by search engines.
Over the entire existence of this engine, tens of thousands of plugins have been created — including those related to comments. But in my case, I decided to take a non-standard route: I made some changes to the “functions.php” file of my theme. And now, the links to commenters’ websites are hidden. Moreover, I see more than one way to hide them (but more on that later).
This method of hiding links works with any WP theme. But! If you’re using a default theme — for example, twentytwelve — then after an update you’ll have to make changes to the “functions.php” file again, which is inconvenient. The best approach is to use this method with a child theme (as I do), or a custom theme.
The implementation example will be shown using the twentytwelve theme. To test it, you can create a child theme or edit the current one after making a backup.
So let’s begin with the root of the problem
We need to hide links to commenters’ websites. To determine which function is responsible for this, open your theme’s “comments.php” file and find the function wp_list_comments. It takes an array of parameters. The function with the “callback” key is responsible for rendering the comments list. In our case, that’s the function twentytwelve_comment. That’s exactly what we need. You can find it in the “functions.php” file at /wp-content/themes/twentytwelve/functions.php.
Open that file and look for the twentytwelve_comment function. Below is the relevant part of the code:
<header class="comment-meta comment-author vcard">
<?php
echo get_avatar( $comment, 44 );
printf( '<cite><b class="fn">%1$s</b> %2$s</cite>',
get_comment_author_link(),
( $comment->user_id === $post->post_author ) ? '<span>' . __( 'Post author', 'twentytwelve' ) . '</span>' : ''
);
printf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
esc_url( get_comment_link( $comment->comment_ID ) ),
get_comment_time( 'c' ),
sprintf( __( '%1$s at %2$s', 'twentytwelve' ), get_comment_date(), get_comment_time() )
);
?>
</header>
Inside this function, look for another one — get_comment_author_link. This is the function responsible for displaying links to commenters’ websites. We now need to copy it and paste it into the “functions.php” file under a new name.
Open the file /wp-includes/comment-template.php, find the get_comment_author_link function, copy it, and paste it into your “functions.php” file. Rename it — for example, to my_get_comment_author_link. Also, rename the call in twentytwelve_comment to use your new function — e.g., my_twentytwelve_comment.
Now we need to modify the new function that outputs the comment author link. Here’s an example implementation:
function my_get_comment_author_link( $comment_ID = 0 ) {
$url = get_comment_author_url( $comment_ID );
$author = get_comment_author( $comment_ID );
if ( empty( $url ) || 'http://' == $url ) {
$return = $author;
} else {
$return = "<span class='s-link' title='$url' onclick=\"goto('".str_replace('http://', '_', $url)."')\">$author</span>";
}
return apply_filters( 'my_get_comment_author_link', $return, $author, $comment_ID );
}
Note that we’re using a SPAN tag instead of an A tag, and the click action is handled via a JavaScript function called goto, which we’ll need to add to the theme’s footer.
Open the file /wp-content/themes/twentytwelve/footer.php and insert the following code between <?php wp_footer(); ?> and </body>:
<script language="Javascript" type="text/javascript">
function goto(l) {
window.open(l.replace("_","http://"));
}
</script>
Also don’t forget to add styles for the s-link class to make the span element look like a normal link:
.comments-area .bypostauthor cite span {
position: initial;
}
.comments-area .bypostauthor cite span.s-link {
font-weight: bold;
}
.comments-area span.s-link, .comments-area .bypostauthor cite span.s-link {
cursor: pointer;
font-size: 15px;
color: #444;
padding-left: 0;
margin-left: 0;
background: white;
border: none;
}
.comments-area span.s-link:hover {
color: #21759b;
text-decoration: underline;
}
.comments-area .bypostauthor cite .s-author {
position: absolute;
}
That’s all for now.
In one of my upcoming posts, I’ll describe other ways to hide links using PHP, jQuery, and maybe something else — if I come up with an idea or find inspiration elsewhere.
And in the next article I’ll explain how I managed to make the Hide My Dates plugin work nicely with the twentytwelve theme (since the comment dates display incorrectly with it).
If you have any other ideas on hiding links — feel free to share them in the comments.
Thanks for reading!
