Recently the question (How to add add custom functions to a theme) was posed in WordPress Support Forum. Personally I faced the same question when re-designing this site. The following is a discussion of the option with the pros and cons.

There are two solutions.
The first is to create a plugin which goes with the theme. However the theme, when invoking functions defined in the plugin should check first for the availability of the function. It should also provide a fallback option if the plugin is not available or has not been activated.

if(function_exists('your_function_name')) {
    // Invoke your function: your_function_name()...
else {
    // Execute fallback option
}

The advantages are:

  • The plugin can be independently managed.
  • The plugin can be reused for other purposes.

The disadvantages are:

  • It requires another additional step for the theme user to remember.
  • It slightly complicates development and testing.

The second solution to this problem would be to incorporate the functionaility in a php file (as usual) which resides in the theme directory. This file is included in header.php like:
include ('your_php_file.php');

Yes you may also require it for simplicity like:
require ('your_php_file.php');

The advantage to this approach is simplicity of usage and deployment by end-users. It also simplifies development.

If the user later decides to switch to a different theme and yet wants to retain the functionality, he would have to re-purpose the custom code into a plugin.

In essence the reusability of the custom functionality determines the ideal location of the custom code.