User login

Set a separate browser window page title from HTML page title (Drupal 7)

This code shows how to set the browser bar page title the Page Title module way, without that module. (This was motivated simply because we were already gathering the info for another purpose in hook_node_view_alter(); there's no reason not to use Page Title!)

<?php
/**
* Implements hook_node_view_alter().
*/
function cultura_archive_node_view_alter(&$build) {
  if (
$build['#bundle'] == CULTURA_DISCUSSION_NODE_TYPE && $build['#view_mode'] == 'full') {
   
// Function in which we're already looking up all the relevant info...
   
$title = 'A string set from info gathered above';
   
cultura_archive_page_title($title);
  }
}

/**
* Implement hook_preprocess_html().
*/
function cultura_archive_preprocess_html(&$vars) {
  if (
cultura_archive_page_title()) {
   
$vars['head_title'] = cultura_archive_page_title();
  }
}

/**
* Simple setter and getter for page title within a single page load.
*/
function cultura_archive_page_title($new_title = '') {
  static
$title;
  if (
$new_title) {
   
$title = $new_title;
  }
  return
$title;
}
?>

Code in use and in context can be seen in Cultura Archive module. If you want to do anything more than a one-off change, you should just use the whole Page Title module.

Searched words: 
set browser bar title without affecting page H1 title change HTML header title not in-page title in Drupal 7 drupal_set_title not working on node drupal set only the page title

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.