Drupal 7: How to remove the pipe | in website title

In this tutorial, you will learn how to remove the pipe or the vertical bar ” | ” from the title of Drupal 7 powered websites that shows on browsers’ title bar. The process will consist of adding a preprocessor function and modifying php variables that relates to Drupal 7 site title.

In your template.php, insert the function below. This function will modify the title of your Drupal 7 site by removing the pipe and replacing it with hyphen ‘ – ‘. Note that you can use any symbols or texts you want to replace the vertical bar.  Make sure to replace ‘yourthemename’ with the name of your theme.

function yourthemename_preprocess_html(&$variables) {
  $variables['head_title'] = drupal_get_title() . " - "
     . variable_get('site_name');
}

The code will simply add a hyphen between a node title and site name. For example, if I have a node which has a title ‘How to remove pipe in Drupal 7 title‘ and site name called ‘Koolshiz‘, the site’s title will become ‘How to remove pipe in Drupal 7 title – Koolshiz‘ removing the vertical pipe from the browser’s title bar.

Removing node title based on node

The code above will work on all nodes including the front page. However, in my case, I do not want my static main front page to show its title on the browser. A quick solution is to add a simple if statement in your php code to not show titles on front page. For example, if the title of my static front page is ‘Main’, the code below will remove the word main from the title.

function yourthemename_preprocess_html(&$variables) {
  if (drupal_get_title() == "Main" ) {
    $variables['head_title'] = variable_get('site_name')
       . ' - ' . variable_get('site_slogan')
  } else {
    $variables['head_title'] = drupal_get_title() . " - "
       . variable_get('site_name');
  }
}

The php code above will check if the current page is ‘Main’ and if it is, it will display the site name and the site slogan. If not, the node title and the site name will be displayed.

Code Notes

  • drupal_get_title() gets the title of the current page.
  • variable_get(‘site_name’) gets the name of the site.
  • variable_get(‘site_slogan’) gets the slogan of the site.
About Editorial Staff

Editorial Staff at KoolShiz is a team of WordPress lovers led by RJ. Page is maintained by RJ