Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there some global kind of functions.php file that works for any theme?

The problem is here:

When I change a functions.php file in any theme, I need to take care of two changes: First, I need to take care of the updates of that theme. Second, I need to take care of changing the theme of the site.

So, instead of making changes in the functions.php of a theme, is it possible to make changes in some functions.php file that is independent of any theme?

share|improve this question
4  
Is the bounty really necessary? The question already has good answers –  onetrickpony Oct 16 '11 at 21:23
    
As @Mohit Bumb was the one giving a) the bounty and b) was the first who suggested using a plugin, I'd say he should be the one getting the rep points (back). IMO a bounty wasn't necessary (and I guess he didn't know that he had to actually give it away). –  kaiser Oct 17 '11 at 11:15
add comment

5 Answers 5

up vote 10 down vote accepted
+300

The difference between theme and non-theme code is organizational rather than technical. Any code that is active contributes to resulting environment, does not matter where it is loaded from.

There is number of places where code gets loaded from, which are not part of WordPress core:

  • wp-config.php configuration file
  • active theme (and its parent in for child themes)
  • active plugins
  • must use plugins
  • drop-ins (these are somewhat advanced and serve very specific purposes)

Typical place for your own code, that should not be part of theme, is to create a plugin. Other approaches do not have benefits from generic case, but forfeit interface (managing through admin area) and technical (activation/deactivation/uninstall events) conveniences of normal plugin.

share|improve this answer
    
+1 to both you and @kaiser. I believe the correct answer is 1) Create a Child Theme to house the custom functionality in functions.php, or 2) create a custom Plugin to house that functionality, so that it is portable among various Themes. –  Chip Bennett Oct 16 '11 at 21:08
    
@Chip since question asks about preserving code when switching themes I don't see child theme as fitting. What if you want to switch to theme that is child theme? Plugin is most appropriate for given requirements. –  Rarst Oct 16 '11 at 21:10
    
that's why I up-voted both of you. :) But, you can still use a Child Theme when switching Themes; simply change the Template: tag appropriately in the Child Theme's style.css. –  Chip Bennett Oct 16 '11 at 21:13
    
@Chip you didn't get my point. What if I want to use child theme? I create child theme for Twenty Eleven with my code. Then I want to switch to theme that is also child theme of Twenty Eleven (or other parent). I have no path, other than manually merging two child themes. Then un-merging next time I change theme. It's a mess. –  Rarst Oct 16 '11 at 21:17
add comment

Well that is a real problem and many peoples facing it when they change theme but there is a simple solutions.

add that function to a new page not functions.php and give it a unique name for example yousitename+timestamp or whatever you want but unique create folder of same name zipped it and upload it as a plugin so it will surely works until you disable it.

To understand which plugin is for what don't miss to give name to that plugin like :

/*
Plugin Name: XYZ
*/
share|improve this answer
add comment

As an addition to @MBTheDeveloper answer.

Other options would be:

  1. Create a child theme for your current theme and use the functions.php file from this one (this is "best practice").
  2. Use include in your themes function php file and load a custom-function.php file that contains your custom code.
share|improve this answer
add comment

Don't make your site specific changes in the theme's functions.php file.

Instead, create a site-specific plugin. I like to use the site's domain name, like "ottopress.com". Then put your snippets into that plugin and leave it active on that site only.

Alternatively, when you have a snippet of related changes, create a specific plugin for just them. That gives you the option to turn on/off related bits of code as needed.

share|improve this answer
add comment

Some Explanation

First you need to understand the purpose of functions.php in a theme. Functions.php is basically just a plugin file with out any name which if present is automatically loaded by Wordpress. Wordpress loads your current theme's function.php for wordpress front-end pages and as well as back-end (admin) pages.

There is no different between a plugin and functions.php except that plugins have name and they can be activated/de-activated irrespective of other plugins and themes. The funtions.php is attached with your current theme and if you switch to another theme Wordpress uses new theme's functions.php and not your old functions.php with custom modifications.

Theme updates also overwrite functions.php and there is not way to avoid it, even if you put your custom modifictions in separate code files and include it in your fucntions.php, you will have to add that include line after every update.

Generally the type of code that should go in functions.php:

  • functions that are used in your theme's template files.
  • theme options
  • sidebars
  • navigation menus
  • post formats
  • post thumbnails
  • any code that is only related to the current theme.

As you want to retain your custom modifications even if you switch themes. Because child theme will not work in this case as you will switch the theme and the functions.php of the child theme will not be used.

The Solution

The only solution as recommended by @Otto is to create a plugin file. The approach you can take to create your site specific plugin is to create a folder with your sitename in 'wp-content/plugins' and create plugin files in it.

Why plugin files? and not single plugin file.

Group related site's functionality related to code in separate plugin files and don't forget to add the plugin header in all your plugin files so they are recognized by Wordpress as separate plugins. You will then have the ability to activate/de-activate different features/functionality of your site without breaking everything.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.