apply_filters( 'get_canonical_url', string $canonical_url, WP_Post $post )

Filters the canonical URL for a post.


$canonical_url string
The post's canonical URL.
$post WP_Post
Post object.

Top ↑


File: wp-includes/link-template.php. View all references

return apply_filters( 'get_canonical_url', $canonical_url, $post );

Top ↑


Version Description
4.6.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by xandercalvert

    An example of using this filter. In this I am setting the canonical url through ACF in a field called ‘canonical_url’.

    function wpdocs_edit_canonical_urls( $original_url, $post ) {
    	$override_url = get_post_meta( $post->ID, 'canonical_url', true );
    	if ( empty( $override_url ) ) {
    		return $original_url;
    	return $override_url;
    add_filter( 'get_canonical_url', 'wpdocs_edit_canonical_urls', 10, 2 );

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