Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

6 Answers 6

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
    }//end of if
  }//end of if
 }//end of function

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

share|improve this answer

Use below code to add your code in to <head> section.

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

For more information, you can visit here http://codex.wordpress.org/Plugin_API/Action_Reference/wp_head

share|improve this answer

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer
    
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 Aug 22 '13 at 9:58
    
edited and now working, add this and dot1's code in functions.php –  Anjum Aug 22 '13 at 15:48
    
Hmm, im getting a parse error when ever I add either yours or Dots code to my functions.php –  moobot Aug 27 '13 at 7:23
    
@moobot oh parse error on which line number? and you need to add both codes mine and Dot's –  Anjum Aug 27 '13 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 Aug 27 '13 at 8:41

I am not a developer but needed to add code to the area to embed Google posts into individual WordPress posts (not pages). On a Google post, the embed instructions include "add this part to the 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:

http://wordpress.org/plugins/per-page-add-to/

Hope that helps.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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