Code Snippets

Description

Code Snippets is an easy, clean and simple way to run PHP code snippets on your site. It removes the need to add custom snippets to your theme theme’s functions.php file.

A snippet is a small chunk of PHP code that you can use to extend the functionality of a WordPress-powered website; essentially a mini-plugin with less load on your site.
Most snippet-hosting sites tell you to add snippet code to your active theme’s functions.php file, which can get rather long and messy after a while.
Code Snippets changes that by providing a GUI interface for adding snippets and actually running them on your site just as if they were in your theme’s functions.php file.

Code Snippets provides graphical interface, similar to the Plugins menu, for managing snippets. Snippets can can be activated and deactivated, just like plugins. The snippet editor includes fields for a name, a visual editor-enabled description, tags to allow you to categorise snippets, and a full-featured code editor. Snippets can be exported for transfer to another side, either in JSON for later importing by the Code Snippets plugin, or in PHP for creating your own plugin or theme.

Manual installation

Upload the contents of the zip file to the wp-content/plugins/ folder of your WordPress installation

Activate the Code Snippets plugin from ‘Plugins’ page.

Network Activating Code Snippets through the Network Dashboard will enable a special interface for running snippets across the entire network.

FAQ

How can I insert my snippet into the post text editor?

Snippets that you add to this plugin are not meant to be inserted into the text editor. Instead, they are run on your site just as if they were added to your functions.php file.

Help! I just activated a snippet, and my whole site broke!

You can try activating ‘safe mode’. All snippets will not execute while safe mode is active, allowing you to access your site and deactivate the snippet that is causing the error. To activate safe mode, add the following line to your wp-config.php file, just before the line that reads /* That's all, stop editing! Happy blogging. */:

No, the snippets are stored in the WordPress database, independent of the theme and unaffected by WordPress upgrades.

Can the plugin be completely uninstalled?

If you enable the ‘Complete Uninstall’ option on the plugin settings page, Code Snippets will clean up all of its data when deleted through the WordPress ‘Plugins’ menu. This includes all of the stored snippets. If you would like to preserve the snippets, ensure they are exported first.

Can I copy any snippets I have created to another WordPress site?

Yes! You can individually export a single snippet using the link below the snippet name on the ‘Manage Snippets’ page or bulk export multiple snippets using the ‘Bulk Actions’ feature. Snippets can later be imported using the ‘Import Snippets’ page by uploading the export file.

Can I export my snippets to PHP for a site where I’m not using the Code Snippets plugin?

Yes. Click the checkboxes next to the snippets you want to export, and then choose Export to PHP from the Bulk Actions menu and click Apply. The generated PHP file will contain the exported snippets’ code, as well as their name and description in comments.

Can I run network-wide snippets on a multisite installation?

You can run snippets across an entire multisite network by Network Activating Code Snippets through the Network Dashboard. You can also activate Code Snippets just on the main site, and then individually on other sites of your choice.

Where are the snippets stored in my WordPress database?

Snippets are stored in the wp_snippets table in the WordPress database. The table name may differ depending on what your table prefix is set to.

I need help with Code Snippets / I have an idea for a new feature for Code Snippets

You can get help with Code Snippets, report bugs or errors, and suggest new features and improvements either on the WordPress Support Forums or on GitHub

All those snippets you collect and put in your functions.php file can be individually turned on and off at will with this plugin.
It's like having another plugin manager, but for all the custom code you write (or copy-paste) into your functions.php file.

Found this simple easy to use plugin in the Woocommerce documentation because I needed to Add a surcharge to cart at checkout. The plugin works great. Just added the necessary snippet to add the surcharge and it runs exactly as expected.

Make the title of each snippet on the manage page a clickable link to edit the snippet (#)

Added nonce to edit snippet page

Hide row actions on manage snippet page by default

Removed screenshots from plugin

Improved CodeMirror implementation

Added a fallback MP6 icon

Use the proper WordPress database APIs all of the time

Rewritten export functionality

Fixed incorrect export filename

Updated CodeMirror to version 3.19

Removed CodeMirror bundled with plugin

Updated WordPress.org plugin banner

Fixed CodeMirror incompatibility with the WP Editor plugin

Fixed CodeMirror incompatibility with the Debug Bar Console plugin

1.8.1.1 (18 Aug 2013)

1.8.1 (29 July 2013)

Compiled all CodeMirror scripts into a single file

Use Sass + Compass for CSS

Use Grunt for build automation

Minify CSS

Fixed code typo that was breaking export files

Updated CodeMirror to 3.15

1.8 (9 July 2013)

Allow no snippet name or code to be set

Prevented an error on fresh multisite installations

Refactored code to use best practices

Improved database table creation method: on a single-site install, the snippets table will always be created. On a multisite install, the network snippets table will always be created; the site-specific table will always be created for the main site; for sub-sites the snippets table will only be created on a visit to a snippets admin page.