WordPress

Integrating Plugins With Your WordPress Themes Using functions.php

Have you ever been browsing through the theme viewer or checking out the daily theme release announcements over at WeblogToolsCollection.com, and noticed that a certain theme requires a plugin to function correctly (or at least has a feature that requires a plugin to work)? I certainly have, and although I have never required a plugin for any themes I’ve released, I can see why one would want to include the functionality of a plugin to make that theme even more awesomely cool (and perhaps get more downloads).

But it is generally frowned upon to require the user to download and install a plugin in order for your theme to work properly. So what is a designer to do? Of course, he could just try to figure out what the plugin does by examining the code, but most designers don’t have a working knowledge of PHP/MySQL. Another option is to just remove the feature from your theme, but that means you lose that super-cool feature that might have set your theme apart from the others.

Thankfully, there is a solution that is actually quite simple.

A while back, I was working on a custom design for a client, and he requested a very long list of features that were beyond what I could do with a vanilla WordPress install. So naturally, I hit google up for any plugins that would give me the functionality he was looking for. But again, I wanted to deliver a fully functional theme to this client, without having to instruct him to install a list of plugins to get the theme working properly.

After thinking for a while, it hit me: just copy the code from the plugin into the functions.php file of the WordPress theme. I did so, and it worked like a charm! Absolutely no plugins were activated, and I was able to deliver a theme that worked perfectly right out of the box.

A few possible limitations to this method.

I’m not sure about this, but some plugins do add new rows/columns/tables to your database. There is a possibility that a plugin will do this upon activation, and may not work as an integrated function in the functions.php file. Again, I’m not sure about this.

Also, plugins with multiple files, especially plugins that require you place files/folders in multiple locations, probably won’t be easily integrated into your theme. Plugins that only require a single file upload are best.

A few extra tips

  1. Be sure to keep the plugin information in tact when copying the content of a plugin file over to your functions.php file. This way you can keep track of which plugins are installed, and where one ends and another begins.
  2. Be sure to copy the entire contents of the plugin file to your functions.php file. If you’re missing an opening or closing tag anywhere, it could break your theme.
  3. Use obvious separators when integrating multiple plugins. This is sort of like point 1, but you’ll definitely want to use a consistent and obvious marker for when a plugin begins and ends.
  4. Test, Test, TEST!!! Do not just slap the code in last minute and expect it to work. Test it extensively and make sure that your theme doesn’t break. Plugins are buggy sometimes, so you need to make sure it’s working properly before you release it or make it live.

One Final Tip

If you are comfortable enough with PHP, it should be noted that you can put the plugin files in a folder within your theme folder, and simply use your functions.php file to include those plugin files. I like to keep the php calls to a minimum, but you’re certainly welcome to go that route if you are more comfortable with it.

Anyway, I thought that was a cool little tip, and hopefully so will you.

Happy Coding!

Author: nathanrice

11 thoughts on “Integrating Plugins With Your WordPress Themes Using functions.php

  1. Hey friends
    I am quite a n00b. I have read that plugins slow down wp. So does integrating plugins to functions.php helps speeding up or just decreases number of plugins?
    I mean is there any added benefits or is this simply a way to customize a theme?
    thanks.
    Dr Bikash

  2. pdatronix,
    that’s not a problem. I was talking about the functions.php in the theme folder, yes. Sorry if I was unclear 🙂

    Nathan

  3. I think I misunderstood :# You we’re talking about the functions.php in the theme folder, not the functions.php in wp-includes 🙁 Sorry for misleading the discussion .. I stand corrected completely (but I dugg the post anyway 😉 )

  4. alimadzi,
    that’s a great tip! I’ve seen plugins that do that exact thing on their options page. Of course, this would require checking the code, and being pretty familiar with php/mysql. Most people aren’t, so I would still suggest not doing this with plugins that require editing the database. In fact, you don’t have to do this at all. As you said, it may not be a good option for everyone out there. But is still a nifty option, nonetheless 🙂

  5. Not at all. That’s one of the reasons I suggested you mark the sections of the functions.php file clearly for the different plugins you put in them. If an upgrade comes out, you just replace the old text with the new. Also, keep in mind that upgrades aren’t necessary unless a WP upgrade causes the plugin to break. In such a case, I would gladly upgrade the plugin for free for a client.

    If you’re worried about upgrade issues, just use the functions.php file to do a php include for the plugins. That way, if a WP upgrade causes it to break, you can either delete it or replace it with an updated plugin.

    The main point was, if you are going to have a theme that relies on plugins, it’s best to include those plugins in the theme folder. Whether you do that by pasting them in the functions.php file or calling them from the functions.php file, is really up to you.

  6. In my experience, people tend to update their themes much less frequently than their plugins. This is partly due to the fact that people customize their themes in many cases. So I’m not sure this is the best way to deliver added functionality to a WordPress blog.

    That being said, I can think of a way to implement database changes using this method. You can add a PHP if block at the beginning of the plugin code that tests for one of the changes to see if it has already been made. If it has been made, the block is skipped and the plugin runs normally. If the change has not been made yet, then the code inside the if block will execute and make the necessary database changes. The only performance hit would be one extra query to test for the changes.

  7. Isn’t this very hard to upgrade, for both the plugins used and newer WordPress versions? Or do you have an agreement with the client to keep his install updated?

  8. It’s been submitted to digg, so if you found it diggable, be sure to head over and digg it.

Comments are closed.