Archive for the ‘HOWTOs’ Category

Series of posts, related posts & popular posts

Monday, June 2nd, 2008

One of the disadvantages of a chronologically organised blog is that older posts tend to get burried under newer ones. A shame when your posts do not have a limited shelf life. A similar problem occurs when you write posts that belong to a series of directly related posts, it is difficult for the reader to keep track of all parts and read them in the correct order.

In this article I will look at a few plugins that deal with these issues.

Series of posts

Travis Snoozy wrote the one of a kind plugin In Series, an invaluable tool for stringing together posts in a series. You can easily add, re-order and remove posts from a series from the Write/Edit Post page. It will automatically build a table of contents, previous/next links etc. This does not require you to hack your theme, it works right away. You can of course customize how series information is displayed.

This plugin is a must-have for me. Thanks to this plugin, whenever you add a new post to a series you won’t have to go through the hassle of editing every single previous post in the series and add a link to the latest addition, the plugin takes care of all that in a very elegant way. A huge time saver.

Related posts

There are many plugins available that can generate a list of posts related to the one being viewed, one of those tasks that is hard to keep up-to-date manually. Unfortunately most are out-of-date or have limited functionality, i.e. merely tag matching.

The best plugin I found so far, and one that works with WordPress 2.5.1, is Similar Posts by Rob Marsh. Similarity is judged according to a post’s title, content, and tags and you can adjust the balance of factors to fit your needs. If you like, you can even exclude specific posts or change a pletoria of other options. Pretty need!

This plugin requires an additional library plugin shared by the author’s various plugins and a simple inclusion in your single.php.

Popular posts

Knowing which posts are the most popular of your website can give you great insight in what your visitors are interested in. Perhaps you can write additional posts, provide even more specific information. Or maybe you simply want to publish a list of popular posts. A very popular plugin that can help you with that is Popularity Contest by Alex King. It keeps a count of your post, category and archive views, comments, trackbacks, etc. and uses a balanced formula to calculate a popularity score for each post. Each view has a different weight attached, e.g. clicking a post’s permalink is weighted higher in the overall popularity score than merely viewing the post as shown on the homepage. You can adjust the weights according to your preferences.

The bad news is, the current version of Popularity Contest does not work with WordPress 2.5.1. The good news is, with a few pointers by Ken McGuire, I have patched the plugin to work properly without any further manual action required.

Popularity Contest plugin (patched for WordPress 2.5.1)

Disable RSS feeds for your website

Sunday, June 1st, 2008

WordPress comes out of the box with RSS functionality. Great if you need it, but it lacks an easy off-switch if you don’t. A rare oversight, let’s fix it!

Disable feed creation

WordPress actually supports a number of different feed layouts, controlled by the actions do_feed_rdf, do_feed_rss, do_feed_rss2 & do_feed_atom. These actions can be removed, the easiest way to do this is by adding the following lines to the functions.php file of your theme. It can also be done by writing a new plugin, my preferred method because if I switch to another theme I might forget to move over these changes.

1
2
3
4
remove_action('do_feed_rdf', 'do_feed_rdf', 10, 1);
remove_action('do_feed_rss', 'do_feed_rss', 10, 1);
remove_action('do_feed_rss2', 'do_feed_rss2', 10, 1);
remove_action('do_feed_atom', 'do_feed_atom', 10, 1);

This disables WordPress’s ability to fill the feeds with posts when requested: feeds will be empty and/or return an error.

Disable feed access

We can improve on this slightly by making an optional change in wp-blog-header.php. It is up to you whether you do this or not: it is a core file, so whenever you upgrade WordPress you will have to redo this.

In wp-blog-header.php find:

1
wp();

Add the following lines below:

1
2
3
if (is_feed()) {
   die();
}

Now whenever someone tries to access your feed directly, nothing happens. No error, no nothing. Nothing to see here, move along…

Disable feed notification

Modern browsers like FireFox & Internet Explorer 7 display the feed notification icon in the address bar when a feed was found for the website you are viewing:

It does not make sense to notify visitors of a non-existing feed.

In the header.php file that belongs to your current theme, find and remove the following line:

1
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />

Depending on the theme you are using, there might be additional links to the feed build in. If you use the standard WordPress theme, you should edit the footer.php file.

That’s it, RSS feeds have now been completely disabled on your website. Make sure your clear your browser cache or it might not appear to work.

Downloads

For your convenience, here is a very simply plugin that will disable feed creation. Just put it in your wp-content/plugins directory and active the plugin.

Feed Disabler Plugin

Hacking the WP-Syntax plugin

Sunday, May 18th, 2008

Table of contents for Syntax Highlighting

  1. Syntax highlighting plugins
  2. Hacking the WP-Syntax plugin

In my previous article I picked WP-Syntax as my plugin of choice for syntax highlighting. At the time of writing I was using version 0.7. The plugin is not perfect: there are a few issues that need to be addressed. I will do so in this article.

Using the visual editor messes up your code snippets

In order to use WP-Syntax, or any syntax highlighting plugins for that matter, you will have to give up using the visual editor. Which is a shame, because that happens to be one of WordPress’s strong suit. Fortunately, Shantanu Goel devised a quick fix which I have adopted to hack WP-Syntax.

In wp-syntax.php, find:

1
2
3
4
5
6
7
function wp_syntax_code_trim($code)
{
    // special ltrim b/c leading whitespace matters on 1st line of content
    $code = preg_replace("/^\s*\n/siU", "", $code);
    $code = rtrim($code);
    return $code;
}

Replace with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function wp_syntax_code_trim($code)
{
    // special ltrim b/c leading whitespace matters on 1st line of content
    $code = preg_replace("/^\s*\n/siU", "", $code);
    $code = rtrim($code);
 
    $code = strip_tags($code);
    if (PHP_VERSION > 5.0) {
    	$code = html_entity_decode($code, ENT_QUOTES, "UTF-8");
    } else {
      $arrSearch = array("<", ">", " ", "&");
      $arrReplace = array("<", ">", " ", "&");
      $code = str_replace($arrSearch, $arrReplace, $code);
    }
 
    return $code;
}

You can now continue to use the visual editor. Simply add a <pre> block with the lang (and possibly class) attribute and paste the code in the visual editor. WordPress will no longer mess up your code.

The line attribute for line numbering is invalid (X)HTML

This might not seem like a big deal, because it is only used as an instruction for WP-Syntax, it does not end up in the final output. Unfortunately, now that we have hacked WP-Syntax to work in the visual editor, WordPress will add its two cents: it will strip the line attribute automatically to make the code XHTML valid. Fortunately, this is easily fixed.

If you recall, the Highlight Source Pro plugin abuses the class attribute for the same purpose. This is valid XHTML, so WordPress will allow it. We can hack WP-Syntax and swap out line for class in much the same way.

In wp-syntax.php, find:

1
2
3
4
5
6
7
8
function wp_syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|line=[\"'](\d*)[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute",
        $content
    );
}

Change this to:

1
2
3
4
5
6
7
8
function wp_syntax_before_filter($content)
{
    return preg_replace_callback(
        "/\s*<pre(?:lang=[\"']([\w-]*)[\"']|class=[\"'](\d*)[\"']|\s)+>(.*)<\/pre>\s*/siU",
        "wp_syntax_substitute",
        $content
    );
}

You can now use the class attribute to specify the first line number.

Changing the highlighting colors

WP-Syntax uses the default GeSHi colors for its syntax highlighting. I don’t particularly like them, let’s see if we can do better. Of course, if you don’t like my colors either, feel free to change them!

In wp-syntax.php, find:

1
add_action('wp_head', 'wp_syntax_head');

Add this line:

1
add_action('wp_syntax_init_geshi', 'my_custom_geshi_styles');

Now find:

1
2
3
4
5
6
7
8
9
function wp_syntax_head()
{
  $css_url = get_bloginfo("wpurl") . "/wp-content/plugins/wp-syntax/wp-syntax.css";
  if (file_exists(TEMPLATEPATH . "/wp-syntax.css"))
  {
    $css_url = get_bloginfo("template_url") . "/wp-syntax.css";
  }
  echo "\n".'<link rel="stylesheet" href="' . $css_url . '" type="text/css" media="screen" />'."\n";
}

Add these lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function my_custom_geshi_styles(&$geshi) {
   $geshi->set_keyword_group_style(1, 'color: #0000FF;', true);
   $geshi->set_keyword_group_style(2, 'color: #0000FF;', true);
   $geshi->set_keyword_group_style(3, 'color: #0000FF;', true);
   $geshi->set_keyword_group_style(4, 'color: #0000FF;', true);
 
   $geshi->set_symbols_style('color: #000000;', true);   
 
   $geshi->set_strings_style('color: #808080;', true);
   $geshi->set_numbers_style('color: #808080;', true);
   $geshi->set_regexps_style('color: #808080;', true);
   $geshi->set_escape_characters_style('color: #808080;', true);
 
   $geshi->set_comments_style(1, 'color: #008000;', true);
   $geshi->set_comments_style("MULTI", 'color: #008000;', true);
}

We have now used WP-Syntax’s custom filter to overwrite GeSHi’s default colors. You might want to take a look at the documentation if you are unsure what the various GeSHi’s functions do. You can also take a look at the code comments in the geshi.php file. It also helps to look at the specific language file, for example php.php. You can find it in the geshi subdirectory of the geshi subdirectory (not a typo) in the wp-syntax directory.

Downloads

For your convenience, here is the modified file. Please note that if you are using a different version of the plugin you will need to hack it yourself, do not replace it with this file!

WP-Syntax plugin