4

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.

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?

2
  • I am not a developer but needed to add code to the <head> area to embed Google posts into individual WordPress posts (not pages). On a Google post, the embed instructions include "add this part to the <head> section, add this part where you want the post to appear in your blog" (not exact words.) I had no idea what the Head section is, since I am a regular Joe WordPress user and not a coder. But a search of plugins brought this result: wordpress.org/plugins/per-page-add-to Hope that helps.
    – user54703
    Commented Jun 25, 2014 at 0:33
  • 1
    This is what you need a perfect plugin wordpress.org/plugins/header-and-footer-scripts
    – Niresh
    Commented Oct 17, 2015 at 3:48

5 Answers 5

5

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.

4

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.

1

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.

0

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

0

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.

6
  • Doos this go in functions.php? I added it to the top and got a error: Parse error: syntax error, unexpected '<' in /xxxxxxxxx/functions.php on line 2
    – moobot
    Commented Aug 22, 2013 at 9:58
  • edited and now working, add this and dot1's code in functions.php
    – Anjum
    Commented Aug 22, 2013 at 15:48
  • Hmm, im getting a parse error when ever I add either yours or Dots code to my functions.php
    – moobot
    Commented Aug 27, 2013 at 7:23
  • @moobot oh parse error on which line number? and you need to add both codes mine and Dot's
    – Anjum
    Commented Aug 27, 2013 at 8:31
  • @moobot have edited and combined the codes, copy and paste above code into your theme's functions.php and tell me back.
    – Anjum
    Commented Aug 27, 2013 at 8:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.