Skip to main content

As you said per page/post basis, this would work for each post

add_action('wp_head', 'add_link_in_head');
function add_link_in_head()
{
    global $post;
    if(!empty($post))
    {
        $alternate = get_post_meta($post->ID, 'alternate', true);
        $hreflang = get_post_meta($post->ID, 'hreflang', true);
        if(!empty($alternate) && !empty($hreflang))
        {
            ?>
                <link rel="alternate" href="<?php echo $alternate; ?>" hreflang="<?php echo $hreflang; ?>" />
            <?php
        }
    }
}

If there is no $alternate and $hreflang then there would be no link.

Kumar
  • 2.9k
  • 18
  • 19