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
The Page Template Example is licensed under the GPL v2 or later.
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
For information about changes related to the project, be sure to review the ChangeLog.
The templating mechanism has changed drastically in 1.0.0
. In order to add templates to your theme using this plugin,
you must do the following:
- Review Lines 91 - 94 in
class-page-templates.php
. - Notice that we're defining templates in an associative array - the example templates exist in the
templates directory
.
To implement your own template, you must:
- Add the template to the
templates directory
- Update the associated array to include the key/value pair necessary for updating the page attribute dropdown.
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.0 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.
Page Template Example originally started and is maintained by Tom McFarlin, but is constantly under development thanks to the contributions from other WordPress developers.