The MU forums have moved to WordPress.org

Hack: Main Site Only Plugins (43 posts)

  1. lunabyte
    Member
    Posted 17 years ago #

    Ever wanted to have plugins that only execute for your main site, so that you aren't running excessive amounts of code not being used elsewhere?

    Here's a quick "hack" to do mu-plugins style. If a plugin works as an mu-plugin, it will have the same results with this.

    1) Open up wp-settings.php, and look for the following around line 211

    $plugins = glob( ABSPATH . 'wp-content/mu-plugins/*.php' );
    if( is_array( $plugins ) ) {
    foreach ( $plugins as $plugin ) {
    if( is_file( $plugin ) )
    include_once( $plugin );
    }
    }

    Add after:

    if ( $blog_id == 1 ) {
    $plugins2 = glob( ABSPATH . 'wp-content/my-plugins/*.php' );
    if( is_array( $plugins2 ) ) {
    foreach ( $plugins2 as $plugin2 ) {
    if( is_file( $plugin2 ) )
    include_once( $plugin2 );
    }
    }
    }

    Now you can drop plugins into that dir, and they'll only execute for your primary site.

    Pretty handy for testing, or just running the extras on the primary site only.

    It could also probably be tweaked to run plugins for another blog_id, vice using the global plugin manager. Mainly because the more code you can cut down being read into memory, the better as your site gets bigger. Every little bit helps, right?

  2. andrea_r
    Moderator
    Posted 17 years ago #

    Oh, I know many people have asked how to do something like this. :)

  3. dizzy99
    Member
    Posted 17 years ago #

    Handy tip.

    What i do is copy the plugin contents into the function.php of the theme i'm using for my landing area. That works the same way.

    Then i just call the function within my page without having to globally load the plugin for everyone.

  4. lunabyte
    Member
    Posted 17 years ago #

    True, but handling rather large plugins can get messy dizzy.

    Imagine you wanted to add SK2, but only for the main site, as an example of a large plugin not as of an actual use.

    Maybe you want to add Tiger Admin, or Spotmilk? Way too much to edit and keep track of.

    I do agree that for really basic ones, functions.php does work. But it's also easier for a server to parse 10 small files than 1 really big one too.

    Heck, WP could be merged into a single file format too. Just think of the load that would stick on the box. lol

  5. dizzy99
    Member
    Posted 17 years ago #

    Ah yes agreed. I was thinking more of the global MU plugins i use. Which at this point is show all themes and Show All blogs. So hadn't encountered that issue.

    But its a valid point of course.

  6. andrewbillits
    Member
    Posted 17 years ago #

    For those who like to keep their install up to date with the nightlies, this could easily be made into a plugin. It's much easier to remember to drop a plugin into mu-plugins than remember to add the code each time.

    For those who just upgrade with each major release, I'd just follow lunabytes instructions above.

  7. lunabyte
    Member
    Posted 17 years ago #

    Valid point Andrew. The same code dropped in an mu-plugin should produce the same results. Hadn't thought of that, really.

  8. andrea_r
    Moderator
    Posted 17 years ago #

    I think your to-do list just got one item longer. :)

  9. lunabyte
    Member
    Posted 17 years ago #

    Awww darn it. lol

  10. skcsknathan001
    Member
    Posted 17 years ago #

    HI guys,
    may be this is too simple.
    What hook to use to make this code as a plugin? I mean to insert this code to the core file need to use a hook in the plugin file right.

    Another thing, you know if you update your code nightly try using the tortoiseSVN program. You just right click the folder [htdocs] and select Update or Update to... and select the revision from the WPMU trunc and the program will do everythign for you. It will add new files, delete unnecessary files, and merge files that you have modified. There could be sometimes, it couldn't merge automatically, then it will show the error message and you can click the Compare and see the changes. it highlights whats in conflict and it gives you the choice to automatically select which text and all.

    Yet, I like to keep the core files unmodified as possible.

  11. lunabyte
    Member
    Posted 17 years ago #

    That's the point of this though.

    To keep from running as much unnecessary code as possible.

    Yeah, it could be an mu-plugin file, but it would be less efficient too. Not a whole lot, but a little.

    Oh, and I wouldn't recommend merging to a live site. But, to each their own.

  12. skcsknathan001
    Member
    Posted 17 years ago #

    Of course I do not merge a live site.
    I merge to my dev, click upgrade, see if it works. then upload the site to live and click upgrade.

    Sorry, I still don't know what hook to use for the code you specified above Lunabyte. I create a file inside mu-plugins with the code above. But what hook to use to insert the code in the file coding?

  13. lunabyte
    Member
    Posted 17 years ago #

    Hook? What hook?

    Why would you need one?

    Any php file in mu-plugins is read in, and executed on every click.
    So essentially you'd be doing the same thing in the file.

    Of course you'll have to call blog_id as a global, then check it, and add in security stuff.

  14. Konshu
    Member
    Posted 17 years ago #

    I know your going to love this Lunabyte but im going to open my big mouth here since I used the search and still am having issues with this related topic. I drop plugins I only want the main site to use in /mu-plugins/ directory but the site doesn't seem to pick this up. Am I reading the information incorrectly that the directory does the same thing mentioned above? Is there a chance that a lot of the more common plugins are not available for this? A big one I have is the bbpress integration plugin that is only needed by the main site. Same goes for the King Framework and King Login plugins. Advice in maybe what I am missing would be helpful as I would rather not expose every new user to plugins that I don't want or need them to have.

  15. lunabyte
    Member
    Posted 17 years ago #

    First, mu-plugins is "sitewide". Everybody gets their use, automatically, no if's and's or butt's.

    That's why I shared my little hack.

    Some plugins are coded in a "less than desirable for MU" fashion. Meaning they hard code paths and links to the plugin directory.

    So, for any plugin you wish to use in either mu-plugins (which I don't use much there to be honest), or using my above little "hack", and they don't seem to "work", that's the first place to look.

    The fix for any specific plugin varies by plugin, so you'll have to find out what and how it calls itself.

    I hope that's what you were asking.

    In case anyone is looking, the code in wp-settings is about line 224 or so now.

  16. demonicume
    Member
    Posted 17 years ago #

    thie upgrade to 1.2 seems to have broken this.

  17. lunabyte
    Member
    Posted 17 years ago #

    Did you add it back into the core file?

    It may have changed, I'm not sure. I'd have to look. But if you upgraded, and didn't re-add the hack... then yeah, it's going to break.

  18. demonicume
    Member
    Posted 17 years ago #

    yeah, that would be a big DUH! my bad. it works great. i dont know why more people dont use this hack. i tossed exec-php in there so that i only could use PHP. this is the ideal fix! now when i add RSS feeds from simplepie, etc, i dont have to mess with the templates at all.

  19. andrea_r
    Moderator
    Posted 17 years ago #

    "A big one I have is the bbpress integration plugin that is only needed by the main site."

    Now I'm confused - you don't need a plugin to integrate bbpress and it's only off the main site anyway.

  20. enseignement
    Member
    Posted 17 years ago #

    Can't make this work since I've updated. Did anyone succeed ?

  21. demonicume
    Member
    Posted 17 years ago #

    works for me just fine

  22. lunabyte
    Member
    Posted 17 years ago #

    Actually Andrea, at least when I did the integration on that particular setup, there certainly was a plugin you needed to add to WP. I believe it was to sync users, either one way or both, which happens on registration or every so often.

    I haven't had the chance to look at 1.2.x yet, just been too busy. I'm hoping by one night next week I can, but from the notes I looked at it should still be a valid "adjustment" to the file.

  23. enseignement
    Member
    Posted 17 years ago #

    demonicume,

    How did you do ?
    When I copy/paste the code above, I got an error.

    Thanks

  24. nolageek
    Member
    Posted 16 years ago #

    I wish wpPHPMailer would work in this directory.... i really dont want all users being able to have access to it. anyone have a work around?

  25. demonicume
    Member
    Posted 16 years ago #

    enseignement- i didnt have to do anything special to make this work.

  26. mram48
    Member
    Posted 16 years ago #

    Im no php programmer so most of this is greek to me, but if I'm reading this right I should be able to load my logo for the main site, but all of the subsequent blogs won't have the same logo? Or have I way over simplified this and missed a totally different plugin for that? I'm guessing the later...I always miss em. :\

  27. demonicume
    Member
    Posted 16 years ago #

    wait. which logo are you talking about?

  28. mram48
    Member
    Posted 16 years ago #

    My own logo for my own site. I want to put my logo on my site, but not on everyones blog that joins. I am running wpmu in case you're wondering.

  29. Ovidiu
    Member
    Posted 16 years ago #

    :-) usually if you want your logo on your site, you edit your theme and don't make it available to anyone else...

  30. lunabyte
    Member
    Posted 16 years ago #

    What a logo has to do with plugins, I have no idea. ::rolleyes::

About this Topic

  • Started 17 years ago by lunabyte
  • Latest reply from lunabyte