Wordpress - How to structure a plugin
Note that plugins are all "controllers" by WP standards.
It depends on what the plugin is supposed to do, but in all cases I would try to separate the screen output from the PHP code as much as possible.
Here's one way to do that easily - first, define a function that loads the template:
function my_plugin_load_template(array $_vars){
// you cannot let locate_template to load your template
// because WP devs made sure you can't pass
// variables to your template :(
$_template = locate_template('my_plugin', false, false);
// use the default one if the theme doesn't have it
if(!_$template)
$_template = 'views/template.php';
// load it
extract($_vars);
require $template;
}
Now, if the plugin uses a widget to display data:
class Your_Widget extends WP_Widget{
...
public function widget($args, $instance){
$title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
// this widget shows the last 5 "movies"
$posts = new WP_Query(array('posts_per_page' => 5, 'post_type' => 'movie'));
if($title)
print $before_title . $title . $after_title;
// here we rely on the template to display the data on the screen
my_plugin_load_template(array(
// variables you wish to expose in the template
'posts' => $posts,
));
print $before_widget;
}
...
}
The template:
<?php while($posts->have_posts()): $posts->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; ?>
Files:
/plugins/my_plugin/plugin.php <-- just hooks
/plugins/my_plugin/widget.php <-- widget class, if you have a widget
/themes/twentyten/my_plugin.php <-- template
/plugins/my_plugin/views/template.php <-- fallback template
Where do you put your CSS, JS, images, or how do you design the container for the hooks is less important. It's a matter of personal preference I guess.
It depends on the plugin. This is my basic structure for nearly every plugin:
my-plugin/
inc/
Any additional plugin-specific PHP files go here
lib/
Library classes, css, js, and other files that I use with many
plugins go here
css/
js/
images/
lang/
Translation files
my-plugin.php
readme.txt
This would be something that would go in the lib
folder.
If it's particularly complex plugin, with a lot of admin area functionality, I'd add an admin
folder to contain all of those PHP files. If the plugin does something like replace included theme files, there maybe a template
or theme
folder as well.
So, a directory structure might look like this:
my-plugin/
inc/
lib/
admin/
templates/
css/
js/
images/
lang/
my-plugin.php
readme.txt
IMHO, the easiest, most powerful, and most maintainable route is to use an MVC structure, and WP MVC is designed to make writing MVC plugins very easy (I'm a little biased, though...). With WP MVC, you simply make the models, views, and controllers, and everything else is handled behind the scenes for you.
Separate controllers and views can be made for the public and admin sections, and the entire framework takes advantage of many of WordPress's native features. The file structure and much of the functionality is exactly the same as it is in the most popular MVC frameworks (Rails, CakePHP, etc).
More info and a tutorial can be found here:
- Plugin page
- Tutorial