# WORDPRESS PLUGIN (Template)
, (*1)
The power of Composer and MVC in your Wordpress plugins., (*2)
Wordpress Plugin (WPP) is a development template that can be used to create modern and elegant plugins. WPP comes with Composer and Lightweight MVC framework., (*3)
Requirements
Installation
WPP utilizes Composer to manage its dependencies. So, before using WPP, make sure you have Composer installed on your machine., (*4)
Step 1
Download the latest release of the software;, (*5)
Step 2
Create the folder location where you plugin will reside, usually inside the plugins folder of your wordpress installation:, (*6)
[WORDPRESS ROOT]
|---> [wp-content]
|---> [plugins]
|---> "your-plugin-name"
Step 3
Copy the content of the release version downloaded into your plugin's folder, should look like this:, (*7)
[WORDPRESS ROOT]
|---> [wp-content]
|---> [plugins]
|---> "your-plugin-name"
|---> [boot]
|---> [config]
|---> [controllers]
|---> [css]
|---> [js]
|---> [plugin]
|---> [views]
|---> ayuco
|---> composer.json
|---> LICENSE
|---> plugin.php
|---> README
Step 4
Open your Command Prompt
application and change directory to point to your plugin's folder, where composer.json
resides., (*8)
Type the following command:, (*9)
composer install
This will install all the software dependencies of WPP., (*10)
Step 5
In order to prevent conflicts with other plugins using this template, it is suggested to set a name and change its namespace., (*11)
To do this, type the following in the commando prompt:, (*12)
php ayuco setup
This will ask you for a new name for yor plugin, you can always change it later with the following command:, (*13)
php ayuco setname MyNewName
Finally open plugin.php
and modify the information located in the first comment section:, (*14)
/*
Plugin Name: [MY PLUGIN]
Plugin URI: [MY URL]
Description: [MY DESCRIPTION]
Version: 1.0
Author: [MY NAME OR COMPANY]
Author URI: [MY IR COMPANY URL]
*/
INSTALLATION COMPLETED!, (*15)
Updating
To update the current version of the software, in the command prompt type:, (*16)
composer update
Usage
Anything you code file that needs to be created must be located inside the plugin
folder., (*17)
Main Class
WPP comes with a master class called Main.php
and which is located in the plugins
folder., (*18)
This class is the bridge between Wordpress and WPP. Any Hook or filter should be declared inside this class., (*19)
Hooks and Filters
Main.php
has two methods:, (*20)
class Main extends Plugin
{
/**
* Constructor.
* Declares HOOKS and FILTERS.
*/
public function init()
{
// Call public Wordpress HOOKS and FILTERS here
// --------------------------------------------
// i.e.
// add_action( 'save_post', [ &$this, 'save_post' ] );
}
/**
* Declares HOOKS and FILTERS when on admin dashboard.
*/
public function on_admin()
{
// Call public Wordpress HOOKS and FILTERS here
// --------------------------------------------
// i.e.
// add_action( 'admin_init', [ &$this, 'admin_init' ] );
}
}
init()
is where all public declarations. on_admin()
will be called only when the plugin is on admin dashboard., (*21)
In this following example a hook to modify a post content will be added to the main class., (*22)
class Main extends Plugin
{
public function init()
{
add_filter( 'the_content', [ &$this, 'filter_content' ] );
}
public function on_admin()
{
}
public function filter_content( $content )
{
// YOUR CODE HERE
return $content;
}
}
Notice how the add_filter
is registering a call to the method filter_content
within Main.php
., (*23)
You can do the same with admin hooks and filters, like displaying a custom metabox in a post admin form:, (*24)
class Main extends Plugin
{
public function init()
{
}
public function on_admin()
{
add_action( 'add_meta_boxes', [ &$this, 'metaboxes' ] );
}
public function metaboxes( $post_type, $post )
{
// YOUR METABOX DECALARATION HERE
}
}
MVC
Lightweight MVC is a powerfull and small MVC framework that comes with WPP., (*25)
To read more about the usar of Models, Views and Controllers we recommed to visit the packages main page:, (*26)
Lightweight MVC, (*27)
Main class and MVC
Lightweight MVC engine is already integrated with Main.php
, call the engine with $this->mvc
., (*28)
In the following example, Main.php
is calling PostController
to save a post modification:, (*29)
class Main extends Plugin
{
public function init()
{
}
public function on_admin()
{
add_action( 'save_post', [ &$this, 'save_post' ] );
}
public function save_post( $post_id )
{
$this->mvc->call( 'PostController@save', $post_id );
}
}
Here is where the MVC files are located within your plugin:, (*30)
[PLUGIN ROOT]
|---> [controllers]
|---> [plugin]
|---> [models]
|---> [views]
Coding Guidelines
The coding is a mix between PSR-2 and Wordpress PHP guidelines., (*31)
License
Wordpress Plugin is free software distributed under the terms of the MIT license., (*32)