Skip to content
Ryan Mark edited this page Oct 13, 2011 · 29 revisions

MTV is a plugin for WordPress that provides a new API for developing plugins and themes. Born out of frustration with the undocumented, inconsistent WordPress API, MTV provides a simple, familiar, consistent way to develop heavily customized WordPress sites.

MTV borrows a lot from existing MVC-style frameworks, namely Django and Backbone.js. If you're familiar with those frameworks, you should feel at home using MTV. If you're not familiar, go try some tutorials. It'll make you a better programmer.

This plugin hijacks and takes over how WordPress handles URLs and templates, and gives you new ORM-style tools for handling posts, users and blogs. This plugin does nothing by itself, and (hopefully) will not break stuff that you already have. It's just a set of tools for developers to use.

Getting started

  • Bare MTV
  • My first theme

The model layer

The model layer is a collection of classes which handle the retrieval and manipulation data. Models are objects which represent a single item or row from a database.

In MTV, two types of classes comprise the model layer: Model and ModelCollection. A Model represents a single item from the database; a post, user, category, etc., and contains all the logic for accessing data, saving to the database, validating changes, and retrieving related models or data. A ModelCollection represents an array of a single type of Model and contains all the logic needed to retrieve and filter one or more models from the database, as well as logic to work on multiple models at once.

\mtv\Model

Constructor new Model( [$attributes] )

The constructor creates a real live model for you to use. It takes one optional parameter, an associative array of attributes to attach to the model.

$data = array(
	$title = "My blog post",
	$published_on = now(),
	$author = "Bobby Tables",
	$content = "I can blog!!"
);
$model = new Model($data);

print "<h1>" . $model->title . "</h1>";
print "by " . $model->author;
print "<p>" . $model->content . "<p>";

save $model->save()

Save this model to the database.

$model = new Model();
$model->title = "My blog post";
$model->save();

The base Model class does not implement save. It's up to you to extend the Model class and write this function.

validate $model->validate()

Validate the attributes in this model. Call this before saving the model to verify that all of the attributes are correct.

$model = new Model();
$model->email_address = "bobby";
$model->validate();
// ERROR! "bobby" is not a valid email address

The base Model class does not implement validate. It's up to you to extend the Model class and write this function.

initialize $model->initialize( $attributes )

Process the attributes

  • Basics of Model, ModelCollection
    • syntax
    • API
  • WordPress Models and ModelCollections

The view layer

  • urls.php
  • Including urls from other places
  • Writing a view

The template layer

  • Available wp template functions
  • How to add a template functions, tags, filters
  • Deep links to twig

AJAX

  • Setting up ajax urls
  • Writing an ajax view
    • Calling ajax
Clone this wiki locally