WordPress как на ладони
rgbcode is looking for WordPress developers. Очень Удобный и Быстрый Хостинг для сайтов на WordPress. Пользуюсь сам и вам рекомендую!

get_comment_author_link()WP 1.5.0

Получает HTML ссылку (<a>) на сайт автора текущего комментария. Анкором ссылки будет имя автора. Если автор не указал свой сайт при комментировании, то функция вернет просто имя автора, без ссылки.

Работает на основе: get_comment_author_url(), get_comment_author()
Основа для: comment_author_link()
Хуки из функции

Возвращает

Строку. Имя автора комментария или HTML ссылку на сайт автора.

Использование

echo get_comment_author_link( $comment_ID );
$comment_ID(число/объект)
ID или объект комментария, ссылку автора которого нужно получить. По умолчанию, null - текущий комментарий в цикле комментариев.
По умолчанию: null - текущий комментарий.

Примеры

0

#1 Получим имя автора комментария в виде ссылки на сайт

Предположим в цикле комментариев нам нужно вывести имя автора комментария в виде ссылки на его сайт:

$author = get_comment_author_link();
echo $author;

/*
Вернет:
<a href="http://author-example.com/" rel="external nofollow" class="url">Евгений</a>

Если у автора нет ссылки на сайт, то вернет: 
Евгений
*/
0

#2 Укажем ID комментария

$author = get_comment_author_link( 76 );
echo $author;

// Вернет: <a href="http://author-example.com/" rel="external nofollow" class="url">Евгений</a>

Заметка

Перед тем как вернуть результат, функция прогоняет его через фильтр get_comment_author_link.

Список изменений

С версии 1.5.0 Введена.
С версии 4.4.0 Added the ability for $comment_id to also accept a WP_Comment object.

Код get_comment_author_link() WP 6.4.3

function get_comment_author_link( $comment_id = 0 ) {
	$comment = get_comment( $comment_id );

	$comment_id = ! empty( $comment->comment_ID ) ? $comment->comment_ID : (string) $comment_id;

	$comment_author_url = get_comment_author_url( $comment );
	$comment_author     = get_comment_author( $comment );

	if ( empty( $comment_author_url ) || 'http://' === $comment_author_url ) {
		$comment_author_link = $comment_author;
	} else {
		$rel_parts = array( 'ugc' );
		if ( ! wp_is_internal_link( $comment_author_url ) ) {
			$rel_parts = array_merge(
				$rel_parts,
				array( 'external', 'nofollow' )
			);
		}

		/**
		 * Filters the rel attributes of the comment author's link.
		 *
		 * @since 6.2.0
		 *
		 * @param string[]   $rel_parts An array of strings representing the rel tags
		 *                              which will be joined into the anchor's rel attribute.
		 * @param WP_Comment $comment   The comment object.
		 */
		$rel_parts = apply_filters( 'comment_author_link_rel', $rel_parts, $comment );

		$rel = implode( ' ', $rel_parts );
		$rel = esc_attr( $rel );
		// Empty space before 'rel' is necessary for later sprintf().
		$rel = ! empty( $rel ) ? sprintf( ' rel="%s"', $rel ) : '';

		$comment_author_link = sprintf(
			'<a href="%1$s" class="url"%2$s>%3$s</a>',
			$comment_author_url,
			$rel,
			$comment_author
		);
	}

	/**
	 * Filters the comment author's link for display.
	 *
	 * @since 1.5.0
	 * @since 4.1.0 The `$comment_author` and `$comment_id` parameters were added.
	 *
	 * @param string $comment_author_link The HTML-formatted comment author link.
	 *                                    Empty for an invalid URL.
	 * @param string $comment_author      The comment author's username.
	 * @param string $comment_id          The comment ID as a numeric string.
	 */
	return apply_filters( 'get_comment_author_link', $comment_author_link, $comment_author, $comment_id );
}
3 комментария
    Войти