Fork me on GitHub

Tutorial

In this tutorial, we'll create a plugin called "Venue Listing" that lets you manage and display venues.

View alone

Create the Plugin

We'll use the code generation utility wpmvc to create the initial code of the plugin. You'll need to have access to the command line to do the following.

Go into the WP MVC directory and make sure that wpmvc is executable.

cd path/to/plugins/wp-mvc

chmod +x wpmvc

Generate the plugin's initial code.

./wpmvc generate plugin VenueListing

This will generate files in path/to/plugins/venue-listing/ that create a WP MVC-based plugin. The plugin can now be activated in WordPress, but before activating it, we'll want to add code for creating a database table during its activation.

View alone

Create the Database Table

To create a table to store our venues, open up plugins/venue-listing/venue_listing_loader.php. The activate() method is called when this plugin is activated, so we'll want to create our table here using WordPress's dbDelta() function. Add a few lines to activate() so that it looks like this:

<?php
function activate() {

  // This call needs to be made to activate this app within WP MVC
  
  $this->activate_app(__FILE__);
  
  // Perform any databases modifications related to plugin activation here, if necessary

  require_once ABSPATH.'wp-admin/includes/upgrade.php';

  add_option('venue_listing_db_version', $this->db_version);
  
  // Use dbDelta() to create the tables for the app here
  
  global $wpdb;
  $sql = '
    CREATE TABLE '.$wpdb->prefix.'venues (
      id int(11) NOT NULL auto_increment,
      name varchar(255) NOT NULL,
      url varchar(255) default NULL,
      description text,
      address1 varchar(255) default NULL,
      address2 varchar(255) default NULL,
      city varchar(100) default NULL,
      state varchar(5) default NULL,
      zip varchar(20) default NULL,
      PRIMARY KEY  (id)
    )';
  dbDelta($sql);
  
}
?>

Now activate the plugin, and the table will be created.

View alone

Create the Models Views and Controllers

Go back to the command line, and in the plugins/wp-mvc directory, generate scaffolding (initial code for a resource's model, views, and controllers) for the Venue resource:

./wpmvc generate scaffold VenueListing Venue

This will create a model, views, a public controller, and an admin controller for venues in plugins/venue-listing/app/. You will also now see an admin menu for managing venues.

Modify the admin views

The generated code only uses the name column, so let's add fields to the add and edit forms for the other columns.

<?php
// app/views/admin/venues/add.php
?>

<h2>Add Venue</h2>

<?php echo $this->form->create($model->name); ?>
<?php echo $this->form->input('name'); ?>
<?php echo $this->form->input('url', array('label' => 'URL', 'style' => 'width: 300px;')); ?>
<?php echo $this->form->input('address1', array('label' => 'Address 1')); ?>
<?php echo $this->form->input('address2', array('label' => 'Address 2')); ?>
<?php echo $this->form->input('city'); ?>
<?php echo $this->form->input('state', array('style' => 'width: 40px;')); ?>
<?php echo $this->form->input('zip', array('style' => 'width: 80px;')); ?>
<?php echo $this->form->input('description'); ?>
<?php echo $this->form->end('Add'); ?>
<?php
// app/views/admin/venues/edit.php
?>

<h2>Edit Venue</h2>

<?php echo $this->html->object_link($object, array('controller' => 'venues', 'text' => 'View')); ?>

<?php echo $this->form->create($model->name); ?>
<?php echo $this->form->input('name'); ?>
<?php echo $this->form->input('url', array('label' => 'URL', 'style' => 'width: 300px;')); ?>
<?php echo $this->form->input('address1', array('label' => 'Address 1')); ?>
<?php echo $this->form->input('address2', array('label' => 'Address 2')); ?>
<?php echo $this->form->input('city'); ?>
<?php echo $this->form->input('state', array('style' => 'width: 40px;')); ?>
<?php echo $this->form->input('zip', array('style' => 'width: 80px;')); ?>
<?php echo $this->form->input('description'); ?>
<?php echo $this->form->end('Update'); ?>

Modify the public views

Let's display the venue address and a link to its website in the public show view.

<?php
// app/views/venues/show.php
?>

<h2><?php echo $object->name; ?></h2>

<div>
  <?php echo $this->html->link('Website', $object->url); ?>
</div>

<?php
  echo '<div>'.$object->address1.'</div>';
  if (!empty($object->address2)) {
    echo '<div>'.$object->address2.'</div>';
  }
  echo '<div>'.$object->city.', '.$object->state.', '.$object->zip.'</div>';
?>

<p>
  <?php echo $this->html->link('All Venues', array('controller' => 'venues')); ?>
</p>

That's it! You can now begin to add venues in the admin section in Venues > Add New, edit them, view them at public-facing URLs like /venue/1/, and see a paginated list of them at /venues/.