WORDPRESS PLUGIN CORE (For Wordpress Plugin)
Core library for Wordpress Plugin (A Wordpress template that comes with composer and MVC);
Add
"amostajo/wordpress-plugin-core": "1.0.*"
to your composer.json. Then run composer install
or composer update
.
WP Core now supports Add-on development. To create your custom package, simply create an Add-on main class that extends from this package abstract, like the following example:
namespace MyNamespace;
use Amostajo\WPPluginCore\Classes\Addon;
class PostPicker extends Addon
{
/**
* Function called when plugin or theme starts.
* Add wordpress hooks (actions, filters) here.
*/
public function init()
{
// YOUR CUSTOM CODE HERE.
}
/**
* Function called when user is on admin dashboard.
* Add wordpress hooks (actions, filters) here.
*/
public function on_admin()
{
// YOUR CUSTOM CODE HERE.
}
}
This is how you should add your Wordpress hooks:
namespace MyNamespace;
use Amostajo\WPPluginCore\Classes\Addon;
class PostPicker extends Addon
{
/**
* Function called when plugin or theme starts.
* Add wordpress hooks (actions, filters) here.
*/
public function init()
{
add_filter( 'post_content', [ &$this, 'picker_filter' ] )
}
/**
* Called by wordpress.
*/
public function picker_filter()
{
// YOUR CUSTOM CODE HERE.
}
}
You can call the main class from any custom method in your Add-on
class, like this:
/**
* Custom method.
*/
public function picker_filter()
{
// Getting a config settings.
$this->main->config->get( 'setting' );
}
Calling for Add-on's controllers or views:
/**
* Custom method.
*/
public function picker_filter()
{
// Calling MVC.
$this->mvc->call( 'Controller@method' );
}
Your controllers should be placed in a controllers
folder on the same level as your Add-on
class, same for the views
folder.
The Main
class can call methods in the Add-on by adding the addon_
prefic, like for example addon_picker_filter
.
Finally add your package in the configuration file. For example:
'addons' => [
'MyNamespace\PostPicker',
],
The coding is a mix between PSR-2 and Wordpress PHP guidelines.
Lightweight MVC is free software distributed under the terms of the MIT license.