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?
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 fileTypical 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.
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 :
As an addition to @MBTheDeveloper answer.
Other options would be:
include
in your themes function php file and load a custom-function.php file that contains your custom code.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.
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:
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.