WordPress.org

Plugin Directory

PHP Snippets

Provides an interface for developers to easily add PHP code to posts and widgets via selectable shortcodes.

= My Snippets are not showing up!=

Make sure your files use the correct extension. If you have configured the plugin to look for .snippet.php files, then any .php files will not be selectable as PHP Snippets.

Make sure you have correctly configured your PHP Snippets directory under Settings --> PHP Snippets. The directory has to be a full path, e.g. /home/myuser/public_html/snippets or you can use the [+ABSPATH+] placeholder to point to the web root of your site, e.g. [+ABSPATH+]mysnippets

What information needs to be in the Snippet header?

As of version 0.9, the information header is optional. Using it, however, can enhance the user experience because it lets users know what your Snippet does and you can customize the inputs.

Your Snippets should include an information header in the same way that WordPress plugins and themes, but it's simplified. Include a Description: and a Shortcode: to indicate a description and a sample shortcode, respectively.

/*
Description: Generates a link to a post or page based on its ID.
Shortcode: [link id=123]Click here[/link]
*/

Look at the examples in the php-snippets/snippets directory for some examples.

What inputs does my Snippet accept?

Anything you pass in your shortcode will be passed to your Snippet. E.g. [mySnippet x="123" y="Llama"] will make the variables $x and $y available in your Snippet (they would contain "123" and "Llama", respectively).

The $content variable is reserved for when you use a full tag, e.g. [mySnippet]My content goes here[/mySnippet] would make$content` contain the text "My content goes here".

Warning: WordPress allows only lowercase parameter names, so your input variables should be lowercase.

What should my Snippet output?

Your Snippet should print its output (not return it). This makes it easy for you to include reusable bits of HTML.

Technically speaking, your code doesn't have to output anything -- some scripts may only need to execute on the back-end and remain out of site.

There's a Bug in the Plugin!

Check to make sure the bug is actually in the plugin and not in one of your Snippets. This plugin attempts to act primarily as a transparent pass-through, and the error messages should be confined to your code (hopefully not mine). If there's something goofy going on, please let me know by filing a bug report.

Requires: 3.0 or higher
Compatible up to: 4.1.0
Last Updated: 2015-2-8
Active Installs: 2,000+

Ratings

3.7 out of 5 stars

Support

Got something to say? Need help?

Compatibility

+
=
Not enough data

0 people say it works.
0 people say it's broken.

100,1,1
0,1,0
100,2,2
100,2,2
100,1,1
100,1,1
0,1,0 0,1,0
50,2,1
0,1,0