Showing Code on WordPress Posts with Snipplr!

Saturday, July 11th, 2009

If you haven't used Snipplr yet, or are unsure what Snipplr is, it is basically a code pasting/sharing site on the surface. I recently signed up for an account, after looking at a lot of different places to store small snippets (bigger projects get stored GitHub) and found Snipplr to be a really excellent application. Today, I'm going to go over how to display code on your WordPress site using a Snipplr plugin and a snippet of your choice, I'll also be talking about some of the cooler features of Snipplr.

A Quick Note

Before I continue, I want to note that I am in no way shape or form in business with or getting paid at all by snipplr or anyone else for this post. I just found Snipplr to be a cool application and decided to share some things with you all :) /end disclaimer

What does Snipplr do?

Snipplr Home Page Screenshot

A simple look at the about page will answer that question for you:

With Snipplr you can keep all of your frequently used code snippets in one place that's accessible from any computer. You can share your code with other visitors and use what they post, too. Did we mention that Snipplr works with TextMate? Yeah, we rock.

Notable Features

  • Tons of programming languages to choose from, all with slightly different syntax highlighting.
  • The ability to add the original URL you found the snippet (if one exsists), you can also add the original author and any comments about the source code. Giving credit where it's due FTW!
  • Ability to tag and favorite any snippet you wish for easy organization.
  • Features integrate into TextMate. Furthermore, there are quite a few applications Snipplr integrates with.
  • Ability to download a backup copy of all of your snippets. I thought this was an especially cool feature.
  • Add others to your 'watchlist', which is other peoples snippets that interest you (much like Twitter, if you find someones snippets to be awesome, you can follow and keep track of them). By the way, you guys can follow me on snipplr anytime :)
  • An API key and available API. In addition, a Snipplr WordPress plugin that we will be using later in this article

Hopefully, the above features will convince you to give Snipplr a look. Let's move on and get to displaying code snippets on our blog using the Snipplr plugin.

Download the plugin

Snipplr Plugin Page

The first thing you will need to do is download the WordPress Snipplr plugin, which will allow you to display any snippet you want, anywhere on your blog using WordPress short codes.

Grab your API key

Settings Page

Next, you will need to grab your API key on the settings page. You will need to register an account to receive an API key, which should take all of 3 seconds or so. Navigate to the bottom of the settings page and copy your API key to your clipboard.

Add a snippet

Add whatever code it is you are wanting to display on your blog by clicking new snippet. Fill in the appropriate fields and click 'Post'. Now you can forever go back to that snippet if you need quick access to it or have any issues with your blog. Moving on...

Activate the plugin

After installing the plugin, activate the plugin and go to the Snipplr configuration page in your admin panel. Paste in your API key and set your Snipplr preferences.

Using the shortcode

We're nearly there. Now when you go to write a new post, use the snipplr shortcode [ snippet = id ] without spaces and replace id with the snippets ID number. You can find the snippet ID number in the URL of the snippet you wish to share.

Example below

Below you will find an example of the Snipplr plugin in use. The snippet is getting pulled from my Snipplr account and displayed according to the settings I have set for the plugin.

GPC Magic Quotes Runtime Stripping
Posted by DrewDouglass on July 11th, 2009
All credit for this code goes to the all mighty Fou-Lou of codingforums.com (link given).

if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
{
    function GPCStrip($arr)
    {
        if (is_array($arr))
        {
            foreach ($arr AS $arrKey => $arrVal)
            {
                $arr[$arrKey] = GPCStrip($arrVal);
            }
        }
        else if (is_string($arr))
        {
            $arr = stripslashes($arr);
        }
        return $arr;
    }
    $_GET = GPCStrip($_GET);
    $_POST = GPCStrip($_POST);
    $_COOKIE = GPCStrip($_COOKIE);
    if (is_array($_FILES))
    {
        foreach ($_FILES AS $key => $val)
        {
            $_FILES[$key]['tmp_name'] = str_replace('\\', '\\\\', $val['tmp_name']);
        }
    }
    $_FILES = GPCStrip($_FILES);
}
if (function_exists('set_magic_quotes_runtime'))
{
    set_magic_quotes_runtime(0);
}

Check out the functions links!

I didn't even notice this until I was previewing this post, but if you hover over the functions in the above code, you will see that they all link to the functions in the php manual! That's a very user friendly feature of this plugin.

Follow me on Snipplr!

Feel free to follow me on Snipplr. If you're already on, leave your Snipplr link in the comments and I'll try to keep you on my watch list if I find it interesting. Have fun and use it responsibly, always give credit where it is due!


If you enjoyed this article, you might consider subscribing to our rss feed to stay updated with all the latest tips and articles!

ABOUT THIS AUTHOR

Hi, I'm Drew Douglass and I make sure Dev-Tips.com runs smoothly. I also work for Envato Support and write for NETTUTS.com and ThemeForest.net. I'm passionate about anything web development related, especially php, WordPress, MySQL, and jQuery. Feel free to follow me on twitter.

Trackbacks/Pingbacks

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Popular Series

Output Buffering Articles
Build a Custom AJAX and PHP Contact Form
The Ultimate Image Gallery Manager.
ThemeForest Premium Site and WordPress Templates