Posted on

Extending WordPress Functions

WordPress allows to extent functionality by using the
/wp-content/themes/THEME/functions.php.

I do not want to play around in the file directly and usually create a directory with all my own extensions, e.g. the directory include/ and there I place the code.

In the functions.php I just reference to my code parts:

/**
 * Custom functions
 */
require_once('include/functions-custom.php');

How to use the PRE Tag in WordPress?

Looks like WordPress likes to reformat even stuff within the PRE tags. Therefore the following functions come handy (just add them to functions.php):

function pre_content_filter( $content ) {
        return preg_replace_callback( '|(.*)

Within the code block you can remove the .*? part but otherwise pre is not displayed correctly ;)

Using a plugin to display code

Another option for displaying code is using a proper plugin. The first one I found which looks really useful is SyntaxHighlighter Evolved. Works smoothly and gives great output. Configuration parameters and usage is explained on the WordPress Plugin Settings for this plugin. See the example of the above code over here:

function pre_content_filter( $content ) {
        return preg_replace_callback( '|<pre.*>(.*)</p.*?re|isU' , 'convert_pre_entities', $content );
}

function convert_pre_entities( $matches ) {
        return str_replace( $matches[1], htmlentities( $matches[1] ), $matches[0] );
}

How to install a plugin in WordPress?

The syntax highlighter led me to the next questions how to install a plugin. I do not want to do the automatic upload so the manual process is like this.

1. Get the plugin as .zip and unzip it
2. Copy the whole content into /wp-content/plugins
3. Go to the Plugins > Installed Plugins page in WordPress
4. Click activate