This plugin is officially retired, and it is not recommended for new installs.
This plugin allows developers to easily add PHP code to their posts and widgets. All .snippet.php files contained in the configured directory will be executable via a corresponding [shortcode]. For example, a file named my-code.snippet.php can be executed by adding a [my-code] shortcode to your post content. The source directory is configurable: you can put it anywhere on your server that you wish, e.g. you can enhance the security of your code by storing it outside your server’s document root.
By keeping all the PHP code in PHP files, you ensure that your WordPress posts and pages remain clean. All available shortcodes are listed when the user clicks a custom TinyMCE button.
See the Project Home Page for more information. This plugin requires PHP 5.2.6 or greater.
This plugin can save you from writing lots of other plugins because you can easily tie into PHP files without having to register your own plugins.
WARNING: this has not been tested on Windows servers. Sorry, but I don’t have access to one, so I’ve been unable to tune the permissions checks.
Create PHP Snippets
A PHP snippet is any bit of code that you wish to reuse via shortcode. Create a file in one of the directories you defined as a Snippet directory. DO NOT create Snippets inside the
In order to be picked up on the radar, your file must use one of the extensions you defined (e.g.
.snippet.php) and you should include a comment block with a “Description” and a “Shortcode” attribute, e.g.
/* Description: Generates a link to a post or page based on its ID. Shortcode: [link id=123]Click here[/link] */<h3>Usage</h3>
1. Create a new post or page
1. Click the
<?php button in the TinyMCE toolbar to see a list of available PHP Snippets
1. Select the Snippet you want to insert into your post: a shortcode should be generated inside your post content.
Optionally, you can manually type in the shortcode and add in any variables you have defined.
You can add your snippet output to any Widget by using the PHP Widget.
- In the WP dashboard, navigate to Appearance –> Widgets
- Drag the PHP Widget from the list of available widgets to any valid widget area on the right.
- Add a title for your widget
- Select one of your PHP Snippets
- Adjust the shortcode as desired (e.g. to add in special parameters)
- Save the widget instance.
You can install this plugin in the standard way from the WordPress plugin admin screen, or you can download its files and upload the
php-snippets folder to the
- Upload the
php-snippetsdirectory to the
- Define one or more Snippet directories where you will save your custom PHP Snippets, e.g. [+ABSPATH+]/snippets/
- My Snippets are not showing up!
Make sure your files use the correct extension. If you have configured the plugin to look for
.snippet.phpfiles, then any
.phpfiles 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/snippetsor you can use the
[+ABSPATH+]placeholder to point to the web root of your site, e.g.
- 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/snippetsdirectory 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
$yavailable in your Snippet (they would contain “123” and “Llama”, respectively).
$contentvariable 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). The plugin is no longer maintained, so if there is a bug in the code, you’ll have to find a way to fix it yourself.
Fireproof Socks, the creator of this plugin, asked me to renew my annual license on March 5. Today, March 14, nine days later, I discover that the developer has retired the plugin, ceased support, and removed the licensing requirement.
While this plugin works like a charm, the retirement in itself is sufficient grounds not to use it. And any developer who has no moral issues with taking people’s money this way needs to be avoided at all costs.
Contributors & Developers
“PHP Snippets” is open source software. The following people have contributed to this plugin.Contributors
Retirement notice. Thank you to everyone who contributed to this plugin, but it is now officially retired. Licensing is disabled with this version.
Includes handy features:
- Ability to show/hide the TinyMCE button
- “PHP Snippets” button now visible in plain-text view
- PHP 5.3.0 is now required.
- Configurable suffix, so now your snippets need not use the ‘.snippet.php’ extension.
- Pay-what-you-like licensing added.
- Various bug fixes and improvements
- Improved permissions checking in Windows environments.
- General cleanup.
More thorough permissions checking of the configured directories, added some snippets.
Worked out some bugs, cleaned this thing up for the prom.
- Initial release