Code Snippets CPT

Description

A WordPress plugin for elegantly hosting, managing, and displaying code snippets. Own your own data!

Adds a custom post type for managing your code snippets with taxonomies for classifying the snippets. Embed snippets with syntax highlighting to posts or pages via a handy shortcode insert button that allows you to pick from the most recent snippets. Syntax highlighting provided by the Prettify javascript library and the Ace Editor.

Features:

  • Host your own snippet library.
  • Button for easy-copying of snippet (disable with: add_filter( 'dsgnwrks_snippet_do_click_to_copy', '__return_false' )).
  • Button to enable full-screen snippet view (disable with: add_filter( 'dsgnwrks_snippet_enable_full_screen_view', '__return_false' )).
  • Two frontend render engines, prettify (with 2 themes) or Ace (with 32 themes).
  • WordPress editor shortcode button for embedding snippets in your content, and creating/editing those snippets on the fly.
  • Live (tinymce) previews of the snippets in your content editor.
  • Programming language picker (for syntax).
  • Snippet tags and categories.

Feel free to fork or contribute on Github.

Screenshots

  • Code Snippets admin listing
  • Editing code snippet
  • Code snippet shortcode in a post
  • Code snippet insert button/modal
  • Front-end snippet view, using Ace with the monokai theme
  • Full-screen snippet view
  • Snippet easy-copy window
  • Inline snippet editor

FAQ

Installation Instructions
  1. Upload the entire /code-snippets-cpt directory to the /wp-content/plugins/ directory.
  2. Activate Code Snippets CPT through the ‘Plugins’ menu in WordPress.
  3. Create a snippet.
  4. Insert a snippet shortcode via the snippet tinymce button.

Reviews

Outstanding – Does What it Says, and Does it Well

This is an outstanding plugin. It does exactly what it says and it does it well.

As a developer who runs a few blogs that provide code snippets, I needed a solution that was beyond just displaying code snippets in my posts (where then the code snippet was part of the post content). I needed something to keep the actual code snippets separate from the post content.

This plugin does that perfectly. As described, snippets are saved as CPTs (Custom Post Types) and the code snippet can then be used in a post as a shortcode.

That would have been good enough for me on its own, but Code Snippets CPT takes it a step farther. Justin allows snippets to be categorized and tagged, and the editor also displays the necessary shortcode for you. Added bonus: when writing post content it adds a button to the editor to find snippet shortcodes to insert.

Let me add that as a professional developer who makes their living with WordPress, I have pretty high standards. I’ve seen and tested a lot of plugins that should never see the light of day. So when I give a 5 star rating, I mean it. And I would give this 6 stars if I could.

Very handsome plugin, but no way to execute!

Great for creating and maintaining what I call a custom CODELIB.
But without an alternative to choose some of this code to execute on my site this has only exhibition value…
What’s the use of me knowing how to create a custom WP role, for example, if I cannot utilize it on my site when needed?

Excellent

Does what is says.

Would be great if shortcode could accept en ID as a parameter.
Would be great if description would show the slug of the cpt, which is ‘code-snippets’
Would be great also to have a straight forward example of a query to dispplay all snippets on a page.

For those who would like to see an example, this is what i did in a template page :

// WP_Query arguments
$args = array (
‘post_type’ => ‘code-snippets’,
‘posts_per_page’ => ‘-1’,
‘order’ => ‘ASC’,
);

// The Query
$myquery = new WP_Query( $args );

while ($myquery->have_posts()) {
$myquery->the_post();
?>
<article id=”page-<?php the_ID(); ?>” class=”page” role=”article”>
<header class=”page-header”>
<h1 class=”page-title”><?php the_title(); ?></h1>
</header>
<?php echo do_shortcode(‘[snippet slug=’.$post->post_name.’ lang=php]’); ?>
</article>
<?php }
wp_reset_postdata();

Read all 8 reviews

Contributors & Developers

“Code Snippets CPT” is open source software. The following people have contributed to this plugin.

Contributors

Changelog

2.1.0

  • Fix issue with double trailing slashes on DWSNIPPET_URL causing resources to no load under some circumstances.

2.0.9

  • Fix issue with calling wp.mce when it doesn’t exist (in instances where only quicktags are enqueued).

2.0.8

  • Fix ACE frontend to honor the ‘Display Line Numbers’ setting. Fixes #29.

2.0.7

  • Fix “Uncaught Error: Call to undefined function post_categories_meta_box()” occurring when not on post-pages. Fixes #28.

2.0.6

  • Update snippet-copy URL so that it doesn’t 404 when nonce is expired, and also noindex,nofollow the snippet-copy pages when the nonce has expired.

2.0.5

  • Enable native copy functionality available in newer browsers. Props ramiabraham, #27.
  • Clean up styles a bit for full-screen view.
  • Clean up Ace front-end view.

2.0.4

  • Fix bug causing the shortcode button not to insert the snippet when in visual mode.

2.0.3

  • The front-end script needs to load in the footer so that wp_localize_script() works as expected.
  • Better styling for the full-width view and the buttons in the full-width view

2.0.2

  • Better Ace editor support for inline php snippets (i.e. no opening <?php tag).
  • Minify css files.

2.0.1

  • Use Ace editor for the snippet add/edit shortcode modal.

2.0.0

  • Button for opening modal for easy-copying of snippet.
  • Button to enable full-screen snippet view.
  • (When using Ace frontend) Button to toggle line-numbers.
  • (When using Ace frontend) Button to collapse/minify the snippet.
  • (When logged-in) Button to edit Snippet.
  • View individual snippet pages, and link to full-screen snippets.
  • Live (tinymce) previews of the snippets in your content editor, and edit them in place.
  • Option to choose the front-end display theme and render engine (prettify or Ace).
  • Ace editor on the snippet-edit page, and option to use the Ace render engine on the front-end. Props JayWood (#22).
  • Add new snippets on the fly via the shortcode button (vs having to leave your post and to create them). Props JayWood (#22).

1.0.5

  • Add C# as available language.

1.0.4

  • BUG FIX: Remove ‘html_entity_decode’ around snippet output, as it will cause the page display to break under certain circumstances.

1.0.3

  • Replace shortcode button’s usage of ids with slugs because ids can change during a migration.
  • Added filter, ‘dsgnwrks_snippet_display’.
  • Better handling of WordPress-converted html entities.
  • By default, convert tabs to spaces for better readability. Can be disabled with: remove_filter( 'dsgnwrks_snippet_content', 'dsgnwrks_snippet_content_replace_tabs' );
  • Added title attribute to pre element to display title of snippet on hover.

1.0.2

  • Add more languages
  • Add lang parameter to shortcode attributes.
  • Use selected snippet language to set the shortcode lang parameter.
  • Allow shortcode to specify line number to start with

1.0.1

  • WP editor buttons for inserting snippet shortcodes

1.0.0

  • First Release