Hard code a domain into Yoast SEO canonical URLs

Below is a small plugin using which you should be able to replace the domain for canonical url to another domain.

<?php
/*
 * Plugin Name: WPSE WPSEO Canonical
 * Plugin URI: http://wordpress.stackexchange.com
 * Description: Changes canonical url domain.
 * Author: Sisir
 * Version: 1.0
 * Author URI: http://developerpage.net
 *
 **/

add_filter('wpseo_canonical', 'swpseo_canonical_domain_replace');
function swpseo_canonical_domain_replace($url){

    $domain = 'google.com'; // this can be loaded from option table if you want admin to set it.
    $parsed = parse_url(home_url());
    $current_site_domain = $parsed['host'];
    return str_replace($current_site_domain, $domain, $url);

}

Note: Code Not tested. Let me know if it doesn’t work.