asalce / WordPress-Base-Plugin

Start developing sane and civilized Wordpress plugins.

Home Page:http://www.emersonlackey.com/sanity/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Just tweaking syntax and such.

Wordpress Sanity Plugin Framework
=================================
The goal of this simple framework is to add some sanity and civility to Wordpress plugin development, a task which is currently messy and savage. So, without further ado, I should like to present you with some sanity.


For Developer's Eyes Only
=========================
The Sanity Framework was created for developers, not people who just install plugins. If you don't understand PHP and the principles of object oriented code you should probably just move on. However, if you're a frustrated Wordpress developer who is sick of the terrible coding practices encouraged by Wordpress' plugin API, then please, pull up a chair. We have much to talk about.


The Benefits
============
* A template system that helps to separate your logic from your views.
* An easy way to include JS and CSS within the admin or front-end of your plugin.
* AJAX in the admin made simple, with nonce integration out of the box.
* An object oriented approach to plugin development which makes modifications easy.


Installation
============
Sanity takes a very opinionated approach to plugin development. There are particular conventions laid out by the framework that need to be adhered to.


Templates
=========
Whenever possible, we should separate PHP from HTML. Within Wordpress, you'll see the two mixed together without apprehension. While this is often an "easy way" of doing things, it is almost never the "right way." Instead, we should segregate the two, thus keeping our logic pure and our views dumb. For this purpose we have the $this->render('my-template') method. A few examples:

		// From within a method in our controller
		$this->data['message'] = 'Pass this on to the template please';
		$this->render('my-template');
		
		// Meanwhile, in the /plugin/views/my-template.php file
		<h2>The Separation of Logic and Views</h2>
		<p><?php echo $this->data['message'];?></p>
		

Admin Javascript
================
Sometimes you might like to include some handy javascript or stylesheet files within the admin area of your wordpress installation. Well, Sanity makes this task quite easy! Just populate our $admin_js and $admin_css class variables with an array of scripts you would like to include.

        class YourPlugin extends SanityPluginFramework {
            
            // Loads /plugin/js/example.js
            var $admin_js = array('example');
            
            // Loads /plugin/css/example.css
            var $admin_css = array('example');
        
        }


Admin AJAX
==========
Adding ajax actions within the admin is easy. Within your plugin class you just follow this pattern:

    class YourPlugin extends SanityPluginFramework {


        // Registers our ajax action
        var $ajax_actions = array('admin' => array('my_ajax_action'));


        // Handles the actual ajax request and response.
        // All Wordpress admin ajax requests are sent through http://your-site.com/wp-admin/admin-ajax.php, which
        // is available as a global javascript variable sensibly named 'ajaxurl'
        function my_ajax_action() {
            $result['message'] = 'Your stuff has been saved';
            update_option('my_plugin_option', $_POST['field_1']);
            echo json_encode($result);
            exit();
        }

    }

About

Start developing sane and civilized Wordpress plugins.

http://www.emersonlackey.com/sanity/