I think, the wordpress plug-in architecture is not MVC. So I want to use/do the MVC architecture in WordPress plug-in development (like cakephp, codeIgniter ). How can I use them in plguin development. Does WordPress support CakePHP, or other frameworks?

7 Answers
7

A: Yes, that's true. It's not MVC in the Design Pattern Sense. It's MVC in a more general view: Model: Mysql, View: HTML/CSS, Controller: PHP. Main Frontend Controller is index.php, Commands are HTTP requests. The router is .htaccess (if you use pretty permalinks), the 404 controller (if you use that one) or index.php if you use the standard setup.

Q: So I want to use/do the MVC architecture in WordPress plug-in development (like cakephp, codeIgniter ). How can I use them in plguin development. Does WordPress support CakePHP, or other frameworks?

A: No, Wordpress does not support CakePHP or CodeIgniter out of the box. You can write an adapter for codeIgniter or Cake so that you can re-use an existing MVC (e.g. by encapsulating the HTTP Request and removing the slashes from request variables / the superglobals). But you can overrider Wordpress as well to just run any other PHP Framework Application as well, e.g. by adding rewriterules to the .htaccess and adding php files. It just depends on what you want to do.

You could definitely use an MVC philosophy in the development of a plugin, but you would be really silly to try to incorporate CI or Cake in addition to WP.

For instance, you could build out a plugin using the following pseudo structure (FYI, there are a ton of ways to build this, depending on your interpretation of MVC. This is just the quick example that pops into mind):

This might be a horrible example of a plugin structure, or even of MVC, but it is simply to illustrate that MVC architecture !== MVC frameworks and that MVC can be used in plugin design. Widget classes and other functionality throughout WP are similar, although without any explicit separation of concerns.

I had this same question a while back, and at the moment, unfortunately, the only way to do MVC in a WordPress theme or plugin was to role your own.

So that's what I did, and I've been using it in my plugins and themes for a few months now. You can view the source on github here: https://github.com/philipwalton/PW_Framework
It's called PW_Framework, and it's loosely based on the Yii Framework, a very good MVC framework for PHP.

At it's core, PW_Framework is a quick and easy way to create options pages complete with client and server side validation. It handles the ajax validation as well as all the WordPress security issues (like nonce), so you can focus your development efforts on what your plugin actually does.

All you have to do is define your model with the attributes, attribute labels, descriptions, and options:

WordPress is pretty much framework for itself, it takes care of stuff that is usually covered by framework with own (or bundled) code. Well, technically parts of it is BackPress framework but it's really very internal and not too relevant.

WordPress extensions (plugins or themes either) use provided WP APIs to retrieve and modify data. It doesn't matter for WP what your extensions uses, as long as it uses proper API to interact with WP itself.

So basically you can include and use any framework, library or whatever in your plugin. As long as you use WP API properly and it makes sense for performance and licensing constraints.

I assume you mean using MVC as cakePHP and codeigniter do, not 'use an MVC framework' to create a plugin or theme. The second option would require users to download the framework you used as well as the extension files.