1

Here's the situation:

I've created a child theme that enhances a popular parent theme. The enhancements include CSS, JS, and php. I want to use this child theme on multiple sites, and I want to allow for additional customization on each site without editing the child theme I created.

A few requirements:

  • Each site uses the same base theme and a standard set of customizations (my child theme), but each site can have its own additional customizations (CSS, JS, and php).
  • I don't want to create my own base theme because I want to avoid that level of maintenance (security updates, compatibility, etc).
  • I'd like to avoid customizing the child theme for each site. That way I can continue to develop my child theme and easily deploy updates without overwriting site-specific customizations.

My current solution is to create a directory outside the child theme that includes the CSS, JS, and php files for customizations, and then reference those in the child theme. But this is non-standard at best and possibly a security risk.

So what would be the best approach for this situation?

Edit: I don't now ahead of time what customizations are needed. So the system needs to be very flexible and open-ended.

1

My current solution is to create a directory outside the child theme that includes the CSS, JS, and php files for customizations

This is a plugin, a folder containing PHP/JS/CSS, with a comment at the top of one of the PHP files that has /** Plugin Name: XYZ at the top of the file.

<?php
/**
 * Plugin Name: XYZ
 */

and then reference those in the child theme.

This isn't needed. You can provide a hook/action/filter or use the existing hooks and filters WP provides. Including files directly from outside of the themes folder is bad practice and should never be necessary. In the long run it causes maintenance problems.

Edit: I don't now ahead of time what customizations are needed. So the system needs to be very flexible and open-ended.

If you don't know then you can't know what to do, but that's okay. At some point you will need to update the child theme, and adding hooks is always good. The parent theme might do this too. Otherwise it's an unreasonable requirement, you can't know what you don't know.

Custom JS and CSS

If you want to add extra javascript files and stylesheets to a theme without modifying the theme, you can do that in a plugin. Just enqueue the styles and scripts as you normally would.

WordPress doesn't see any difference between a style enqueued by a parent theme or a plugin, or itself. It's all just code that has been loaded into memmory. There is no such thing as a plugin script, or theme style, and no sandboxing, so nothing prevents you adding extra styles and scripts this way.

Perhaps if you wanted to add inline code tags I could understand, but there are APIs for this, e.g. wp_add_inline_script. If you really had to add it directly, there's hooks such as wp_head of wp_footer etc to do it on.

Custom PHP

A plugin is by definition custom PHP. You can execute any custom PHP using a plugin.

If you wanted to replace a template with a new template from your plugin, you can do that too with the right filter. WooCommerce does it, lots of plugins do it, overiding specific templates or adding subfolders.


So use a plugin!

1
  • Thanks Tom, that makes sense. Really appreciate the detailed answer.
    – Diego
    Mar 22 at 14:26
3

Firstly, I would ensure that your child theme is well equipped with hooks to accommodate the potential customizations.

Secondly, I'd create a site-specific plugin - specific, that is, to the site to be customized - which takes advantage of the theme's hooks to make the required changes. This will be immune to theme updates and will correctly integrate into the standard WordPress architecture.

4
  • Thanks vancoder. The only issue I see with that – and maybe I'm misunderstanding – is that I would need to know what customizations are needed ahead of time in order to create the appropriate hooks. I want it to be more open-ended than that. By having the child theme simply reference three external files (CSS, JS, and PHP) I think I would have more flexibility. Does that make sense or am I misunderstanding?
    – Diego
    Mar 22 at 13:28
  • @Diego if all you need is 3 external files, what's stopping you enqueing 3 external filters? Or even just 2? Afterall a plugin can enqueue JS without any hooks being added to the theme. Actually it can do the same with CSS so just 1 filter. A plugin is itself written in PHP so no hooks at all. A plugin can do what you want.
    – Tom J Nowell
    Mar 22 at 14:02
  • @vancoder, I appreciate the answer, will keep that in mind for other projects. I'm marking Tom's answer as the accepted answer because it seems to fit my needs a little better.
    – Diego
    Mar 22 at 14:38
  • Tom's solution is the same as mine, though his answer is certainly more comprehensive.
    – vancoder
    Mar 22 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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