I have a wordpress setup that I use for clients with around 30 or so child themes that they can choose from. Each child theme has it's own functions.php file currently. Is it possible to setup just one child functions.php and call it from all the separate child themes? My reason for wanting this functionality is I find myself continually making changes to the functions.php and I have to do it to all 30+ themes every time.
4 Answers
Well, I'd say that you need a custom plugin. All the rationale is in this Q&A:
Where to put my code: plugin or functions.php?
Also related:
- Where do I put the code snippets I found here or somewhere else on the web?
- Create a Functionality Plugin Instead of Using Functions.php
And answering to the Question, create the following file /wp-content/themes/common-functions.php
, and paste what you need in it. And inside the child theme functions.php
use:
require_once ( TEMPLATEPATH . '/../common-functions.php' );
or
require_once ( WP_CONTENT_DIR . '/themes/common-functions.php' );
-
1I was going to say put it above the web root, but you this works too. Commented May 1, 2013 at 20:13
Use a plugin for that. Hook into after_setup_theme
and check if the current theme is the correct child theme:
add_action( 'after_setup_theme', 'common_child_theme_functions' );
function common_child_theme_functions()
{
if ( 'Twenty Twelve' !== wp_get_theme()->parent() )
return;
// do your work
}
wp_get_theme()
returns a WP_Theme
instance, and parent()
either the parent theme name or FALSE
.
I do not recommend a mu-plugin. Real plugins can be deactivated, and you can enable or disable them for each site separately. Adjust the capabilities for the clients instead; this is a much cleaner approach.
I'd use a must use plugin. The code will apply across the network while your clients won't have access to it so they won't be able to disable or modify it.
To proceed you just have to put your code in one single file and put it in /wp-content/mu-plugins/
Hope this will help :)
-
1Sorry I was writing this and s_ha_dum post the same answer before.– JMauCommented May 1, 2013 at 20:23
-
There's something that needs to be taken into account: running the mu-plugin only in specific sites, otherwise it runs in all of them. & welcome to WPSE :) Commented May 1, 2013 at 20:57
-
Thanks brasofilo. You're right but Tory wanted it for all. Maybe if for some reasons one or more sites need to be excluded it is easy to put this in a if statement like :
global $blog_id; if($blog_id !== ...etc)
– JMauCommented May 1, 2013 at 21:04
You can include
just about any file you want from any of the child themes, but there is little known feature to WordPress called Must Use plugins. I think I would lean towards that.
- Create a directory called mu-plugins in
wp-content
- Create a file in that directory and add your functions to it.
Unlike ordinary plugins, must-use plugins do not show up in the plugin list and cannot be deactivated, other than deleting the mu-plugin file, so they can serve as a kind-of global functions.php
.