Description
This took me a while to figure out, so I thought I'd write some notes in case any of it may be useful.
I wanted to bundle some plugins/themes in wp.data
, for several reasons: to cache the whole bundle, without having to make requests to wp.org on every page load; and to run plugins that are not published to the directory, for example pre-release versions or commercial products. It's also for a curated demo experience, so users won't be able to install arbitrary plugins, only the pre-bundled ones.
In src/wordpress-playground/wordpress/Dockerfile
, I added a step to bundle the plugins.
Before the line with comment "Separate WordPress static files":
RUN rm -rf wordpress/wp-content/plugins/* && \
mkdir -p wordpress/wp-content/mu-plugins && \
cd wordpress/wp-content/mu-plugins && \
# Install plugins
for plugin_name in example-1 example-2; do \
plugin_file=$plugin_name.latest-stable.zip && \
wget https://downloads.wordpress.org/plugin/$plugin_file && \
unzip $plugin_file && \
rm $plugin_file && \
# Create entry file in mu-plugins root
echo "<?php require_once __DIR__.'/$plugin_name/$plugin_name.php';" > $plugin_name.php; \
done
So that part works, and the plugins are activated on site load.
The issue was on the frontend, where their static assets were not being found. I tracked it down to the service worker, where it calls isUploadedFilePath
to check if a requested URL path should be rewritten to the static assets directory. It was expecting all plugin assets to be in the virtual file system in the browser, not as actual static files on the server.
In my case, the bundled plugins are in the mu-plugins
directory, and users are expected to use only the bundled theme - so I was able to solve the issue by commenting out some lines in that function.
export const isUploadedFilePath = (path) => {
return (
path.startsWith('/wp-content/uploads/') ||
path.startsWith('/wp-content/plugins/')
// || path.startsWith('/wp-content/mu-plugins/')
// || path.startsWith('/wp-content/themes/')
);
};
I'm not sure if there's a better solution, if the service worker can recognize the difference between a "bundled" plugin/theme (with static files on the server) and those that are "pre-installed" on site load (with static files in the browser's virtual file system).