Is there any way to allow html in the title field of a node? I realize if I want ALL titles to appear a certain way, I can do that with CSS but what I want to be able to do is italicize JUST one or two words within a title. Basically, the titles of some posts are the proper names of events and my client wants part of the titles to be italicized. Any ideas?

Comments

vm’s picture

for SEO purposes, im not sure HTML tags should be inserted into a node title. (someone can of course correct me if I am wrong.)

miana_ed’s picture

Italicizing text in a regualar html document would not effect SEO. I've never done it, but I don't see any reason why you couldn't with Drupal. I don't know if it will let you though.

gnotorious’s picture

Yeah, apparently it does not. Wondering if anyone has a workaround. Thanks for your responses so far.

vm’s picture

It isn't a standard HTML document though, the title field becomes the title of the page. thus html tags would show to bots no ?

if i title a page <i>home</i> what will strip those html tags when goggle spiders the title for inclusion into search results ?

Drupal doesn't allow this as default , and I was wondering if this is the reason. Especially before user changes the behavior in the manner suggested.

sampelo’s picture

I have not found a workaround

styro’s picture

Basically $title is a phptemplate variable, and these variables can be overridden:
http://drupal.org/node/16383
by changing _phptemplate_variables() in your themes template.php file.

Looking at...
http://api.drupal.org/api/5/function/phptemplate_page
...it looks like it is defined by...
http://api.drupal.org/api/5/function/drupal_get_title

You could write your own copy of the drupal_get_title() function and use it to override $title in your templates by changing the check_plain() function to check_markup() or similar.

I wouldn't recommend removing checks altogether though - you could open up yourself to XSS attacks.

Sorry - I don't have time to show example code at the moment, but i-ligns technology page is an example of where I used a similar technique to slip some markup and section name into the title.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ | Project management software knowledge base

styro’s picture

In your template.php:

<?php
function  _phptemplate_variables($hook, $vars) {
   switch($hook) {
     case 'node' :
     case 'page' :
        // for page and node templates, redefine the title variable
        $vars['title'] = mythemename_custom_title();
        break;
   }
   return $vars;
}

function mythemename_custom_title() {
  // customised version of drupal_get_title() that allows some filtered markup
  // uses check_markup() instead of check_plain()
  $title = drupal_set_title();
  if (!isset($title) && function_exists('menu_get_active_title')) {
    $title = check_markup(menu_get_active_title());
  }
  return $title; 
}

?>

I suspect that will work :)

Note: if your template.php already has a _phptemplate_variables() function, you will need to adapt the above code to it rather than just replacing it.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ | Project management software knowledge base

sampelo’s picture

you can put a switch statement with nid's in template.php

I don't know how that compares with the other approaches here.

markhope’s picture

what about this example?
http://drupal.org/node/28537

I've used it successfully in 4.7

Mark

Z2222’s picture

It also works well on Drupal 5.2. Should work on any PHPtemplate site. I think it's the best option because it doesn't allow HTML directly, but instead uses BBcode.

designguru’s picture

Hey all,

I just posted to my blog with a simple method for enabling custom HTML titles on nodes: check it out here:
http://designguru.org/blog/031110/how-use-html-alternative-titles-drupal...

Cheers,

Qasim

Qasim Virjee
Principal, http://designguru.org
qasim@designguru.org
1.416.777-1864