get_tag_link()WP 2.3.0

Retrieves the link to the tag by given tag ID.

1 time — 0.0039971 sec (very slow) | 50000 times — 2.05 sec (fast) | PHP 7.4.25, WP 5.9.2

No Hooks.

Return

String. Link on success, empty string if tag does not exist.

Usage

get_tag_link( $tag );
$tag(int|object) (required)
Tag ID or object.

Examples

#1. A basic use case. Let's display a link to tag 46:

echo get_tag_link( 46 ); //> http://example.com/tag/tagname

<a href="<?php echo get_tag_link(46); ?>">tag name</a>

#2 Filter

The output of this function is filtered through a hook tag_link, which passes two parameters to it's function:

  • $taglink - URL that function normally returns.
  • $tag_id - ID of the tag whose link is returned.

Example, filter the link - replace http:// with https://:

add_filter( 'tag_link', 'add_https_to_tag_link', 10, 2 );

function add_https_to_tag_link( $taglink, $tag_id ){

	$taglink = str_replace( 'http://', 'https://', $taglink );

	return $taglink;
}

Notes

Changelog

Since 2.3.0 Introduced.

Code of get_tag_link() WP 6.0

function get_tag_link( $tag ) {
	return get_category_link( $tag );
}