Manage Code Snippets in WordPress with the Code Snippets Custom Post Type Plugin
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 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.
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.
10 Comments
I’ve tried a bunch of these plugins and this one is the most proper way to do this.
Report
Amusingly enough, I’m building something similar to this myself. What do you like about this version so much?
Report
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
I’ve been utilizing and recc Code Snippets for quite some time now.
https://wordpress.org/plugins/code-snippets/
With 50K downloads and and average rating of 4.8 out of 5 stars it’s pretty epic.
Report
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
You’re exactly right Sami I completely missed this.
Report
Although perhaps suitable on some sites, I think hosting a Code Snippets library on Github makes a lot more sense to most use cases.
I have one there which is open to contributions and forking: https://github.com/senlin/Code-Snippets
Report
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
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
Tim, if you like Gists so much, then perhaps you will like Gistbox: https://app.gistboxapp.com/
Report
Comments are closed.