I wanted filepaths and filenames in WordPress post and page content to be displayed with a monospace font, so I wrote the following PHP plugin to wrap filepaths with a <span> HTML and CSS style tag. The code could be added to a theme’s functions.php file, or used as a stand-alone plugin.
The span-filepaths.php script wraps filepaths and filenames with a CSS ID of “span-fp”, and variables / perl module names with a CSS ID of “span-ftp-var”. Here’s an example of a CSS stylesheet:
|
1 2 3 4 5 6 7 8 9 10 |
#span-fp, #span-fp-var { font-family:'Vollkorn', Geneva, sans-serif; font-size:1.1em; letter-spacing:0.5px; font-style:normal; } #span-fp-var { color:#3c3f07; } |
The script is built around a preg_replace() function (see the preg_replace manual), which allows arrays as arguments for the pattern and replacement strings. When using arrays with preg_replace(), it’s important to ksort() them before-hand (see the ksort manual) so each array has the same order.
The content parsed by the span-filepaths.php script is split into lines to check for comment, script, and pre html tags. The text within those blocks is left as-is to avoid, in the case of javascript for example, breaking code that must be executed by the browser.
I will attempt to document each regular expression within the code. If you have any questions about the regular expressions or the script, feel free to post a comment bellow.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<?php /* Plugin Name: Span Filepaths Description: Searches for filepaths within content, and wraps them with a span style tag. Author: Jean-Sebastien Morisset */ add_filter( 'the_content', 'span_filepaths' ); add_filter( 'comment_text', 'span_filepaths' ); function span_filepaths( $content ) { // split content to exclude comments and javascript sections foreach ( preg_split( '/((\r?\n)|(\r\n?))/', $content) as $line) { if ( preg_match( '/<(!--|script|pre)/i', $line ) ) $in_code = 1; if ( preg_match( '/(--|\/script|\/pre)>/i', $line ) ) $in_code = 0; if ( ! $in_code ) { $pattern = array( /* Look for filepaths and filenames: Prefix: Start of line, or any single character matching greater-than (the end of an html tag), space, or open-bracket. Filepath: Zero or one occurrence of a tilde, dot, or double-dot, which could be part of a filepath, followed by a slash, and one or more characters that are allowed in filepath names (alpha-numeric, underscore, hyphen, period, wildcard, and slash). Filename: One or more characters allowed in a filename (alpha-numeric, underscore, hyphen, period, and slash), followed by a dot, and 3-4 characters allowed in a filename extension (alpha-numeric and underscore). Suffix: Any single character matching a close-bracket, dot, comma, semi-colon, exclamation mark, interrogation mark, return, or new-line. --Prefix--- --Filepath---------------------- --Filename---------------------- --Suffix--------- */ 'http://cdn4.static.surniaulula.com/(^|[>\s\(])((~|\.|\.\.)?\/[a-z0-9_\-\.\*\/]+|[a-z0-9_\-\.\/]+\.[a-z0-9_]{2,4})([\)\.,;!\?<\s\n\r])/i', /* Look for variables and perl module names: --Prefix--- --Variable--------- --Module--------------- --Suffix--------- */ 'http://cdn5.static.surniaulula.com/(^|[>\s\(])([\$\@\%][a-z][a-z0-9_:]+|[a-z0-9_]+::[a-z0-9_:]+)([\)\.,;!<\s\n\r])/i', ); $replace = array( '$1<span id="span-fp">$2</span>$4', '$1<span id="span-fp-var">$2</span>$3', ); ksort($pattern); ksort($replace); $line = preg_replace( $pattern, $replace, $line); } $new_content .= $line."\n"; } return $new_content; } ?> |
Download the span-filepaths.php script.
If you want to use span-filepaths.php as a WordPress plugin, you’ll need to create a ./wordpress/wp-content/plugins/span-filepaths/ folder, save the span-filepaths.php script in this folder, add a readme.txt file to describe the plugin, and then activate it from the WordPress plugins page.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
=== Span Filepaths === Contributors: jsmoriss Tags: post, page, content, replace, filepath, filename, variable Requires at least: 3.0 Tested up to: 3.4.2 Stable tag: 1.0 License: GPLv2 or later Searches for filepaths within content, and wraps them with a span style tag. == Description == Searches for filepaths within content, and wraps them with a span style tag. == Installation == *Using the WordPress Dashboard* 1. Login to your weblog 1. Go to Plugins 1. Select Add New 1. Search for *NextGEN Facebook* 1. Select Install 1. Select Install Now 1. Select Activate Plugin *Manual* 1. Download and unzip the plugin 1. Upload the entire span-filepaths/ folder to the /wp-content/plugins/ directory 1. Activate the plugin through the Plugins menu in WordPress == Frequently Asked Questions == == Changelog == = v1.0 = * Initial release. |
Did you find this post useful? Share it with your circles / friends, or leave a quick note bellow.
Thank you,
js.
