Source Code Snippets
Putting something as simple as PHP source code into a post is a bit of a major project in WordPress. While there is a manual way of doing this. The easy way is to use what is called a syntax highlighter plugin. John H. Gohde has settled upon using Dean’s Code Highlighter on his blog.
John has reviewed 3 or 4 different syntax highlighters and settled upon Dean’s Code Highlighter for offering more of what he wanted in a source code lister.
Dean’s Code Highlighter
All so typically, this plugin offers next to no instruction on how to use it.
First, of all, you have to disable the WYSIWYG Visual Editor before using this plugin. The visual editor in WordPress will actually destroy your source code. Disabling the visual editor is how most of these source code highlighter plugins work.
After installing the plugin, you have to check out the Code Highlighter options in the WordPress Settings Tab. Basically, you have to decide if you want all of your source code listings to be with or without line numbers. This is a major weakness of Dean’s plugin, as some of the others let you make that choice, code snippet by code snippet.The default option is to show numbers, which on the theme being used by this blog at least does not display properly.
With this plugin, you simply enclose your code snippet with the standard <pre>tags, as follows.
Source Code
</pre >
A typical listing for PHP code is rather colorful due to this plugin’s use of GeSHi – Generic Syntax Highlighter. What Dean has done is basically provide a front-end interface to all of the real work that is being done by GeSHi.
The plugin works with many different programming languages, such as css, javascript, perl, php, and html4strict.
<?php _e(‘Home’); ?>
<?php } elseif (is_page()) { ?>
<?php _e(‘Page’); ?>
<?php } elseif (is_single()) { ?>
<?php _e(‘Post’); ?>
<?php } else { ?>
<?php _e(‘Bye’); ?>
<?php } ?>
Dean’s plugin offers no flexibility to the formatting of the required <pre> tags. Leave a trailing space and your code listing will go blank. Add an extra parameter, such as “1″, after the lang parameter and the colorful colors will disappear.
<?php if (is_home()) { ?>
<?php _e(’Home’); ?>
<?php } elseif (is_page()) { ?>
<?php _e(’Page’); ?>
<?php } elseif (is_single()) { ?>
<?php _e(’Post’); ?>
<?php } else { ?>
<?php _e(’Bye’); ?>
<?php } ?>
John H. Gohde liked Dean's Code Highlighter because it avoided the use of a horizontal scroll box since the plugin offers automatic text wrapping. In addition, most of the other syntax highlighter plugins did not display properly on John's custom theme.