WordPress Fuel Plugins

Writing a WordPress Plugin using WordPress Fuel


WordPress Plugins allow easy modification, customization, and enhancement to a WordPress blog. As you might be aware of the fact that the WordPress codebase is a procedural design, not object-oriented. Plugins can be extremely simple or extremely complex. Utilizing HMVC architecture in your plugin development greatly reduces the complexity and isolates the business, application and presentation logic.


  • License
    This article assumes you are already familiar with the basic functionality of WordPress, and PHP programming. This article describes how to write WordPress Plugin using WordPress Fuel framework. If you are not familiar with WordPress plugin development, please refer to the article writing a WordPress plugin.

The following example explains how to write a plugin using WordPress Fuel. In this example WordPress Shortcode API is used.


Step 1 - Creating folders

  1. Create a plugin folder and name it whatever your plugin do. Let's assume that you need to write a Simple Contact Form plugin using WordPress Fuel. So, create a folder simple-contact-form in your wp-content/plugins folder.
  2. create a folder mvc in your wp-content/plugins/simple-contact-form folder.
  3. create a folder controls in your wp-content/plugins/simple-contact-form/mvc/controls folder.

  • License
    Please refer to the filesystem document for more details on folder structure.

File: wp-content/plugins/simple-contact-form/index.php
<?php 
/*
Plugin Name: Simple Contact Form
Description: Simple Form to email (WordPress admin emai) - WordPress Fuel plugin
Version: 1.0
Author: [Your name]
Author URI: [Your website]
License: A "Slug" license name e.g. GPL2
*/
?>

Step 2 - Creating WordPress Fuel Bootstrap

Paste the following code into mvc/init.php

File: wp-content/plugins/simple-contact-form/mvc/init.php
<?php
if(defined('_WP_FUEL_MVC') == false)
{
    if(class_exists('absMVC_Plugin') == false)
    {
         $mvc_loader = WP_PLUGIN_DIR . "/wpfuel/load.php";
        
        if ( ! file_exists( $mvc_loader ) ) {
            wp_die( __('Simple Contact Form requires WordPress Fuel.'));
        }
        else
        {
            require_once $mvc_loader;
        }
    }
}
?>

Step 3 - Creating a Plugin Class

Let's give a name to class 'WpSimpleContactForm_Plugin'

File: wp-content/plugins/simple-contact-form/index.php
<?php
class WPFuel_SimpleContactForm_Plugin extends absMVC_Plugin
{
    /* Unique Plugin slug */
    protected $_plugin_slug = "wpsimplecontactform";

    protected function _init()
    {
        $this->add_shortcode("wpfuel_contact_form", 
            "scoWPSimpleContactForm_ShortCode","render_form"
        );
    }
}
?>

  • Line 2: WPFuel_SimpleContactForm_Plugin inherits abstract 'absMVC_Plugin' class.
  • Line 4: Plugin slug must be one word and unique.
  • Plugin slug will be used as suffix for controllers, models, helpers etc to avoid name conflicts.
  • If you want to develop a WordPress plugin you should extend the plugin class with absMVC_Plugin.

Plugin Workflow

  • Plugin registers Shortcode wpfuel_contact_form
  • Shortcode [wpfuel_contact_form] will be used to display the form.
  • Place the shortcode [wpfuel_contact_form] in any post or page.
  • Shortcode handler 'scoWPSimpleContactForm_ShortCode' interacts with controller to display XHTML markup.
  • On form submission, controller validates the form data and sends email to WordPress admin.

Step 4 - Creating Shortcode Class

File: wp-content/plugins/simple-contact-form/mvc/shortcodes/wpsimplecontactform/shortcode.php
<?php
class scoWPSimpleContactForm_ShortCode extends absMVC_Shortcode
{
    public function render_form($shortcode_attributes,$content = null)
    {
        global $post;
        
        /*Display the contact form on single page or post*/
        if(is_single($post))
        {
            /* Get the plugin that registered the shortcode */
            $oPlugin = $this->getPlugin();
            $plugin_file = $oPlugin->getPluginFile();

            /* Include stylesheet */
            wp_enqueue_style('contact-simple-form-styles', 
                plugins_url( '/assets/css/style.css', $plugin_file ),
                false,'1.1','all'
            );

            /*
            * Dispatch request to form controller. "form" is controller class.
            */
            return $oPlugin->dispatchRequest("form",array(
                'shortcode_attributes' => (array)$shortcode_attributes
            ));
        }
    }
}
?>

Step 6 - Creating 'form' Controller Class

File: wp-content/plugins/simple-contact-form/mvc/controls/wpsimplecontactform/form.php
<?php
class wpsimplecontactform_form_controller extends absController
{	
    public function index()
    {
        if(WPFuel::isPost())
        {
            $responseMessage = $this->_process();
            $this->ViewData('message',$responseMessage);
        }
        
        $this->View('form/index');
    }
    
    private function _process()
    {
        $data = $this->_request->getParams();
        if(isset($data['submit_form_btn']) == false)
            return;
        
        $errors = array();
        if(clsValidate::not_empty($data['full_name']) == false)
        {
            $errors['full_name'] = 'Name required';
        }
        else if(clsValidate::alpha_space($data['full_name']) == false)
        {
            $errors['full_name'] = 'Invalid name';
        }
        
        if(clsValidate::not_empty($data['email']) == false)
        {
            $errors['email'] = 'Email required';
        }
        elseif(clsValidate::email($data['email']) == false)
        {
            $errors['email'] = 'Invalid email address';
        }
        
        if(clsValidate::not_empty($data['subject']) == false)
        {
            $errors['subject'] = 'Subject required';
        }
        
        if(clsValidate::not_empty($data['message']) == false)
        {
            $errors['message'] = 'Message required';
        }
     
        if(count($errors) > 0)
        {
            $this->ViewData('row',$data);
            return array('type' => 'error', 'message' => $errors);
        }
        
 
        $this->_send_mail($data);
        return array('type' => 'success', 'message' => array('Thank you. Our representative will contact you shortly.'));

    }
    
    private function _send_mail($data)
    {
        $subject = "New Contact Request from ".$data['full_name']."";

        $message = "<html><body><table cellpadding='0' cellspacing='0' width='100%'>";

        foreach($data as $label=>$value)
        {
            if($value != "")
            {
                $label = ucWords(str_replace("_"," ",$label));
                $message .="<tr><td width='30%'>".$label."</td><td width='70%' colspan='2'>".nl2br($value)."</td></tr>";
            }
        }
        $message .="</table></body></html>";
       
        wp_mail(get_option('admin_email'), $subject, $message);
        
    }
}
?>

Step 5 - Creating 'form' View

File: wp-content/plugins/simple-contact-form/mvc/views/form/index.php
<div class="main_container"><div class="form_container">
        
        <?PHP if(isset($message)): ?>
        <ul class="system_messages" id="msg_flash_ul">
            <li id="li_msg" class="<?PHP echo $message['type']; ?>">
                    <span class="ico"></span>
                    <strong class="system_title">
                        
                    <?PHP foreach($message['message'] as $k => $message_text): ?>
                        <?PHP echo $message_text; ?><br />
                     <?PHP endforeach; ?>
                        
                    </strong>
            </li>
        </ul>
        <?PHP endif; ?>
        
        <form method="POST" class="rounded_form" action="<?PHP echo clsUri::currentURL(); ?>">
            <fieldset>
                
                <div class="field_box">

                        <span class="field_area">
                            <label>Your Name *</label>
                            <input type="text" id="full_name" name="full_name" value="<?PHP echo $row['full_name']; ?>">
                        </span>


                </div>
                
                <div class="field_box">

                        <span class="field_area">
                                <label>Email *</label>
                            <input type="text" id="email" name="email" value="<?PHP echo $row['email']; ?>">
                        </span>

                </div>
      
                <div class="field_box">

                        <span class="field_area">
                            <label>Subject *</label>
                             <input type="text" id="subject" name="subject" value="<?PHP echo $row['subject']; ?>">
                          
                        </span>

                </div>
                
                <div class="field_box">

                        <span class="field_area">
                                <label>Message *</label>
                           <textarea id="message" name="message"><?PHP echo $row['message']; ?></textarea>
                        </span>
   
                </div>
       
                <div class="field_box">

                    <span class="field_area">
                          <input type="submit" value="Submit" id="submit_btn" class="form-btn-submit" name="submit_form_btn">
                          <span style="display:none;" id="msg_bottom" class="info_area bottom_info"></span>
                    </span>

                </div>
            </fieldset>
        </form>
    </div>
</div>

Full Plugin Code - Complete File

File: wp-content/plugins/simple-contact-form/index.php
<?php 
/*
Plugin Name: Simple Contact Form
Description: Simple Form to email (WordPress admin emai) - WordPress Fuel plugin
Version: 1.0
Author: [Your name]
Author URI: [Your website]
License: A "Slug" license name e.g. GPL2
*/
class WPFuel_SimpleContactForm_Plugin extends absMVC_Plugin
{
    /* Unique Plugin slug */
    protected $_plugin_slug = "wpsimplecontactform";

    protected function _init()
    {
        $this->add_shortcode("wpfuel_contact_form", 
            "scoWPSimpleContactForm_ShortCode","render_form"
        );
    }
}
//run it
new WPFuel_SimpleContactForm_Plugin(__FILE__);
?>

Steps for writing a plugin

  • Create a plugin folder yourplugin in wp-content/plugins/
  • Create a plugin file index.php in wp-content/plugins/yourplugin/
  • Create an mvc folder in in wp-content/plugins/yourplugin/
  • Prepare WP Fuel Plugin directory structure (controls, views, models, helpers etc). Please read filesystem.
  • Include WordPress Fuel bootstrap mvc/init.php file.
  • Prepare your plugin information using WordPress header.
  • Write a Plugin class and extend it with absMVC_Plugin.
  • Set the plugin slug property. Must be unique.
  • Implement the _init() method as it's an abstract method.
  • Write your plugin specific functionality. Please see WordPress Fuel plugin API.
  • Happy coding!