and like us on FB

Unlike the pastie-style of sharing snippets of code, Snipplr is more of a code repository, or code library, that serves primarily as a resource for collating your own collection of snippets. The Snipplr website says that “… 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”.

With around 60,000 snippets added to their site since 2006 (comparatively small compared to other similar websites), and around 215000 users, it’s not the most popular website… but it can be a good resource. Sadly, with the proliferation of other excellent services, Snipplr is becoming less and less relevant.

The code on this page will simply embed Snipplr code into a WordPress post of page with shortcode. The primary purpose of this quick article isn’t to demonstrate Snipplr embeds but rather illustrate how the highlight code we talked about here might be used to embed code from any website (not just Snipplr).

The default output of the Snipplr embed code is crappy. Snipplr uses GeSHi (Generic Syntax Highlighter) by default and the library hasn’t seen an updated in years. The embed code returns a block of code that doesn’t support line wrapping, there’s a visually unappealing h3 heading, and there’s no stylistic integration with your website.

1. Default Snipplr WordPress Shortcode

An example snippet using our generic shortcode is rendered below. The shortcode of [snipplrr id="29888"] returns:

■ ■ ■

If you were happy to simply return the basic Snipplr code as shown above, the shortcode below is all you’ll need.

2. Simple PHP Syntax Highlighter

The basic beliefmedia_simple_php_highlighter() from this page was provided some time back for demonstration purposes only. It returns formatted PHP code but doesn’t play well on mobile devices. While it line wraps, it doesn’t preserve formatting. The shortcode of [snipplr id="29888"] (where 29888 is the Snipplr snippet ID) returns the following:

1
<?php
2
function getYoutubeVideoID($sVideoURL) {
3
    $sVideoID preg_replace('%.*(v=|/v/)(.+?)(/|&|\\?).*%''$2'$sVideoURL);
4
    return $sVideoID;
5
}

If you’re on a mobile it’s likely you aren’t looking at anything pretty!

3. BeliefMedia CSS Syntax Highlighter

The third method is our preferred means of showing Snipplr (and some other repository) snippets. In this case we’ve used [snipplr id="29888"] which returns the following:

1
<?php
2
function getYoutubeVideoID($sVideoURL) {
3
    $sVideoID = preg_replace('%.*(v=|/v/)(.+?)(/|&|\\?).*%', '$2', $sVideoURL);
4
    return $sVideoID;
5
}

Our function retrieves the Snipplr code, modifies the output, applies the syntax, and caches the result. It supports line wrapping and all the other features of the function described on this page.

Considerations

As stated, while Snipplr was used the purpose of the demo, the function could essentially support any PHP code repository website, mitigating the issues associated with an output that doesn’t fit with the theme of your website. Of course, you could simply copy and paste code in your preferred highlighter tool, but the automation aspects simply improve upon your workflow.

Download

You should be logged in to download this snippet.

Shortt URL for this post: http://shor.tt/14nF