rel_canonical() WordPress Function

The rel_canonical() function is a powerful tool for managing your Wordpress site's SEO. This function allows you to specify the canonical URL for your site, which can help you avoid duplicate content issues with search engines. You can use the rel_canonical() function to specify the canonical URL for your entire site, or for specific pages or posts.

rel_canonical() #

Outputs rel=canonical for singular queries.


Source

File: wp-includes/link-template.php

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";
	}
}


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More