Add-ons
The framework supports Add-ons. Add-ons are external packages that act as partials for a project giving it additional functionality. These are created exclusively for the framework.
Template
Write your add-on using the following development template.Register
Add-ons must be registered in the configuration file in order to be functional.
return [ // OTHER SETTINGS... 'addons' => [ 'MyNamespace\MyAddon', ], ];
Create
Start your add-on development (in the form of a package) by adding the MVC Core package as dependency.
Create an Add-on main class that extends from Addon
package abstract.
namespace MyNamespace; use WPMVC\Addon; class MyAddon 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. } }
Add Wordpress hooks as follows:
class MyAddon extends Addon { public function init() { add_filter( 'body_class', [ &$this, 'body_class' ] ); add_action( 'wp_enqueue_scripts', [ &$this, 'enqueue' ] ); } /** * Callback for defined filter hook "body_class". * Hook added on add-ons "init()" method. */ public function body_class( $body ) { $body[] = 'my-custom-class'; return $body; } /** * Callback for defined action hook "wp_enqueue_scripts". * Hook added on add-ons "init()" method. */ public function enqueue() { wp_enqueue_script( 'my-addon', assets_url( '../vendor/my-addon/assets/js/addon.js', __FILE__ ), [ 'jquery' ] ); } }
Composer
Add-ons must be registered in composer, at packagist.org.Main Class
To call the main class from any method in the add-on class use property $this->main
.
class MyAddon extends Addon { public function my_method() { $config = $this->main->config->get( 'configuration.setting' ); } }
Add-on methods can be accessed throughout the main class by calling them with an addon_
prefix.
class Main extends Bridge { public function init() { // This will call my_method() in add-on $this->addon_my_method(); } }
MVC
To call the MVC functionality from any method in the add-on class use property $this->mvc
.
class MyAddon extends Addon { public function controller() { $this->mvc->call( 'Controller@method' ); echo $this->mvc->view->get( 'view.key' ); } }