rel_canonical()

In this article

Outputs rel=canonical for singular queries.

Source

function rel_canonical() {
	if ( ! is_singular() ) {
		return;
	}

	$id = get_queried_object_id();

	if ( 0 === $id ) {
		return;
	}

	$url = wp_get_canonical_url( $id );

	if ( ! empty( $url ) ) {
		echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
	}
}

Changelog

VersionDescription
4.6.0Adjusted to use wp_get_canonical_url().
2.9.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.