If you read “how-to” stuff for WordPress sites around the web, then you frequently run across what many people like to call “snippets”. Short bits of code or functions to do various things. I myself post snippets frequently, usually made up on the fly to solve somebody’s specific problem.

One question I get a fair amount is “where do I add this code?”

The usual answer to this for a lot of people is “in the theme’s functions.php file”. This is a quick solution, but it is often a problematic one.

The reason this has become the more or less go-to place to add these snippets is because it’s complicated to explain to a newbie how to make a plugin and activate it, or to point out the problems with modifying core code, or plugin code. Saying to look for a specific file in their theme, on the other hand, is quick and easy, and until recently theme upgrades have been fairly rare.

However, as themes get upgrades, it becomes more and more incorrect to tell people to modify them directly. And telling people how to create child themes is complex, even if it’s easy to do.

So I’d like to start a new trend, and recommend that people start making Site-Specific Plugins. Most people who run WP sites on a serious level do this in some way already, but if you make it sorta-standard practice, then it’ll make things simpler all around.

How to create a Site-Specific Plugin

1. Create a new directory in the plugins directory. Name it after the site in some fashion. For example, /wp-content/plugins/example.com or something like that.

2. Create a new php file in that directory. Name is dealer’s choice.

3. Put this in the file:

<?php
/*
Plugin Name: Site Plugin for example.com
Description: Site specific code changes for example.com
*/

4. Finally, go activate your new blank plugin on the site.

Now you have a simple and specific place to add snippets. It will survive upgrades of any sort, and you can edit it to add new code on an as needed basis. What’s more, it’s kinda sorta break-proof. If the user uses the built-in plugin editor to edit it, and they add code that breaks the site, then the editor detects that on saving the code and deactivates the plugin, preventing the “white screen of death” for their site.

This is a much better way to use “snippets” than the theme’s functions.php file, and we should really use it more often in our replies to users.

Shortlink:

25 Comments

  1. Good point on the issue of there being no white screen of death with this route. I wonder if you get a white screen of death if a child theme breaks the site in a similar manner? I might check later on.

  2. One site has it as a sitewide plugin, the other is theme specific, since I don’t use the same theme on all sites. That’s the problem with MultiSite, though. CPTs and other site-based solutions are harder to apply.

  3. I was going to suggest using mu-plugins, but the WSOD-prevention is too valuable.

    Just remember that it works only as long as you’re editing the main plugin file.

  4. Of course, why didn’t I think of that before? :)

    I’ll always recommend this approach to everyone, thanks Otto!

  5. Been using a “Portable Functions Plugin” for some time now – a huge time saver!

  6. Just love the idea of “bullet proof” : simple and efficient idea.

  7. […] Plugin nutzenDiese Variante hat vor kurzem Samuel Wood aka Otto in seinem Blog unter „Creating a Site-Specific Snippets Plugin“ veröffentlicht.Hiebei wird ein einfaches Plugin wie folgt angelegt:Erstelle zunächst ein neues […]

  8. […] the plugin you are using does NOT offer a way to add the info, then you could consider a site specific plugin where you add your own filter or shortcode to display the values however you want them […]

  9. […] well what you could maybe do (if you have some php knowledge), is write your own little site-specific snippets plugin see otto’s post on how to do this […]

  10. […] you are not happy with your theme’s author/user template, you could use a site specific snippets plugin to override it with a template of your own using the template_redirect […]

  11. This is such a simple neat little tip. I used thematic and arras recently and created child themes for them. The problem that I faced was that functions.php was getting too bigger and I was using the child theme mainly to add snippets.

  12. I’d made a couple posts before about a functionality plugin. But not in enough detail. I covered it again on my site, citing this article. It really is a good idea. I’m going to try to point people toward making a plugin when answering in the forums (and on VoodooPress) from now on.
    http://vudu.me/plugin is my article, linked to yours.

  13. Dave Clements wrote a good article about this topic over the summer — http://www.doitwithwp.com/create-functions-plugin/

  14. How about site specific plugin and theme. we can use some function our theme.

  15. this is awesome, thank you! such an easy and great tip.

  16. […] that idea with other ideas of 1) Otto's site specific plugin (avoid changes to upgradeable files ) http://ottopress.com/2011/creating-a…ippets-plugin/ and 2) the wordpress.com custom css plugin http://wordpress.org/extend/plugins/safecss/. and […]

  17. Thanks for this. Quick question, I made the plugin, but how do I enter in the code I want it to put into my header.php? For instance I want to include buy-sell adds info, google webmaster, bing, yahoo, etc….all in the header.php. Is using a plug in like this the best way to do this?

    Thanks.

  18. I have started to use mu-plugins as suggested by Bill Erickson for any functions that are not theme related. Would you still sugest having both the mu-pugins and a site specific plugin?

  19. Hi Otto, Thanks. I had to add some code that I didn’t want in my theme, so this was just the ticket, but it didn’t actually work for me. Though, when I went to the codex page http://codex.wordpress.org/Writing_a_Plugin and copied the equivalent header information (about 1/3 way down the page) that worked just fine. Maybe you changed the plugin header requirements in the versions of WP? I tried this on version 3.3.2 Maybe I just put some white space somewhere I shouldn’t?

    Anyway, thanks this is a nice and simple solution.

  20. […] Creating a Site-Specific Snippets Plugin (Otto, man!) […]

  21. […] made it easy to rinse and repeat. However, using only a single core functionality plugin or a site-specific plugin, while having its advantages, can be problematic. As Gary specifically illustrated to me, […]

  22. […] WP Engine24.How Many WordPress Plugins Should You Install on Your Site? – WP Beginner25.Creating a Site-Specific Snippets Plugin- Otto on […]

Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Need to post PHP code? Wrap it in [php] and [/php] tags.