Put Code Snippets in a WordPress Blog

I’ve installed WP-Syntax, a great plugin for WordPress that lets you add code snippets to your blog posts without losing all the formatting.

You can use all manner of languages and even include line numbers if you want. Here’s a snippet from one of my math worksheet makers written in Perl:

5
use List::Util shuffle;    # Use the Shuffle module
166
167
168
169
170
171
# define and populate the arrays
my @girlsnames = ('Erin','Anna','Joanne','Sally', 'Mary');    
my @boysnames = ('Adam','Ken','Tom','Paul','David', 'Sam'); 
 
@girlsnames = shuffle @girlsnames;    # shuffle the girls' array
@boysnames = shuffle @boysnames;    # shuffle the boys' array
377
378
$name1 = pop(@boysnames);    # assign last name in array to $name1;
$name2 = pop(@girlsnames);    # assign last name in array to $name2;
If you like, you can find me on Twitter at @nick_ramsay. I'd love to hear from you!

9 thoughts on “Put Code Snippets in a WordPress Blog

  1. I actually don’t really understand this post. 🙁 What’s a code snippet? Can you show an actual example of what this code really does?

    1. Stop pulling my leg, Mike. It’s quite obvious what a “code snippet” is, and I’ve commented the actual code so you know what it does. I’m not falling for your trickery! 😉

    1. A definition for you:

      A segment of a document. Typically, a snippet is a set of contiguous text about the size of a paragraph and is about a single topic.

      And another:

      “Snippet” is a programming term for a small region of re-usable source code or text.

  2. Love the photo, Nick. Rikuto looks so cute.I also like the diary updates. I can keep an eye on what you are doing. Ha ha!

    1. You mean the Twitter updates? If you join Twitter.com, you can get those sent directly to your mobile phone! Then you’ll know exactly what I’m doing that very minute! 😉

  3. I’m a little confused…

    What you are saying is this plugin displays code snippets as text in a post, right?
    (Because normally any code would not show as text)
    This would be useful for showing others a bit of code used for x purpose.

    Or are you saying something else entirely?

Comments are closed.