How Often do You Use Functions.php in Themes?

December 4th, 2009
WordPress Discussions

Before I start, I must say that this question is directed more towards advanced users, rather than users who don’t fiddle with their themes.

As a advanced user of both WordPress and PHP, I prefer to keep things to the minimal and make use of existing features rather than adding new ones that overlap.

For example, I wanted to use the API to shorten URLs and track them. Instead of using a plugin I could create for my use or use existing ones, I preferred to add a new method to the functions.php of my theme. For those who are interested, you can find the code below.

define('TB_BIT_API_USER', 'username');
define('TB_BIT_API_KEY', 'apikey');
function tb_make_bitly_url($postid, $url, $format = 'xml') {
  $shorturl = get_post_meta($postid, "shortened_url", true);
  if(!$shorturl) {
    $bitly = ''.urlencode($url).'&login='.TB_BIT_API_USER.'&apiKey='.TB_BIT_API_KEY.'&format='.$format.'&version=2.0.1&history=1';
    try {
      $httpclient = new WP_Http;
      $response = $httpclient->request($bitly);
      $xml = simplexml_load_string($response['body']);
      $shorturl = ''.$xml->results->nodeKeyVal->hash;
      add_post_meta($postid, "shortened_url", $shorturl);
    catch(Exception $e) {
      $shorturl = $url;
  return $shorturl;

Functions.php should generally be used when you just want to add a minor feature that requires a single function or few lines of code. However, you cannot use functions.php to run code in the actions such as “plugins_loaded”, which run before the theme is loaded, you can however add code to other actions which run after the theme is loaded. This is by design and should be too, as each theme will have their own functions.php file and should only perform functions that relate to displaying the website.

Theme developers can use Functions.php to include common methods like getting recent posts or comments and so on instead of including additional plugins. Of course giving due credit wherever required is of utmost importance too.

I had written a post earlier, whether or not it made difference If you use 1 plugin or 100 plugins which can give you a general idea on what benefits you can get by using functions.php for smaller functions. It is still debatable, but you can always run benchmarks to find which the best method is.

Of course, users who edit themes themselves will have a minor problem if they keep changing their themes, as functions.php is different for each theme. I usually create a new file called “customfunctions.php” and then include it in the functions.php to keep things short and sweet and reusable.

Do you prefer to add code to functions.php or prefer using plugins for small tasks? Do let me know your thoughts about it.




  1. Jess Planck (4 comments.) says:

    Definitely both.

    Sometimes I use a theme’s functions.php as the main driving force behind a specially purposed WordPress site. Although you mentioned that functions.php cannot be used for setting actions, you can set quite a few actions and filters with functions.php.

    I still reserve this for functionality that is specifically tied to the theme. Although the functions, filters, or actions may be complicated and almost plugin style code, the PHP I add to functions.php still won’t stand alone without the theme.

    Many theme frameworks use functions.php to create an extensive library of theme based utility functions, actions, and filters that can be really useful.

    I also really liked the example using a PHP try and catch. The WP_Http class is good place to do that for sure!

    • Patrick Daly (6 comments.) says:

      I agree with Jess. I only use function.php for theme-specific functions. If it’s code I’ll likely reuse elsewhere then I turn it into a plugin, though that’s rare. I use the Hybrid framework and it covers a lot of bases that make plugins less necessary. When it comes time to use a plugin for something like the API then you’re only adding to the few plugins you’re using (hopefully).

  2. King Rat (3 comments.) says:

    I never use functions.php. I like to keep my themes up to date, and functions.php gets overwritten when I do that. I much prefer plugins.

    I do like the Carrington Framework, because it has functionality for lightweight theme based plugins. I can drop in any custom functions I want into its plugins directory, and not have them overwritten.

  3. TheRegge (1 comments.) says:

    The more familiar I get with WordPress, the less do I tend to use third party plugins for small or mediums range functionality. I am certainly getting more criticall about code quality (first concern, security,nbut also coding practices that will not get me into dead ends). As far as my code is concerned, I will use functions for small stuff and then make my own plugins in order to be able to reuse code from one project to another.

  4. Glenn Ansley (1 comments.) says:

    My company does a lot of contract work for web developers that need additional functionality added to WordPress sites their developing. I usually make the decision to use functions.php or a custom plugin depending on how specific or complex the functionality is. If it’s something that I could see using over and over, I’ll often make it a plugin regardless of how simple or short the code is. If it’s site specific or something I don’t want the client deactivating through the plugin interface, I’ll place it directly in the functions file or include it via that file regardless of how complex or long the code is. functions.php is the most important file in a theme (as far as this non-designer is concerned). Thanks for the post!

  5. Keith Dsouza (82 comments.) says:

    @Jess – My bad, I meant to say that you cannot add code to actions like “plugins_loaded” which runs before a theme is loaded, however you can always call or run code on actions which run after a theme is loaded, have updated the post to reflect it now.

    Glad to see you using functions.php extensively and good to hear that you liked the example too.

  6. Lloyd Budd (22 comments.) says:

    We use ‘functions.php’ extensively with our VIP customers. In a MU environment, it can be the perfect place just to manually load plugins [include()] specific to that blog/theme/customer.

    It’s always a good idea to wrap your code in functions — name spaces are you friend, and in a rich environment like WordPress you never know what globals you are going to pick up.

    I don’t like a busy functions.php… busy is bad for debugging, so pretty quick I’ve broken what I started to put into functions.php into individual files :p

    • Ron (7 comments.) says:

      Same here. Quite often we add plugins to the MU home/main blog via the theme functions file.

  7. Ian Stewart (28 comments.) says:

    All the time. I eat my own dogfood with all this relentless Child Theme talk and functions.php—or as Lloyd mentions, a functions file that loads smaller ‘functions’ files—is your secret weapon for rapid development.

  8. Elpie (7 comments.) says:

    I always use functions.php. However, it doesn’t contain any functions – I use it specifically for things like defining directory constants and the paths to reusable code.

    These days my functions go into libraries as I reuse code across projects extensively. Theme-specific functions go into a custom functions folder.

    I find I end up rewriting or fixing up so many plugins that for regularly-used functions in themes it makes more sense to just include the functions I want in the theme library. I did some benchmarking on this last year and found a small, but significant, improvement in page loading by using custom functions instead of plugins. Some things are more suited to plugins but some plugins really should be just theme functions.

  9. Dean Saliiba (8 comments.) says:

    I do use it but I tend not to make an edits myself.

  10. cimddwc (3 comments.) says:

    I got my own theme tailored to my patricular needs, so I use functions.php quite a lot. However I decided to use it mainly for presentation-related functions, and I put other functions (for the feed, backend,…) into a single plugin which I also can reuse on another blog.

  11. Lisa Risager (1 comments.) says:

    I prefer to use functions.php instead of having to tweak plugins for my custom themes.

  12. Jay (1 comments.) says:

    All the themes I have coded including the current one on my website use a functions.php file.

    The reason is simple: I love it. It’s my way of knowing this is something specific to this theme and that’s it! If it’s a function I need to carry over to another theme, I carry it entirely.

  13. Hicham Maged (36 comments.) says:

    Always since I prefer to tweak “Functions.php” to do some work rather than having an entire plug-in; something near to your ‘’ example plus few customizations too.

  14. samuelclough (1 comments.) says:

    I end up using functions.php quite a bit. However, I try to keep all the code in it related to the display of content then logically fitting within the theme. If I’m doing something that changes the basic behavior of the site then I put it in a small custom plugin. I use plugins for behavior/processing logic and functions.php for display of content related logic.

  15. Otto (215 comments.) says:

    I use a theme’s functions.php only for adjustments that relate specifically to that theme. That way, if I switch themes, those don’t come along with me.

    For my own site specific settings, I always create an empty plugin like so:

    Plugin Name: Adjustments

    And then activate it. Then I can add snippets of code to it as required. These adjustments will survive theme changes.

  16. bubazoo (213 comments.) says:

    I can’t comment, for I use plugins because I don’t know how to use functions.php. I mean, I do have enough PHP experience to figure it out I’m sure, but its just less time consuming to use a plugin. Besides, I know basic PHP, but I don’t understand OOP, like classes, so because of that, I try not to figure out how to do functions.php or plugin writing, because I figure it probably involves using classes in OOP, I figure its much easier just to snap in a plugin, instead of taking the time to try to learn all that…

    There are times, however, that I want to take an existing plugin, and just modify its screen output a little bit. If I can do that with functions.php, then it might be worth learning I don’t know, but until I know for sure, doesn’t seem worth it yet. THere are some basic features of WP I still don’t understand yet? like pingbacks and trackbacks, I don’t understand what those are useful for? same with the tweetbacks that are on here? I don’t understand what those are useful for? took me just as long to figure out what permalinks are for? lol

  17. Uwe (18 comments.) says:

    Never used it, there was no need to.


  1. […] visiting various wordpress sites and at one occasion I read a post on functions.php vs plugin on . Of course I submitted my comment on that, but it raised an interesting question and I am sure […]

Obviously Powered by WordPress. © 2003-2013

page counter