Code Filter

Last updated on
30 April 2025

The Code Filter module allows users to post and display code using <code></code> and <?php ?> tags without having to worry about manually escaping < and > characters with &lt; and &gt;. The Code Filter is enabled on Drupal.org for input formats Filtered HTML, Full HTML, and Documentation.

Examples

Type in the following:
<code><h2>Example HTML heading</h2></code>
Code Filter displays:
<h2>Example HTML heading</h2>
(rather than turning Example HTML heading into an actual H2 heading)

Type in the following:
<code><?php print 'Example PHP code.'; ?></code>
Code Filter displays:
<?php print 'Example PHP code.'; ?>

Type in the following:
<?php print 'Example PHP code.'; ?>
Code Filter displays:
print 'Example PHP code.';

Installation

  1. Install as you would normally install a contributed Drupal module
  2. Go to the Modules administration page and enable the Code Filter module
  3. Go to the Text Formats administration page. Click 'configure' for the format you wish to enable codefilter in, and enable the Code Filter checkbox on the next page.
  4. Go to the input format's ordering page and rearrange the filtering chain to resolve any conflicts. For example, to prevent invalid XHTML in the form of '<p><div class="codefilter">' make sure "Code filter" comes before the "Line break converter."
  5. (optional) Edit your theme to provide a div.codeblock style for blocks of code.

Help improve this page

Page status: Not set

You can: