My dream is to include a php file in a theme which checks if a set of plugins are installed, and installs the ones which are not. Kind of like a set of dependencies for the theme, but also just a good way to package theme development to include a set of good plugins.
My questions…
- Is there something like this in existence?
- Is it possible to achieve from a single php file in a theme folder?
- Are there any obvious pit-falls or problems with this approach?
- How would I go about achieving this?
- is it possible to enumerate installed plugins from within theme folder?
- is it possible to download and place plugin files in the plugins folder?
- is it possible to activate plugins from within theme directory?
07/06/2018 EDIT: If you are coming across this answer, the code highlighted below is extremely outdated and insecure and should not be used in any capacity outside of experimentation on a local server. If you are looking for a more modern solution for plugin management, consider installing WordPress via Composer and Bedrock
I would advise NOT programatically checking for the existence of certain plugins, downloading, installing, and activating them from within any theme file. You have to consider that the check will be run every time the given page is loaded, and can result in a lot of superfluous code and unnecessary activity.
Instead, my advice would be to package any plugins on which your theme depends as a part of the theme itself, and NOT as a plugin. Plugins should be installed at the discretion of the user. If a theme depends on a plugin to function properly or efficiently, then it really should be packaged and downloaded with the theme itself.
But to answer your questions directly:
Plenty of research
I cannot stress enough, however, that the purpose of a PLUGIN is to give the user the option to extend a given theme’s capabilities. If your theme’s capabilities DEPEND on existing plugins, then you really REALLY should include all the files when somebody downloads your theme.
Though if you feel that your approach benefits your theme in ways that I might be missing, feel free to write it however you like.
COMPLETE ANSWER: I decided to help create a proof of concept for you, because I got bored and curious. Much of this should be self explanatory. Add these functions:
… and then execute like so:
‘name’ can be anything, as it serves to be more of a temporary value. ‘path’ is exactly what it looks like, and is the direct URL to the zip file on the WordPress server. The ‘install’ value is simply the path to the main PHP script that has all of the plugin information. You will have to know the layout of that particular plugin directory in order to fill out this information, as this is also required for the activation hack to work.
Activation function was found here (credit to sorich87): https://wordpress.stackexchange.com/questions/4041/how-to-activate-plugins-via-code
WARNING: This is by no means a very safe way to do things. I actually think that this can be abused quite easily, so our best bet is to use this as our baseline and try and improve from there.
If you should decide to use this approach,
all I ask is that I’m credited with the initial overall script, along with sorich87 for his activation processmay God have mercy on your soul.07/06/2018 EDIT: Seriously, don’t do this. By today’s standards, this code is hot garbage. Plugin management should be done through Composer and Bedrock.
Inspired by the comment from Jamie Dixon I inspected how WordPress works.
The process can be seen in /wp-admin/update.php from line 93. A short version could be made like this:
If you don’t want the feedback displayed you should create a custom Skin class. For example: