Sniplets Plugin
| Download: | sniplets.zip |
|---|---|
| Version: | 1.4 |
| Updated: | January 1, 2009 |
| Size: | 431.58 KB |
Support This Plugin!While this software is being provided free to use, it takes considerable time to develop and support. If you do find it particularly useful or want to request a feature then consider donating money as an incentive for me to carry on developing it. Thanks! |
|
| I have other plugins too! | |
Sniplets is a generic text insertion plugin with support for an extensible processing framework. At it's simplest this means you can dynamically replace text in your posts with text that may be defined elsewhere, or created by some other module. For example, you can use Sniplets to perform syntax highlighting of files, execute custom PHP code, insert data from a database, and perform all manner of other useful tasks within a standard interface. On top of this, Sniplets can be automatically inserted at key points on a page. For example, you can automatically insert text on every RSS post.
- Replace tags in a post with pre-defined text
- Customizable placement points and templates
- Apply processing modules to the pre-defined text to perform additional functionality
- Automatically insert Sniplets in key points of a page (before post, after post, more tag, header, footer, comment form, sidebar, and initialization)
- Apply pre-defined templates to Sniplets
- Includes a Sidebar Widget to allow Sniplets in the sidebar
- Custom modules can be written by the user
- Fully localized
New for version 1.3
Sniplets can now be placed in a variety of places including RSS feeds. This gives you the ability, for example, to add a message at the end of every RSS post.
Note that since version 1.3 Sniplets requires WordPress 2.5 and PHP 5. While every effort is made to retain data from previous versions, there is no guarantee that your data will survive the upgrade process. Sorry.
If you did use custom theme placements then you will need to change your places. Placements configured from the options screen can be found under 'custom/your place name'.
Version History
- 1.4 - 2.7 styling, nonces, jQuery
- 1.3.10 - Fix menu in WP 2.7
- 1.3.7 - Bring back missing sniplets, fix limits
- 1.3.6 - Add sniplet_transform_text function
- 1.3.5 - Update to the_sniplet
- 1.3.4 - Fix #289
- 1.3.3 - Add 'all' placement
- 1.3.2 - WP 2.6
- 1.3.1 - WordPress 2.5, import/export
- 1.2.5 - Fix #84, #107
Installation
The plugin is simple to install:
- Download sniplets.zip
- Unzip
- Upload the
snipletsdirectory to your/wp-content/pluginsdirectory
You can find full details of installing a plugin on the plugin installation page.
Creating sniplets
Sniplets can be local or shared. A local sniplet is only available to a specific post or page, while a shared sniplet is available across your entire site. Local sniplets are created from a special interface on the edit page:
Shared sniplets are created from the Manage/Sniplets page.
Wherever the sniplet is created it is edited in the same way:
A sniplet has a name, some contents, a template, a number of modules, and a number of placements:
name- used to refer to the sniplet in your post (it may contain spaces and does not need to be unique)contents- can be any text, HTML, PHP, or whatever data you want to pass through any functionstemplate- determines if a pre-defined style is to be usedmodules- small modules that perform a specific function on the content of the sniplet. For example, one function may syntax highlight PHP code.placements- determines where on the page the sniplet will appear. Multiple placements are allowed, and a sniplet can always be manually placed in a post.
Modules
Sniplets takes the idea of text replacement one step further by allowing you to attach special processing modules to a piece of text. Multiple modules can be attached and are 'executed' in order. The first module takes some text, processes it, and passes it on to the next module. In this way it is possible to chain functions together and create a highly customized sniplet.
Modules can also accept parameters that change how the function works. Parameters are comma-separated, and are specific to the module:
Note how a parameter is defined by a name (language), an equals, and a value (php). If no equals is supplied then the parameter is assumed to be 'true'.
Adding sniplets into a post
Sniplets are inserted into posts by using a special tagging syntax:
[sniplet NAME]
That's it! The name must match either to a local or shared sniplet, including any spaces. The Sniplets plugin will replace this tag with the results of running all functions on the sniplet contents.
If you configure Sniplets to do so, you can also use an additional tag to change the contents of a Sniplet from inside your post:
[snip NAME]new content[/snip]
This is basically the same as before but this time the contents of the sniplet are overridden by whatever is inside the start and end tag.
Placements
Sniplets can be automatically inserted into key points of a page by assigning placement points. For example, adding a placement point 'comment form' to a sniplet will automatically insert that sniplet into the comments form area. The following areas are pre-defined:
- Header - appear in the theme header
- Footer - appear in the theme footer
- Before post - appear at the start of a post (first page or all pages)
- After post - appear at the end of a post (last page or all pages)
- More tag - appear where the more tag is defined
- Comment form - appear where the comment form is
- Initialization - appear before anything else on the page (useful for running custom code to insert cookies)
As part of version 1.3, these areas can now refined to specified pages of your website. For example, RSS feeds, posts, archives, and home page. Not all places are available for given page (for example, there are no comments on RSS feeds).
Additionally you can define your own placement points from within your theme or from the Sniplets options page. Custom placement points can then be inserted into your theme using template tags. To do this you need to use the special WordPress filter sniplet_places. You can hook into this from the functions.php file in the theme:
<?php add_filter ('sniplet_places', 'my_theme_places'); function my_theme_places () { return array ( 'My Theme Name' => array ( 'mytheme/myspecialplace' => 'Before the Sidebar', 'mytheme/myotherplace' => 'After the Sidebar' ) ) } ?>
Note how the points are defined as an array mapping from a unique ID to a description string. Since version 1.3 you must prefix all places with an identifier for your theme.
Alternatively you can define placements from the Sniplets options page. Here you can only specify the unique ID without description.
Limits
Sometimes you may want to limit the number of Sniplets that appear in a particular placement. For example, you have a message area at the top of your posts, but you only want a maximum of two messages to appear. You can achieve this using limits.
Limits are configured per placement area from the Sniplets option screen. Each placement has an input field where you can specify the maximum number of Sniplets allowed to appear there. A value of 0 means there is no limit. In addition to specifying the maximum limit you can also randomize a placement. This has the effect of displaying Sniplets in that placement in a random order. For example, you limit the placement 'message' to three, set the randomise option, and then create five Sniplets for that placement - the plugin will randomly select three of the Sniplets each time the page is viewed.
Templates
Templates are simple wrapping styles that are applied to the output of a Sniplet. Some templates are provided as default and they can be extended from the Sniplets options page:
- Inset - wrap the content in a
divwith classinset - Warning - wrap the content in a
divwith classwarning - Notice - wrap the content in a
divwith classnotice
Templates are defined in the sniplets/view/sniplets directory. If you add a new template to the Sniplets option page then you need to add a corresponding template file.
Bundled functions
The processing functions included with Sniplets are:
- Syntax highlighting (using GeSHi) - this will syntax highlight text from a variety of sources, including PHP, HTML, CSS, and JavaScript, using the GeSHi source highlighter
- Execute as PHP - executes the contents as though it was PHP code
- Include file or URL - treats the contents as a filename or URL and retrieves the contents of that file/URL
- User is logged in/out - returns an empty peice of text is the user is not logged in or out
- Random line - randomly selects a line from the contents
- Ramdom word - randomly selects a word from the contents
- Reverse - reverse the contents
- wpautop - apply WordPress auto-formatting to the contents
- HTML Encode - HTML encode the contents
Full documentation on modules can be found on the Sniplet modules page.
Execute PHP
The Sniplets plugin has an additional replacement mode that will execute and replace any PHP code in a post. This mode must be enabled from the Sniplets options page as it represents a security risk if you do not trust your editors.
For example, with the mode enable you can directly enter this into a post:
Today is the year <?php echo date ('Y'); ?>, and it's very lovely.
Security Concerns
Some of the modules open a big door into your website and could give someone the ability to cause a lot of mischief. The Sniplet plugin allows anyone to insert a Sniplet tag in a post, but only an administrator can create a Sniplet. This should be sufficient for you to restrict what an untrusted user can do. If you need further restrictions then you may want to consider deleting some of the modules. This can be done simply by deleting the appropriate file in the 'sniplets/modules' directory.
Creating your own module
Creating a custom module is simple (provided you know PHP!):
- Create file in
wp-content/plugins/sniplets/modules - Write code to process the
$textvariable - Output any results to the screen (i.e. use
echo) - Include a special header at the start of the file so Sniplets will know it is a module:
<?php /* Name: Name of your module, as appears in the drop-down box */ ?>
Template Tags
You can also insert a Sniplet directly in a theme template file by including the function:
<?php the_sniplet ('name'); ?>
You can insert a placement point by including the function:
<?php the_sniplet_place ('place'); ?>
This will insert the contents of all Sniplets that have the same placement.
Alternatively you can insert Sniplets and placements using WordPress 'actions':
<?php do_action ('the_sniplet_place', 'mytheme/yourplace'); do_action ('the_sniplet', 'sniplet name'); ?>
This has the advantage that should the Sniplets plugin not be active then the theme will not show any errors.
Support
Please direct all support questions to the Sniplets support forum. Support questions left on this page may not be answered.
Bugs & New Features
A full list of all bugs can be found in the Sniplets issue tracker.
| Date | Current outstanding bugs | Status |
|---|---|---|
| 01 Oct 2008 | Limits |
A full list of all requested features can be found in the Sniplets feature tracker.
| Date | Current requested features |
|---|---|
| 07 Dec 2008 | Disable warnings |
Help me to save time by reading these instructions!
Please report bugs in the Sniplets issue tracker.
Please make feature suggestions in the Sniplets feature tracker.
Please direct all support questions to the Sniplets support forum.







Comments (page 3 of 3)
Jul 6, 2008 6:44 pm | Reply
This quick hack seems to make the system functional again on 2.5.1:
Change line 22 of view/admin/sniplets.php from:
<form action="" method="post" onsubmit="return add_page_sniplet(0);">
to:
<form action="" method="post">
I can't vouch for any disabled functionality that removing the add_page_sniplet might cause, but it seems to work without having to disable javascript.
Jul 12, 2008 9:49 am | Reply
hi. Thanks for plugin
perfect.
Regards
Jul 15, 2008 12:31 am | Reply
Hi John,
Sniplets is looking excellent although I've spotted a slight issue in version 1.3.1. There's no export.php file in the /sniplets/view/admin/ folder. Also, there's something wrong with the "limits" subpage - getting issues here.
Have emailed you details (since the tracker doesn't seem to be working).
Would be cool to have this fixed soon as that bug really messes up pages.
Cheers,
Zain
Jul 19, 2008 2:53 am | Reply
I am created a sniplet with php code. The problem is that I need to include my code in a single line, if I use several lines PHP code is no executed. For example:
+ Fine execution
+ Wrong execution
Outputs the following:
echo 'Hello world';
?>
Please, any idea?
Jul 19, 2008 10:19 am | Reply
I'm not sure what the code was but Sniplets definitely does execute more than one line of PHP. If you want to raise a bug then you can put the code that you are having trouble with there.
Jul 23, 2008 9:21 am | Reply
Thanks, this is exactly what i was looking for.
Aug 3, 2008 12:39 pm | Reply
Hi John,
As I have some problems to register on the forum, I post my problem here.
I made a fresh install of WP 2.6 with only the sniplet plugn activated. The theme is the default one.
When I create a sniplet and put it in a post, the sniplet displays well when I am on the post page but does not display when I am on the homepage.
Template tags don't work more on the homepage where as it displays well on the post page.
I use PHP > 5.0
It's odd that other users don't have this problem... (and I tested on three wordpress installs)
Aug 4, 2008 1:43 pm | Reply
Jooj, in previous versions of Sniplets nothing was displayed on the home page - this was on purpose. In the latest version I've added the ability to put a sniplet of the home page, but this is something you need to enable using the placements function
Aug 8, 2008 3:37 pm | Reply
I'm having trouble getting any sniplet widget to display in the archive.php page. It displays fine on all other pages. I put the same code in a text widget and it displayed fine. Am I missing something?
Aug 17, 2008 4:09 pm | Reply
Hello. A very cool plugin, but i have also the same problem of executing more than one line of php code. I have the following sniplet:
and use the 2.6 wordpress.
Aug 17, 2008 4:25 pm | Reply
..and the source code i get looks like:
<?php</p>
<p>echo date(Y);</p>
<p>?>
Aug 22, 2008 3:12 am | Reply
This happens when you enter the code in the "visual editor" of Wordpress. You have to do it in clean HTML.
Aug 25, 2008 4:49 pm | Reply
Hi,
First of all, thanks for the work you put into this plugin. I am curious about one thing, and it could be that I am not doing this properly or overlooked the instructions for it.
I want to put a sniplet in a page and I want the sniplet to execute a php script. This seems like a pretty simple thing to do.
My problem is that the php isn't being executed. I also tried making a module, but I couldn't see any modules anywhere, let alone add one to my page.
I am using PHP version 5.2.6, MySQL version 5.0.16, and Wordpress version 2.6.
Thanks!
Bryan.
Aug 30, 2008 3:26 pm | Reply
Great work,
I have been searching for a plugin such as this one for a while now.
Am using it in conjunction with some of the plugin from Rob Marsh (Similar Posts etc...).
Thanks a mil,
R
Sep 3, 2008 4:49 am | Reply
Bryan, it is a simple thing to do and was the original reason for this plugin. If you want to execute PHP then you need to add a module to the sniplet and set it as 'execute as PHP'
Sep 3, 2008 10:31 am | Reply
This sniplets plugin and all your other plugins are too awesome. I definitely will use those plugins. Hope to learn more from you.
I love it... Good work!
Sep 9, 2008 1:45 pm | Reply
Hi there John,
just wondering about the sniplet custom placements bug that where in the bug tracker just a while ago and that I'm still experiencing. Have you had a look at it? Is it only me that is having problems with sniplet places defined the "new" way in the functions.php and then not working when I assign that place to a sniplet (sniplet not found)?
Best wishes and keep up that fantastic work. Still think that sniplets is one of the most awesome WP plugins out there.
/Jonas
Sep 10, 2008 1:47 am | Reply
As far as I'm aware the problem was resolved or explained (see here
Sep 12, 2008 12:23 pm | Reply
Is my interpretation right, that the_sniplet() function allows you to display a sniplet anywhere you want in a theme template? I was the one that posted the bug in the issue tracker mentioned by Jonas and it's still not working for me. In the issue discussion people started talking about custom placements, but I'm not custom using placements. I simply select "Manual" when I create the sniplet, then call it using the_sniplet() in my theme. But nothing shows up and this used to work.
Sep 14, 2008 6:45 pm | Reply
Hey man, can you tell me exactly how to add the sniplet into my post?
Sep 15, 2008 1:45 am | Reply
Amar, see the section above titled 'Adding sniplets into a post'
Philip, I only looked at placements before so I'll look into the_sniplet function
Sep 15, 2008 7:13 am | Reply
Philip, I've tried using the_sniplet at it worked perfectly. You'll need to provide more details about your sniplet and how you are calling the function in your theme.
Sep 24, 2008 2:23 pm | Reply
Hi John, thanks for replying. I've detailed how I'm using Sniplets in your Support forum. I think I also found out why the sniplet isn't showing.
Sep 24, 2008 5:56 pm | Reply
In Wordpress MU (I did not try in Wordpress), sniplets do not show in "Latest posts" (Home) page, only in the Post page itself.
Sep 24, 2008 6:05 pm | Reply
Syntax highlighter has ugly "feature". For example, if you want it to highlight a PHP language, it is not sufficient to specify language=php as parameter - you must include braces in a code.
And - another thing: I was never sure if Sniplet was active (The green-red button). Just add a tooltip to the button with: "Sniplet is active. Click here to deactivate it", or something like that. I know, there is fade (gray) effect on whole snipet line, but I was never sure.
Sep 25, 2008 1:38 am | Reply
Sniplets has not been tested with WPMU and is not designed to work with it.
Oct 10, 2008 12:20 pm | Reply
It seems this plugin has bug when executing php code within documents. It can execute only oneliners, meaning iw whole php code is in ine line. If phpcode spreads toseveral lines it is not executed properly.
I tried this on plain Wordpress 2.6.2 with classic template
Oct 12, 2008 3:22 am | Reply
Sniplets works just fine with multi-line PHP code. Remember that PHP code must be wrapped in <?php and ?>
Oct 13, 2008 2:30 am | Reply
Well, of course I use php delimiters for code. I tested it. When are at the same line it is parsed correclty, but if it is split to more than one line, that part of code is displayed as text.
Oct 13, 2008 8:59 am | Reply
I've also tested it, and I can't reproduce any problems with multi-line PHP. As with any problem, please provide full details of the issue including your server environment, version of WP, and the code you are trying to insert. Without this there is little I can do. Please also try to detail problems either in the support forum or in the bug tracker so that this page does not become full of code.
Oct 29, 2008 2:33 am | Reply
What a fantastic plugin! Thanks!
Nov 4, 2008 1:36 am | Reply
Howdy,
I'm trying out the sniplet plugin and can't seem to get the 'include file or URL' module to work at all. I try a fully qualified pathname such as c:\somewhere\other\wp-content\plugins\sniplets\readme.txt and it always returns an error. Always 'file does not exist'.
I place this pathname in the 'content' box right? Tried it in the 'parameter' box also but doesn't work there either. All I want to do is include a chunk of html into a post. I must be doing something wrong. I've tried this in shared and post specific sniplets and neither works.
Sniplets 1.3.8, WPMU 2.6.2
Any help?
Burt
Nov 8, 2008 3:34 pm | Reply
Hi John, first of all thank you for a great plugin.
I am experiencing the same issue as Pedja, where multi-line php code within a sniplet are not executed. I'm running WP 2.6.3 with default templates.
Nov 16, 2008 4:30 am | Reply
Viil, you will need to send me the code you are trying to insert because I cannot reproduce this problem at all.
Burt, Sniplets was never designed to work with WPMU and I cannot say whether this will cause a problem or not
Nov 20, 2008 6:36 pm | Reply
John - I am in the same boat as Burt. I cannot get the include module to work. I am using regular wp, not wpmu.
Burt - a work around that I am using is change module to Exec-PHP and then use:
This is working for me, but would still like to get the include module to work. I can get it to grab a url from another site, but can't do a local file.
John - what should the path be to get it to work? Thanks, other than this issue its work great for me. Great plugin.
Nov 20, 2008 6:39 pm | Reply
sorry, last time. put this inside php block:
include 'file-to-include.php';
Nov 24, 2008 2:55 pm | Reply
Will a sniplet that I put in a post also be rendered in the RSS feed for that post? What I am finding is that when I create a sniplet to be executed as PHP, is not executed as PHP in the feed. Is this expected behaviour or am I doing something wrong?
Dec 8, 2008 12:10 pm | Reply
Hi John,
you have an ugly typo on your page.
do_action ('the_snipet', 'sniplet name');is missing 'l', so the correct is
do_action ('the_sniplet', 'sniplet name');Dec 9, 2008 2:24 am | Reply
Can I also add javascript to a page with this plugin?
Dec 9, 2008 6:59 pm | Reply
I don't get it. I don't see where I can select a module anywhere. There is only the 'Placement' and 'Template' option for new sniplet.
I want insert a file as highlighted source code.
Dec 11, 2008 4:24 am | Reply
Peter, I'll try to make my typos a little more attractive next time.
Matthias, yes you can.
Arnold, look at the advanced section in the bottom right of each sniplet.
Dec 16, 2008 10:14 pm | Reply
Seeing the same problem as "Arnold", there is no "Advanced" selection box being displayed. I've just installed WP and Sniplets today, so is this perhaps a WP 2.7 incompatibility problem?
Jan 1, 2009 9:37 am | Reply
Can you send a screenshot? There should be no reason why an advanced box doesnt appear.
Dec 18, 2008 5:14 pm | Reply
If it would only work, but it wont let me create a sniplet. the "add" buttons dont work at all.
Pings & Trackbacks
wordpresscn.com, etng.net, schwarzerhut.wordpress.com, knowledgeconstructs.com, knowledgeconstructs.com, griffon.cc, stalkked.com, leonaut.com, neilbryant.net, stephanmiller.com, timesaver.in, stephanmiller.com, protonsoft.co.uk, salescopyquickfix.com, salescopyquickfix.com, wp-plugin-archive.de, marktastic.com, code.compartmental.net, simonholywell.com, warriorforum.com, suamimalas.blogdetik.com,
Leave a Reply