Home ›
Set a separate browser window page title from HTML page title (Drupal 7)Set a separate browser window page title from HTML page title (Drupal 7)
Submitted by Benjamin Melançon on May 30, 2015 - 9:33pm
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