WordPress: A Tale of Two Tweaks

The more I play with WordPress, the more I like it. It’s usable straight out of the box and, if you like to fiddle around under the hood, there’s plenty of scope for everything from minor tweaks to complete overhauls. Here’s the much abbreviated story of two tweaks which have tickled me deep down in the parts my underclothes cover.

Migrating from Geocities

Moving to WordPress, I had to deal with how to redirect people from my Geocities web site. The problem is the GeoCities is not interested in letting me access anything interesting such as an .htaccess file, a server-side 301/302 redirection, etc and did not allow me to post perl or php code to do job. I had to fall  back to javascript to redirect a browser to here. The unwanted side-effect of this is that Geocities would wrap a very annoying frame around the redirected page.

Never fear! The WordPress add_action() function is here! A small bit of javascript hooked into wp_head did the trick. The add_action() function takes a pre-defined function and adds it into the hook specified – meaning that the function is added to the list of stuff to be processed when WordPress is deciding what to include in the HTML <head> tag it sends the browser. Here’s the code in question:

function kill_geocities_frames() {
   echo "<script type='text/javascript'>
      // <![CDATA[
      if (self != top && self.location.host != 'sleech.info' ) {
         top.location.href = self.location.href;
      } else {
         if ( document.getElementById('y_gc_div_adcntr') ) {
            y_gc_div_adcntr.innerHTML = '';
            y_gc_div_mast.innerHTML = '';
            y_gc_div_au1.innerHTML = '';
         }
      }
      // ]]>
   </script>";
}

add_action('wp_head', 'kill_geocities_frames');

Excerpts and Ellipsis

My preference has always been to have the front page of my blog include the full text of the latest post only and excerpts of all other posts. To me, this just looks better. The issue is that the theme I prefer did not support this. The code to do this is better explained pretty much everywhere so I’m not going to dig into the explain here.

The problem that concerns me now is how to make make the excerpt display in the way I want it to. This problem can be broken down into two goals to be achieved:

  1. I want the excerpt to show more than just plain text. I want it to show links, images, bold text, etc — all the standard HTML.
  2. I want to convert the standard ellipsis […] at the end of the excerpt to be a clickable link which takes the reader to the full version of the post.

There are numerous plugins which solve one of these problems but none that I’ve found that do both at the same time to the level of quality I want. None of the plugins I tried wanted to play nice together. I chose Advanced Excerpt to solve goal 1 and give me complete control over what HTML is displayed in the excerpt.

To solve goal 2, WordPress again came to the rescue in the form of add_filter(). This is a function through which you pass a bunch of text – such as the text of the excerpt – which is manipulated and changed according to a function you write for the purpose. In this case, I wrote a function which searches the text given it for the HTML ellipsis entity added to the end of the excerpt by the plugin and replaces it with the HTML code for the link I want. Add_filter() then hooks my function into the the_excerpt() template tag. Here’s the code:

function my_excerpt_ellipsis($text) {
   return str_replace('[&hellip;]', '&hellip; <a href="'.
         get_permalink($post->ID) . '" rel="nofollow" class="more">
         read on »</a>', $text);
}

add_filter('the_excerpt', 'my_excerpt_ellipsis');

Man, I love this stuff!