March 2009

Syntax highlighting code snippets in WordPress

March 6th, 2009

My last post included some code snippets.

I’ve never found an altogether satisfactory way to include nicely formatted and syntax highlighted code in WordPress. I’ve tried various methods but always ended up settling on something so-so after spending a while on it and eventually deciding I was wasting my time.

But this time round I tried again and found Chris Cagle’s Definitive Guide on WordPress Syntax Highligher Plugins.

In the spirit of “if  it’s good enough for him, it’s good enough for me” I decided to use the same plugin Chris settled on: Chili. You can see the results for yourself. However, although I’m fairly pleased with the end product, the process for getting there (using Live Writer) isn’t ideal. I’m documenting it here because, fortunately, I don’t often use code snippets, so this will serve to remind me how I did it:

  1. Copy the code you want to use.
  2. Paste it into Postable (an online processor for making code browser friendly):
    http://www.elliotswan.com/postable/
  3. Click make it friendly.
  4. Copy the browser-friendly code.
  5. In Live Writer, go to Source view.
  6. Create nested elements as follows:

    <p><pre><code class="html"></code></pre></p>

  7. Change "html" for "php" or "c#" or "mysql" or "javascript" or "css". There are a few other supported languages listed on the Chili site
  8. Paste the code into the middle, between the code tags. For example:

    <p><pre>
      <code class="html">&lt;p&gt;&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</code>
    </pre></p>

  9. Publish your post.

Leave a comment

 

Changing your WordPress RSS feed titles

March 6th, 2009    8 Comments

In my last post I mentioned that I'd been tweaking the feeds from my WordPress blog. Here's the detail (in case I need to redo this next time I upgrade WordPress).

The problem

The feed for the ITauthor Podcast (which is just a feed for a WordPress category called "Podcast") was coming out as ITauthor >> Podcast. It annoyed me that the podcast showed up in iTunes with those two greater than signs, or double angle brackets, chevrons, whatever you want to call them. In fact, although it looks like two characters it's actually the double right angle quote character:
»   which is typically written in HTML using the character code &raquo; and in XML using the code &#187;

My solution

The way I got rid of this may not be the easiest or best way. I search the WordPress Codex without finding out how to fix this, so in the end this is just the way I figured out how to do it. It may not be good. It may not be nice. It's certainly not pretty. All I'm saying is that it works for me.

So what I wanted to do was have a feed for my podcast category page that had the feed title "The ITauthor Podcast".

There were two tasks:

  1. Remove the angle quote completely in the cast of the feed for my "podcast" category page, but for other feeds change this character to a colon so that, for other categories, instead of "ITauthor >> Category", I'd have "ITauthor: Category".
  2. For the "podcast" category page, add "The" at the start of the feed title.


Here's what you do:

  1. Go into your wp-includes directory and take a backup copy of feed.php and feed-rss2.php.

    Now I've moved to Dreamhost, I have the luxury of an ssh connection and a command line, which makes this very quick and easy (provided you're happy working on the command line).
  2. Open the feed.php file (e.g. using vi or some other plain text editor).
  3. Change:

    function wp_title_rss($sep = '&#187;') {
            echo apply_filters('wp_title_rss', get_wp_title_rss($sep));
    }

    to:

    function wp_title_rss($sep = '&#187;') {
         if (in_category('26')) {
              echo ' ' . trim( get_wp_title_rss('') );
         } elseif ( is_archive() | is_category() | is_tag() | is_date() ) {
              echo ': ' . trim( get_wp_title_rss('') );
         }
    }

    What this does is as follows.

    For category 26 (which happens to be the number of my podcast category), it gets the category title. The empty string for an argument means that it prepends a space followed by nothing (because I don’t want a separator character), followed by a space. The second space is added because the function expects a separator character in the middle. The trim function trims away the two spaces, but we add back a single space (that's the first thing that gets echoed) otherwise the result would be "ITauthorPodcast". 

    For all other categories, and various other types of pages we do exactly the same, but prepend a colon followed by a space, rather than just a space. This gives us "ITauthor: Category".

    For anything else (e.g. the Home Page) this function does nothing, which results in a feed title of just "ITauthor".

  4. Save your changes to this file.
  5. Open feed-rss2.php.

    This is the file that creates the RSS2 feed (the default format for feeds). It's here that the category title (as generated by wp_title_rss() in the feed.php file) is appended to the blog title. So it's here we can prepend "The " to give us "The ITauthor Podcast.
  6. Insert an if clause into the part of the script that prints the title, so that if this is the feed for the podcast category (ID 26 on my system) it adds "The " in front of the title generated by bloginfo_rss('name') and wp_title_rss().
  7. Change:

    <title><?php bloginfo_rss('name'); wp_title_rss(); ?></title>

    to:

    <title><?php if (in_category('26')) echo 'The '; bloginfo_rss('name'); wp_title_rss(); ?></title>

  8. Save the file.

Leave a comment



Is WordPress the Venice of blogging software?

March 4th, 2009    1 Comment

wordpress-logo

All I wanted to do was change the title of one of the RSS feeds for this blog. Sounds simple enough and, to be honest it wasn’t rocket science, but it did involve digging around among the foundations of my WordPress blog. And this made me think: WordPress is a bit like Venice. What you see (gliding towards the Lido on a water taxi or arriving at the home page of a WordPress blog) is usually attractive. There are things that are not so great (crumbling masonry or a cumbersome list of categories in a side panel), but on the whole it’s a good experience.

However, WordPress, like Venice, is not built on strong foundations. It’s built on the software version of rotting wood.

WordPress is a great example of a software application that has grown organically and has ended up one great big mess. Granted it’s a mess that people (millions of them) manage to do wonderful things with. But if you sat down to design a highly configurable, mass market blogging application you would not build it like WordPress. For starters (sorry PHP lovers out there), you would not use PHP. WordPress’s collection of PHP on PHP on PHP, distributed through a collection of files that is only loosely architected (I’m trying to be kind here), is the equivalent of Venice’s wooden posts driven into the mud of a lagoon, long, long ago. It seemed like a good way to build a town at the time. It made perfect sense back then. But the years went by and Venice grew: outwards and upwards. And down the ages the architects of Venice have wished they had something better to support their beautiful, lovingly designed constructions than lots and lots of rotting posts.

But to answer my own question: Is WordPress the Venice of blogging software? No, it’s not. And the reason it’s not is that, unlike WordPress, Venice will outlive us all. It’s too important and we love it too much. The posts are being replaced. Huge amounts of effort and money have gone into solving the problem of sinking buildings. Venice’s future is secure (except maybe for the effects of climate change – but that’s another story).

WordPress on the other hand, I believe, has trouble ahead. It’s massively popular right now. But because of the mess at its core, there’s an opportunity for someone to come along with something that provides everything WordPress does but is much easier to use, is easier to configure into a wider variety of looks and styles, and is easier to create plugins for. And because all your data is in a MySQL database, it wouldn’t be difficult to make it very easy to migrate blogs over to a new system.

At the moment WordPress is king of the hill. I, like millions of others, enjoy using it from day to day to write up our thoughts (like these) and inflict them on the world (or some small part of it). Mind you, I think the things I like most about posting to my WordPress blog are mostly features of Microsoft’s Live Writer application, which I use for writing posts, rather than features of WordPress itself.

So this king better watch out. I wouldn’t be at all surprised if, before too long, WordPress ends up the ruler of a small and crumbling city state whose days of influence are long behind it.

Marc-Olivier Maheu (http://www.flickr.com/people/marcomaheu/)

Leave a comment



Inserting a feed into your WordPress blog

March 4th, 2009    1 Comment

Now that RSS feeds are all over the place, and we have our own personal feeds provided for us on all sort of social network sites, it’s nice to be able to able to drop an RSS feed into your blog as a list.

Bill Rawlinson’s feedList plugin for WordPress allows you to do precisely that.

As a simple example of what it can do. Here’s my personal Writer River feed and my twitter feed grabbed and stuck together on a blog page:

http://www.itauthor.com/syndicated-posts/

Leave a comment



JustGiving: making charity fund-raising easy

March 1st, 2009

My daughter Martha is going off to Guyana later this year to spend 12 months in a village teaching kids maths and science. Teachers are in short supply in Guyana, so without volunteers like those sent out there by Project Trust, kids simply wouldn’t get taught some subjects.

To go out there for a year, volunteers need to raise £4660 to cover travel, Project Trust’s running costs, etc. In order to make donating easy, and to provide a page of information to refer people to, Martha set up an account at JustGiving. It’s a really good use of the Web and I’d highly recommend it to anyone raising money for a charity.

Martha’s page is at http://www.justgiving.com/marthachristie but it expires in September, so here’s a screenshot of how it looks today:

Martha-Christie-JustGiving

Leave a comment



^ back to top ^

Page 3 of 41234