An example WordPress plugin used to show how to include templates with your plugins and programmatically add them to the active theme.
You can read more about this plugin and how it works in this blog post.
The plugin works like this:
- Upon activation, it checks to see if a template with the same filename exists within the active theme's root directory.
- If so, the plugin will not do anything; however, if no template file exists, then it will create the template in the root of the active theme.
- Upon deactivation, the template will be removed from the active theme directory.
- Navigate to the 'Add New' Plugin Dashboard
- Select
page-template-example.zip
from your computer - Upload
- Activate the plugin on the WordPress Plugin Dashboard
- Extract
page-template-dashboard.zip
to your computer - Upload the
page-template-dashboard
directory to yourwp-content/plugins
directory - Activate the plugin on the WordPress Plugins Dashboard
Because this plugin is used strictly for example purposes, there are a number of things that are not properly handled:
- If the user changes a theme, the plugin does not move the template to the new theme's directory.
- There's practically no error handling for the file operations.
- If a template with the same file name already exists, there's no notification or error handling in this case so the user has no idea that the bundled template was not activsated.
- …I'm sure there are more.
This particular plugin is tagged as 0.1 and is open to any and all pull requests to help make this better, so feel free to contribute to resolve any of the above issues or any that you experience.
7 March 2013
- Initial release