WordPress Plugin Development In MVC Architecture, How?

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?

Related posts

Leave a Reply

8 comments

  1. 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):

    class Plugin(){
        function __construct(){ // controller?
            run conditionals/determine query
            process model
            process view
        }
        function model(){ 
            get query from database 
            return variables
        }
        function view(){
            echo html
        }
    }
    

    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.

  2. You asked multiple questions at once:

    Q: I think, the wordpress plug-in architecture is not MVC.

    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.

  3. 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:

    public function data()
    {
      return array(
        'name' => array(
          'label' => 'Name',
          'default' => '',
        ),
        'email' => array(
          'label' => 'E-mail',
          'default' => '',
        ),
        'sex' => array(
          'label' => 'Sex',
          'default' => 'male',
          'options' => array( 'm' => 'Male', 'f' => 'Female'),
        ),
    
      );
    }
    

    Then you define the validation rules:

    protected function rules() {
      return array(
        array(
          'properties' => 'name, email, sex',
          'validator'=> array('PW_Validator', 'required'),
        ),
        array(
          'properties' => 'email',
          'validator'=> array('PW_Validator', 'email')
        ),
        array(
          'properties' => 'sex',
          'validator'=> array('PW_Validator', 'match'),
          'pattern' => '/^m|f$/'
        ),
      );
    }
    

    And the view would look something like this:

    <?php $form = new PW_Form( $model ); ?>
    <?php $form->begin_form(); ?>
      <?php $form->begin_section('Year Options'); ?>
      <ul>
        <li><?php $form->textfield( 'name' ); ?></li>
        <li><?php $form->textfield( 'email' ); ?></li>
        <li><?php $form->radio_button_list( 'sex' ); ?></li>
      </ul>
      <?php $form->end_section(); ?>
    <?php $form->end_form(); ?>
    

    Then in the controller, you simply write all of your code functionality.

    Here’s a working example of it in live use: http://wordpress.org/extend/plugins/pw-archives

  4. 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.

  5. 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.

  6. I think I am late to the party but if anyone is still looking for an answer, I have been working on MVC boilerplate for WordPress plugin development for some time. This boilerplate aims to separate concerns between Model, View & Controller.

    With this boilerplate, developer gets a chance to write individual Model, View & Controller classes. Also, the concern of whether to load a controller/model or not is delegated to Router, so that your controller & model can focus only on what they are supposed to do. I believe this would help in keeping the footprint small.

    This is not a full fledge MVC Framework, it is just a boilerplate allowing developer to write code in MVC style. Because of this, it has only those features which are required to build plugin in MVC way – No ORM – No Extra Goodies – No Huge Learning Curve.

    Here is the link to a project: https://github.com/sumitpore/mvc-plugin-boilerplate-for-wordpress