Introduction to the PHP Snippet Library

The PHP Snippet Library plugin is very easy to use. Once you have installed it, you can go to Tools >> PHP Snippet Library and immediately begin adding your snippets of PHP code to the library.

The first screen you will see contains an empty list–you don’t have any snippets yet!–with a couple of buttons above it. Clicking the Add New Snippet button will display the Add Snippet form where you can enter your first snippet of PHP code.

Did you notice how quickly the form appeared? That’s because the plugin’s settings pages are driven entirely by Javascript (the JQuery framework, to be precise), and only when the plugin needs to communicate with the web server will there be any delay in processing.

To add a new PHP snippet, simply give it a name, a useful description (optional) and type or paste the PHP code snippet into the text area provided. You can also decide whether you want the snippet to be available everywhere on your site, or only in the admin section, or just in the main, public part of your blog.

Once you have finished entering the snippet, click Add Snippet to add it to the library. Before the code is added, it is checked for syntax errors. This is very useful because if you run invalid PHP code on your blog, you could be left staring at a blank screen with an nasty looking error message at the top of it.

Assuming the snippet validates okay, you will be take back to the main settings page where your new snippet now appears in the list.

That’s about all there is to it. The snippet is stored in a PHP source file on your web server, and will be included in the PHP code for your site for as long as the plugin is active. You can disable and enable each snippet individually–useful for running the occasional debug code, for example–and you can also disable and enable the entire library at once, if necessary.

You will quickly notice that the list of snippets behaves the same way as the lists of posts and pages on their settings pages. You can sort the list by clicking on one of the column headings, and you can perform bulk actions by selecting rows and an action from the bulk action drop down list.

Clicking on the name of the snippet allows you to edit it, and you can also delete a snippet when you have finished with it (but remember it is gone for good once you have confirmed its deletion!).

If you run in to trouble using the plugin, please consult the Troubleshooting page for assistance, or leave a comment in the Support Forum.

Comments are closed.