For some bizarre reason, Drupal 7’s out-of-the box search results teasers do not strip
<span> tags if they exist in a custom field.
Let’s fix this.
1. Create a search results template file in your theme.
Copy the contents of Drupal’s base
search-result.tpl.php out of
/modules/search/search-result.tpl.php and into a new file located at
The comments in this file show us what variables we have access to. The
$snippet variable contains the text that we want to scrub.
2. Edit the $snippet variable by preprocessing it.
Preprocessing is just Drupal’s way of moving heavier template logic out of template files, into one giant dump of functions. Preprocessor functions are all held in your theme’s
template.php file. In this case we want a preprocessor function to give us a clean
$snippets variable without HTML elements.
So, at the bottom of your template.php file, let’s add a search preprocessor:
Flush your theme cache once you’ve made this update.
Notice that I’m chaining two php functions together to accomplish this.
html_entity_decode followed by
At last, when you render
$snippet in your theme’s
search-result.tpl.php, the HTML markup will be gone. Hope this saves someone a headache.