Samuel Elh Blog

WordPress, bbPress, BuddyPress, JavaScript tutorials and snippets

Tag: comments

Link WordPress comment author to BuddyPress profile

In this quick tutorial, we will filter the WordPress comment author link URL and point to BuddyPress profile of this author as long as the comment was made by a verified user of your blog.

We can hook into get_comment_author_link for filtering the comment author link, and to get the BuddyPress profile link of a given user, you can use

bp_core_get_user_domain( $user_id )

for this purpose for which you specify a user ID in the first parameter.

The code:

Add the following code to your child theme’s functions file or with a custom plugin:

add_filter('get_comment_author_link', function( $link ) {

	if ( !function_exists('bp_core_get_user_domain') )
		return $link;

    global $comment;
    
    if ( !empty( $comment->user_id ) && !empty( get_userdata( $comment->user_id )->ID ) ) {

   		$link = sprintf(
   			'<a href="%s" rel="external nofollow" class="url">%s</a>',
   			bp_core_get_user_domain( $comment->user_id ),
   			strip_tags( $link )
   		);

    }

    return $link;
});

To make the avatar clickable as well, see how to add Link to WordPress Comment Avatar

Link WordPress comment author to bbPress profile

In this quick tutorial, we will filter the WordPress comment author link URL and point to bbPress profile of this author as long as the comment was made by a verified user of your blog.

We can hook into get_comment_author_link for filtering the comment author link, and to get the bbPress profile link of a given user, you can use

bbp_user_profile_url( $user_id )

for this purpose for which you specify a user ID in the first parameter.

The code:

Add the following code to your child theme’s functions file or with a custom plugin:

add_filter('get_comment_author_link', function( $link ) {

	if ( !function_exists('bbp_user_profile_url') )
		return $link;

    global $comment;
    
    if ( !empty( $comment->user_id ) && !empty( get_userdata( $comment->user_id )->ID ) ) {

   		$link = sprintf(
   			'<a href="%s" rel="external nofollow" class="url">%s</a>',
   			bbp_get_user_profile_url( $comment->user_id ),
   			strip_tags( $link )
   		);

    }

    return $link;
});

To make the avatar clickable as well, see how to add Link to WordPress Comment Avatar

Add Link to WordPress Comment Avatar

In this quick snippet of coding tutorial, we learn about adding an anchor link to WordPress comment avatar.

Link WordPress Comment Avatar

By default, if a user has left a comment on your WordPress blog through a post comment form, they can specify a link to their website, which will be assigned as anchor to the name they specified and displayed within the comments loop, and WordPress does not make this link added to the avatars.

Filtering get_avatar:

Simple work-around for achieving this is by filtering entire get_avatar output, which we will concentrate to apply this only when the queried avatar (gravatar) is used for comments, and this can be achieved by checking the second parameter provided while hooking into get_avatar, if it is a comment identifier, while it can be a user ID, email address or something else.

The coding:

Add the following code to your child theme’s functions file or with a custom plugin:

/**
  * PHP < 5.3 ? use a custom function instead of the anonymous callback
  */

add_filter('get_avatar', function( $avatar, $indent ) {

	/**
	  * check if the current queried avatar is for comments
	  */

	if ( !empty( $indent->comment_ID ) ) { // now that's a comment avatar

		/**
		  * Check if the comment poster has left a link
		  * You can link somewhere else regardless of link availability
		  * by commenting out the if statement lines and specifying a $url
		  * in the sprintf 2nd param, e.g link to bbPress, BuddyPress, author
		  * archives, etc..
		  */

		if ( "" < ( $url = get_comment_author_url( $indent->comment_ID ) ) ) {
			$avatar = sprintf(
	   			'<a href="%s" rel="external nofollow" class="url">%s</a>',
	   			$url,
	   			$avatar
			);
		}

	}

	return $avatar;

}, 10, 2);

That should make the avatar clickable in the comments section and ONLY if the comment author has left and specified a URL in the website comment form field while submitting.

Filter avatar link:

You can link to something else, such as bbPress profile

bbp_get_user_profile_url( $ident->user_id )

or BuddyPress profile

bp_core_get_user_domain( $ident->user_id )

or even to the author archives of this user ( and to know if the comment author is a verified registered user, just check if $ident->user_id is not empty ):

get_author_posts_url( $ident->user_id )

This should always work as long as you’re pulling the comment avatar by the comment ID in your comments output callback function, that if you are overriding the default WordPress’s comments callback.

© 2017 Samuel Elh - Powered by WordPress, DigitalOcean & NameCheap

Theme by Anders NorenUp ↑

Subscribe to our mailing list

Sign up to receive updates about WordPress, free and premium plugins and themes in general and tips and tricks

* indicates required