How do I add JavaScript that will execute on all my sites in Multisite?

We are creating an internal network of sites using MU and there are some small things we would like to have globally on all the sites without having to add them individually for each site. I would like to add a block of JavaScript and CSS that will execute on every site. What’s the best way of going about this?

Related posts

2 comments

  1. Your answer, Kyle, is the mu_plugins folder. Create it within wp-content. Any php file put in here will be automatically loaded into ALL multisite blogs (or any non-multisite blogs).

    This is a great area to put plugins vital to the site working, or shared functionality such as custom post types, taxonomies, filters, etc. Really anything! The main bonus here is when updated said “shared” files, you don’t have to do it in every single theme that utilizes it! It’ll be available for all themes, on all sites.

    If you want to sort your stuff within mu_plugins just put everything in a nice folder structure, and create a index.php or load.php in the root of mu_plugins where you can load everything in.

    From there, wp_enqueue_script just as you normally would!

  2. You should be able to register these styles and scripts in a shared plugin file, and enqueue them from within the site functions.php. Alternatively, you may attach them in the individual functions.php in the site themes you desire. In that case, remember to wrap them in conditionals in case they are not activated, found, or should you wish to manage them through some other mechanism.

Comments are closed.