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:

33 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 […]

  23. Hello Otto,

    I have changed all my snippets/code adjustments from functions.php over to site specific plugins.

    However I see that instead of overwriting the functions.php code it is simply adding to it.

    For example my “Change footer” script, adds a second footer instead of removing the old +creating a new.
    How do I fix this so that the site specific plugins are overwriting the standart scripts (functions.php) (I guess this means I have to find a way to make sure that the plugins are loaded after functions.php…but how do I do this?)

    Thank you,

    My code for the change footer plugin:

    &amp;lt;?php
    /*
    Plugin Name: *mysite change footer
    Description: Change the footer for mysite
    */
    /* Start Adding Functions Below this Line */
    
    
    /* Remove the site footer*/
    remove_action( 'genesis_footer', 'genesis_footer_markup_open', 5 );
    remove_action( 'genesis_footer', 'genesis_do_footer' );
    remove_action( 'genesis_footer', 'genesis_footer_markup_close', 15 );
    
    
    //* Customize the site footer
    add_action( 'genesis_footer', 'bg_custom_footer' );
    function bg_custom_footer() {  ?&amp;gt;
     
    	&amp;lt;div class=&amp;quot;site-footer&amp;quot;&amp;gt;&amp;lt;div class=&amp;quot;wrap&amp;quot;&amp;gt;&amp;lt;p&amp;gt;Copyright &amp;amp;copy; 2014-2015 &amp;lt;a href=&amp;quot;http://localhost/&amp;quot;&amp;gt;Lean-Strength&amp;lt;/a&amp;gt; &amp;amp;middot; All Rights Reserved &amp;amp;middot;&amp;lt;a href=&amp;quot;http://localhost/contact/&amp;quot;&amp;gt;Get in Touch&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;
     
    &amp;lt;?php
    }
    
    ?&amp;gt;
    
    
  24. Hi, I’ve just started using this site-specific plugin method today. The first couple of snippets in my plugin worked fine, but the third had no effect. When I moved the code to the child theme’s functions.php it works perfectly.
    Can you think why that might happen?

    I’m trying to modify the working of the “Restricted Site Access” plugin by opening access to a couple of pages. the code i have is below. I’m guessing maybe my site specific plugin sets the variable “$is_restricted” and then the “Restricted Site Access” re-sets it. Or something? . My PHP / WordPress code knowlwedge is very superficial…


    //
    add_filter( 'restricted_site_access_is_restricted', 'my_restricted_check' );
    function my_restricted_check( $is_restricted ) {
    global $wp;
    if (
    $wp->query_vars['pagename'] == 'contact' ||
    $wp->query_vars['pagename'] == 'subscribe' ||
    $wp->query_vars['name'] == 'aplr-2013-volume-21-number-2'
    )
    $is_restricted = false;

    return $is_restricted;
    }

    Any help or hints would be much appreciated.

    • The restricted_site_access_is_restricted filter actually passes you the relevant $wp value, you shouldn’t check the global $wp for it.

      add_filter( 'restricted_site_access_is_restricted', 'my_restricted_check', 10, 2 );
      function my_restricted_check( $is_restricted, $wp ) {
      

      And remove the dependence on global $wp.

      • Otto, apologies for delay in getting back to you – Easter got in the way.

        Thank you for the quick reply, and the code corrections. It worked perfectly and my site restrictions are fixed. 🙂

  25. Hello. I know this is an old post. I did this but my favicon gets affected because I inserted a “body element” in the header area, as was suggested here. Any workaround? Thank you very much.

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=""> <s> <strike> <strong>

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