# Canonical Link to the Original Source for Republished Posts

> Points the canonical tag of a post to the URL in its "original_url" custom field, for articles first published elsewhere, so the original keeps the search credit.

- Type: PHP
- Runs: Frontend only
- Category: [SEO](https://wpide.com/library/category/seo/)
- Tags: canonical, syndication, custom-fields
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/seo-canonical-original-source/

## Code

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

add_filter( 'get_canonical_url', function ( $url, $post ) use ( $wpide_original_field ) {
    $source = esc_url_raw( (string) get_post_meta( $post->ID, $wpide_original_field, true ) );

    return wp_http_validate_url( $source ) ? $source : $url;
}, 10, 2 );
```

## Notes

Works with WordPress's own canonical tag. SEO plugins replace it, so use their canonical setting instead when one is active.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Canonical Link to the Original Source for Republished Posts" 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/
