Getting the canonical URL of a WordPress page using wp_get_canonical_url

The wp_get_canonical_url function in WordPress is used to retrieve the canonical URL of the current webpage. This function can be useful for ensuring that search engines and other web services recognize the correct URL for a webpage, especially in cases where the webpage may have multiple URLs pointing to it.

By using the wp_get_canonical_url function, developers can programmatically retrieve the canonical URL and use it to set the appropriate rel="canonical" link tag in the webpage’s HTML header. This helps to prevent duplicate content issues and can improve the webpage’s search engine optimization (SEO) performance.

Parameters Accepted by the wp_get_canonical_url Function

The wp_get_canonical_url function accepts the following parameters:

  • $post (int|WP_Post): This parameter is optional and has a default value of null. It represents the Post ID or object. If no value is provided, the function will default to the global $post.

Value Returned by the wp_get_canonical_url Function

The wp_get_canonical_url function returns a string or false. It will return the canonical URL as a string, but if the post does not exist or has not been published yet, it will return false.

Examples

How to get the canonical URL for the current page

Use the wp_get_canonical_url function to retrieve the canonical URL for the current page:

$canonical_url = wp_get_canonical_url();

This code snippet retrieves the canonical URL for the current page and stores it in the $canonical_url variable.

How to get the canonical URL for a specific post

Use the wp_get_canonical_url function to retrieve the canonical URL for a specific post by passing the post ID as a parameter:

$post_id = 123;
$canonical_url = wp_get_canonical_url($post_id);

This code snippet retrieves the canonical URL for the post with ID 123 and stores it in the $canonical_url variable.

How to output the canonical URL in a custom template

Use the wp_get_canonical_url function to retrieve the canonical URL and output it within a custom template:

$canonical_url = wp_get_canonical_url();
if ($canonical_url) {
 echo '<link rel="canonical" href="' . esc_url($canonical_url) . '" />';
}

This code snippet first retrieves the canonical URL for the current page and then outputs it as a link tag with the “canonical” relationship attribute within a custom template. The esc_url function is used to sanitize the URL before outputting it.

Conclusion

In conclusion, the wp_get_canonical_url function is a valuable tool for WordPress developers and website administrators. By providing a standardized way to retrieve the canonical URL for a given post or page, this function helps ensure proper SEO and content management practices. Its flexibility and ease of use make it a powerful addition to the WordPress toolbox, allowing for more efficient and accurate management of canonical URLs. Incorporating wp_get_canonical_url into your development workflow can lead to improved search engine rankings and a better overall user experience for your website visitors.

Related WordPress Functions