4

According to my client's requirement, I need to change the order of meta tags. The requirements are also to hide some of the meta tags.

The order requested is similar to the following one.

<Title>
<Description>
<keywords>

Is there a module to achieve this easily, do I need to edit the files of the Metatag module?

  • 1
    Could you tell WHY client wants that? Maybe there is a module satisfying his need, but if so, it can't be googled simply under "head reorder". – Mołot Jun 24 '13 at 7:13
  • 1
    Correct me if I'm wrong, but I just don't see the purpose of such re-ordering. If it's elements in the Body then it's for SEO purposes, but nonetheless an interesting question. If you could tell us "why" then it'd be even more interesting. – Beebee Jun 24 '13 at 8:38
  • Its purely for SEO purpose, he whats to remove them just to get site data up on the site, that would be easy for google to crawl. – user7048 Jun 24 '13 at 9:44
  • 1
    What source claims that the order of the meta tags is of relevance to SEO? While there's not reason to not answer the question itself, it's valid enough, the implementation here just seems like a big waste of time. As doing-what-the-client-said so often is. – Letharion Jun 24 '13 at 15:14
10

To alter the order of specific meta tags, a hook_html_head_alter() implementation in your theme's template.php file would be your best bet:

function MYTHEME_html_head_alter(&$head_elements) {
  // Combined with the devel module this line will tell you what elements you have to work with
  dpm($head_elements);

  // Any you can just alter the weight like this
  $head_elements['module:key']['#weight'] = -1000;
  $head_elements['another_module:another_key']['#weight'] = -999;
}
  • It's only valid for 7 and up. – Mołot Jun 24 '13 at 7:58
  • @Mołot Yep, as the OP requested... – Clive Jun 24 '13 at 7:59
  • 1
    My mistake - it was not tagged with 7 and I somehow missed it in title. Either way, your answer does not tell how to bump <title> to the top, so I'll leave mine in place. – Mołot Jun 24 '13 at 8:01
  • @Clive thanks for the answer, can you please elaborate how can i get "['module:key']". – user7048 Jun 24 '13 at 9:52
  • You need to inspect the $head_elements array to see what's in it...the dpm() method through the Devel module would be easiest as noted in the comments – Clive Jun 24 '13 at 9:54
6

HTML head elements are defined in 2 places.

First one, easiest to access, is your theme's html.tpl.php.

In Professional Theme relevant part looks like that:

<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<?php print $head; ?>
<title><?php print $head_title; ?></title>
<?php print $styles; ?>
<?php print $scripts; ?>
<!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
</head>

So, here you can put title before other headers. It's wise to leave CSS and JS parts at the end, in the order specified.

If you feel you need to reorder what's in $head variable,

it gets more tricky. This variable is generated by Drupal core, and then altered by modules and themes. Good thing, your theme will be the last accessing it.

For Drupal 6, in template.php define THEMENAME__preprocess_page(&$variables), and alter or reorder $variables['header'] as you please. Keep in mind that it may break other modules, especially if you will remove things. Reordering is relatively safe, adding is usually safe, removal is risky.

For Drupal 7 see answer by Clive. THEMENAME__preprocess_page(&$variables) would still work, but 7 provides more direct solution to your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy