Point the All in One SEO Canonical to an Original Source
Uses a URL stored in a custom field as the canonical link on republished posts instead of All in One SEO's default.
Opens WPIDE on
Code
// Custom field holding the original URL.
$wpide_aioseo_canonical_field = 'original_url';
add_filter( 'aioseo_canonical_url', function ( $url ) use ( $wpide_aioseo_canonical_field ) {
if ( ! is_singular() ) {
return $url;
}
$source = esc_url_raw( (string) get_post_meta( get_queried_object_id(), $wpide_aioseo_canonical_field, true ) );
return wp_http_validate_url( $source ) ? $source : $url;
} );
Notes
Add a custom field named original_url with the full source URL.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.