Add code into <head> on a per page/post basis

Is there plugin (or an easyish way) to add code to the <head> section on a per page/post basis?

I have a multilingual website and I need to implement the rel=”alternate” hreflang markup, so need a way to define rel="alternate" individually on each page/post.

Read More

I have searched the WordPress plugins but haven’t found any thing thus far. I’m not really a coder so can’t create something myself, unless it was rather easy!

UPDATE

OK, I’m having a go myself trying to create a custom field for this functionality, this is what I have so far:

<link rel="alternate" href="<?php
    while (have_posts()) : the_post();
        $alternate = get_post_meta($post->ID, 'alternate', false);
        if ($alternate) {
            echo $alternate[0];
        }
    endwhile;
?>" hreflang="<?php
    while (have_posts()) : the_post();
        $hreflang = get_post_meta($post->ID, 'hreflang', false);
        if ($hreflang) {
            echo $hreflang[0];
        }
    endwhile;
?>" />

Now this works fine if I only want to add one alternate page. When I try to add any more, it will only add one instance of rel="alternate", with the last added replacing the original.

Also, on any pages that I haven’t defined any of these custom fields, I’m getting an empty tag:

<link rel="alternate" href="" hreflang="" />

Can someone please point me in the right direction to correct these issues?

Related posts

5 comments

  1. 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.

  2. Use the below code in the <head> section:

    <?php
    add_action('wp_head', 'your_function');
    function your_function() {    
        //your code will go here
    } 
    ?>
    

    For more information you can visit here.

  3. I don’t know how you are saving your data but assuming that you are using the built in “Custom Fields” meta box your multiple alternate pages should come through as an array.

    function alternate_pages_links() {  
      global $post;
      if (!is_singular()) {
        return;
      }
      $pat = '<link rel="alternate" href="%s" hreflang="%s" />';
    
      $lang = get_post_meta($post->ID, 'hreflang', true);
      $alt = get_post_meta($post->ID, 'alternate');
    
      if (!empty($alt) && !empty($lang)) {
        foreach ($alt as $a) {
          printf($pat,$a,$lang);
        }
      }
    }
    add_action('wp_head','alternate_pages_links');
    

    However, I see a potential issue. If you are to have multiple associated alternate/hreflang pairs per post, then you need to save them as pairs in the database. Unless you do, there will be no way to tell which hreflang goes with which alternate. You will need to construct your own meta box for that.

  4. If you are using Transposh (The plugin that I have developed) For managing your multilingual sites, the rel=alternate will be added automatically (with the option to disable it)

    However, the reason I am answering this, is that in my experience, the results of adding this tag have actually been abysmal, so I am not sure that adding it is helpful at all.

    Good luck

  5. sorry for this i have not 50 reputation to add a comment

    dot1‘s answer is perfect for include link alternate in head section get value from post meta but now you need to add custom meta keys for hreflang and alternate values for each page/post from post edit screen or create a meta box for to do this.

        // alternate link meta box hook into WordPress
    add_action( 'admin_init', 'add_my_link_metabox' );
    
    // Add meta box
    function add_my_link_metabox() {
        // for posts
        add_meta_box( 'link_meta_box', __( 'Alternate Link Attr', 'Your Text Domain' ), 'my_link_data_function', 'post', 'side', 'core' );
        // for pages
        add_meta_box( 'link_meta_box', __( 'Alternate Link Attr', 'Your Text Domain' ), 'my_link_data_function', 'page', 'side', 'core' );
    }
    
    
    // create a function to display metabox
    function my_link_data_function() {
       global $post, $post_id;
       $alternate = get_post_meta($post->ID, 'alternate', true);
       $hreflang = get_post_meta($post->ID, 'hreflang', true);
    
       // generate html for metabox ?>
       <div class="admin-inside-box">
            <p>
                <input type="hidden" name="my_meta_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>" />
            </p>
    
            <p>
                <label>Link alternate</label>
                <input type="text" class="widefat" name="alternate" value="<?php echo $alternate; ?>" />
            </p>
    
            <p>
                <label>Link hreflang</label>
                <input type="text" class="widefat" name="hreflang" value="<?php echo $hreflang; ?>" />
            </p>
       </div>
       <?php
    }
    
       // now save metabox data on save post
       // hook in save_post action
       add_action( 'save_post', 'save_my_link_data' );
    
      // create function to save values
      function save_my_link_data() {
         global $post, $post_id;
    
         // verify nonce
         if ( !wp_verify_nonce( $_POST['my_meta_nonce'], basename(__FILE__) ) ) {
             return $post_id;
         }
    
         // check if current user can edit post
         if ( !current_user_can( 'edit_post', $post_id ) ) {
             return $post_id;
         }
    
         // now save/update the values in post meta keys
         update_post_meta( $post_id, 'alternate', $_POST['alternate'] );
         update_post_meta( $post_id, 'hreflang', $_POST['hreflang'] );
      }
    
      // hook alternate link into wp_head
      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
           }
         }
      }
    

    hope this will help you, you may change function names etc.

Comments are closed.