I would not use that code.
The function rel_canonical() calls wp_get_canonical_url(). The value returned from this function is filterable. I would use this hook and create a filter to replace what WP is using. This is how the plugins you’ve mentioned to this.