WordPressMVC

  1. Get Started
  2. Documentation
  3. Tutorials
  4. Blog
  1. Documentation
  2. Tutorials
v1.0.8 Login
  • Blog
  • Download
  • Get Started
  • The Basics
    • Ayuco
    • Main Class
    • Configuration
  • Hooks
    • Actions
    • Filters
    • Widgets
    • Shortcodes
  • MVC
    • Models
    • Post Models
    • Option Models
    • Term Models
    • User Models
    • Views
    • Controllers
  • Resources
    • Assets
    • Styles
    • Scripts
  • Advanced
    • Request
    • Response
    • Localization
    • Cache
    • Logger
    • Database
    • Testing
    • Deployment
    • Functions
    • Add-ons

Add-ons

Last updated: January 12, 2020

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' );
    }
}

MVC

MVC will only access controllers and views located inside the addon. Controllers should be located at [add-on root]/controllers/. Views should be located at [add-on root]/views/.
© 2023 10 Quality Studio . All rights reserved.
Search in: