# Point the Yoast Canonical to an Original Source

> Uses a URL saved in a custom field as the canonical link on posts republished from another site, overriding Yoast's default.

- Type: PHP
- Runs: Frontend only
- Category: [Plugin Integrations](https://wpide.com/library/category/integrations/)
- Tags: yoast, canonical, syndication
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/yoast-canonical-from-field/

## Code

```php
// Custom field holding the original URL.
$wpide_canonical_field = 'original_url';

add_filter( 'wpseo_canonical', function ( $canonical ) use ( $wpide_canonical_field ) {
    if ( ! is_singular() ) {
        return $canonical;
    }

    $source = esc_url_raw( (string) get_post_meta( get_queried_object_id(), $wpide_canonical_field, true ) );

    return wp_http_validate_url( $source ) ? $source : $canonical;
} );
```

## Notes

Add a custom field named original_url with the full source URL to a post. Yoast Premium's canonical field also works per post.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Point the Yoast Canonical to an Original Source" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
