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.
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.