Manage Code Snippets in WordPress with the Code Snippets Custom Post Type Plugin

code-snippets-cpt

Maintaining a library of code snippets can save you time when coding similar tasks in the future. Although there are countless sites online where you can host code snippets, it’s more convenient to have them at your fingertips when writing a blog that includes code.

Plugin developer Justin Sternberg recently released Code Snippets CPT, a plugin that allows you to manage and display code snippets in WordPress. This unique use of custom post types stores code snippets as their own individual posts, which can be pulled into content via a handy shortcode.

The plugin uses Google Code Prettify to add syntax highlighting to your snippets. When creating a new snippet, you simply select the language from the dropdown at the top of the post editor. Write a description, add your snippet, and click publish.

code-snippets-cpt-edit-post

Code Snippets CPT also includes custom taxonomies for classifying your snippets. You can add both snippet categories and snippet tags, which will then allow for some unique ways of organizing and displaying your library of snippets.

When you want to display a snippet within a post or page, click on the “Add Snippet” button in the visual or text editor to launch the shortcode finder. You can select whether or not you want to display line numbers for the snippet that you are embedding.

code-snippets-shortcode

Sternberg’s site is running the plugin, if you want to view a few live examples of snippets in posts.

Code Snippets CPT allows you to create a searchable archive of code snippets on your own site, without having to host snippets with a third party service. The advantage of hosting your own snippets is that you can keep your code library centralized and back it up as part of your WordPress site.

Having code snippets stored as custom post types is a step up from simply using a syntax highlighter plugin, because it gives you the flexibility to sort and display snippets using their own snippet-specific taxonomies. If you want to get your code library organized in 2015, check out the Code Snippets CPT plugin on WordPress.org.

Would you like to write for WP Tavern? We are always accepting guest posts from the community and are looking for new contributors. Get in touch with us and let's discuss your ideas.

10 Comments


    1. Amusingly enough, I’m building something similar to this myself. What do you like about this version so much?

      Report


      1. Wow James, they are so similar in a lot of ways. I like some of the features you’ve incorporated like adding a new gistpen from your insert dialog as well as being able to change themes. Maybe we ought to consider joining efforts?

        Report


    1. Did I get it wrong but isn’t that different kind of plugin. Code snippets plugin is for running code in your site, kind of like mini plugins.

      Code Snippets CPT is a plugin which you can use to display code snippets, not execute them.

      Report


  1. This looks great. I’ve been looking towards organizing and hosting my code in 2015, and this looks like a pretty nice way of doing it. Thanks Justin and Sarah for bringing it to our attention.

    Report


  2. Like Piet I’m a fan of Github for snippets. I use a private repo to store and sync Sublime Text snippets across my machines, private Gists to share with coworkers and public Gists to share/embed code in blogposts.

    Report

Comments are closed.