Syntax Highlighting in WordPress

After some research, I decided to use the iG:Syntax Hiliter v3.5 plugin in my WordPress 2.0.5.

Installation and use are fairly easy. I only adjusted syntax_hilite_css.css which ships with the plugin to get an output compatible with my site’s style.

Here’s the CSS code and itself a demo of the plugin:

[css]

.syntax_hilite, li .syntax_hilite {
padding:3px 3px 10px 8px; border:1px solid #aaaaaa; background-color:#FEFEFE;
font-family:’Courier New’,Courier,monospace;
/* comment the line below to remove scrolling in code boxes */
overflow:auto; white-space:nowrap;
}

/* change this line to set the width of code box */
.syntax_hilite {
width:100%;
}

/* change this line to set the width of code box in a list */
li .syntax_hilite {
width:100%;
}

/* text bar above code box */
.igBar {
width: 100%;
text-align:right;
text-decoration:none;
font-size:10px;
padding-top:1px;
margin:0px;
}

.igBar a {
text-decoration:none;
text-transform: lowercase;
border-left: 1px solid #aaaaaa;
border-top: 1px solid #aaaaaa;
border-right: 1px solid #aaaaaa;
padding:2px;
}

.syntax_hilite .langName {
color:#000000;
font-weight:bold;
}

.syntax_hilite textarea {
margin:0px -5px -2px 0px;
border:none;
}

[/css]

Read here about which other useful WordPress plugins I use.

Migrating Old Permalinks to WordPress

Now that I migrated to WordPress for blogging, the permalinks of my previous blog entries have changed.

For example:

/article_php_5_enterprise_edition_available_online

changed to:

/2006/11/26/article-php-5-enterprise-edition-available-online/

My initial idea was to use mod_rewrite by adding a RewriteRule for each old link redirecting to the new location, but that looked like a lot of copy&paste to me and would not teach me anything new.

Instead, I chose to redirect requests for old links to the WordPress search page. Try it out yourself: Click on this old permalink /article_php_5_enterprise_edition_available_online and you will be redirected to the search page, which displays the blog entry to you as a search result. This looks like a more flexible solution to me, just in case that my permalinks might change again one day.

The implementation was simple. I added a few lines of PHP code to the 404.php page of my WordPress theme as described in the support topic 404 Search Function for WordPress. I also added a header redirect directive.

This is the full code:

[php]

// Adjust if WordPress is located in subdirectory,
// e.g. http://www.example.com/weblog. Otherwise leave empty.
$blog_uri = ‘weblog’;

// Don’t change from here.
$search_url = $blog_uri.’/?s=’;
$search_term = urldecode( substr($_SERVER[‘REQUEST_URI’],1) );
$find = array(“‘”.$blog_uri.”‘”, “‘/'”, “‘[-/_]'”) ;
$replace = ” ” ;
$new_search = preg_replace($find, $replace, $search_term);
$new_search = urlencode($new_search);

// Redirect to search page.
header(“Location: /”.$search_url.$new_search.”&http_status=404″);
exit;
[/php]

Notice that for security reasons, I use urlencode() for the search page URL to avoid HTTP Response Splitting.

Useful Extensions for WordPress and MediaWiki

These are the WordPress and MediaWiki extension I use on my site:

WordPress Plugins

  • Ultimate Tag Warrior: Very nice and powerful plugin which lets me add tags to my blog entries and display the tag cloud in the sidebar. It also allows me to retrieve tag suggestions from Yahoo! for a post – easy to install and configure and very helpful!
  • WP-Leftycats: A mini patch which makes the feed icons display to the left side of a category name. I was just too lazy to hack it myself 🙂

MediaWiki Extensions

  • GoogleMaps: This extension allows me to show where I am located on the page with my contact details.
  • XFeed: Aggregates RSS feeds and displays them from within MediaWiki. Look at the startpage of my site to see it in action. There I show the 5 latest blog entries. I had to tweak the code a bit though, from $feedItems = array(); to $feed->items = array();. The author had it the right way initially, don’t know why he changed it.

My site with new design and software

I have made myself a little christmas present and redesigned my homepage on sandro.groganz.com. Furthermore, I have changed the software to WordPress and MediaWiki. Some long evenings and short nights have come to an end.

I am particularly happy about using MediaWiki now. Hey, weeee, I use the same software as Wikipedia does :). While transfering the contents from the old homepage to my new one into MediaWiki, I was quite suprised how well the default CSS formatting works and how we all got used to it by using Wikipedia. Hence, I did not change the formatting of contents. Actually, the same is true about the default CSS of WordPress, which is also widely used on the Web.

Big thanks go to Gassi of Netzwirt, who helped me very quickly by moving my domain to a PHP 5-enabled server. I am very happy with his hosting service for my site – no problems since 2,5 years.

Merry christmas to all of you!