Post Model - 10quality/wpmvc-addon-metaboxer GitHub Wiki

Custom fields are managed in a metaboxer post model, this model will also auto-generate metaboxes based on the configuration defined.

A metaboxer post model is an extension of the Post Model; a project can hold multiple post models if needed.

Content

Create a Post model

Since a metaboxer model is essentially a Post Model, it can be creating the following WordPress MVC commands:

For existing post types

To add custom fields to a custom post type not be registered by your project, use the following command:

php ayuco create model:{model name}

For example (for posts):

PHP ayuco create model:Post

For custom post types

To register a custom post type and add custom fields, use the following command:

php ayuco register type:{type} {model}

For example:

php ayuco register type:book Book

This will create the model and allow you to fill additional registration properties, as explained in WordPress MVC documentation.

IMPORTANT: The metaboxer addon will replace automated models, this means that you can remove the $registry_controller property from your model.


Convert to a metaboxer model

To convert your Post model to a metaboxer model, it will need the following modifications:

  • Replace use WPMVC\MVC\Models\PostModel for use WPMVC\Addons\Metaboxer\Abstracts\PostModel.
  • Add const TYPE and use it on property $type.
  • Add a protected init() method.
use WPMVC\MVC\Traits\FindTrait;
use WPMVC\Addons\Metaboxer\Abstracts\PostModel as Model;

class Post extends Model
{
    use FindTrait;
    /**
     * Type constant (post type).
     * @var string
     */
    const TYPE = 'post';
    /**
     * Post type.
     * @var string
     */
    protected $type = self::TYPE;
    /**
     * Initializes metaboxer configuration.
     */
    protected function init()
    {
        // TODO
    }
}

Metaboxes

Your metaboxes and custom fields configuration are defined inside the init() method:

// Namespace and use statement...

class Post extends Model
{
    // Class properties and methods...

    protected function init()
    {
        $this->metaboxes = [...];
    }
}

Enqueue

Enqueue additional scripts or styles inside the enqueue() method to load them when your custom fields are metaboxes are rendered, for example:

// Namespace and use statement...

class Post extends Model
{
    // Class properties and methods...

    public function enqueue()
    {
        wp_enqueue_script(
            'my-custom-settings',
            assets_url( 'js/admin.js' )
        );
    }
}

Footer

Render HTML templates at the footer using the footer() method, for example:

// Namespace and use statement...

class Post extends Model
{
    // Class properties and methods...

    public function footer()
    {
        get_bridge( 'MyNamespace' )->view( 'my-model.metaboxer-templates' );
    }
}

Register metaboxer models

To enable custom fields and metaboxes, each metaboxer post model needs to be registered into the Add-on. There are 2 ways to register models:

  • Register via the config file
  • Register via filter hook

Register via the config file

In the [project folder]/app/Config/app.php file, the following configuration must be added:

    'metaboxer_models' => [
        '[unique_id]' => '[Namespace]\Models\[Model]',
    ],

[model_id] must be replaced with a unique model ID, [Namespace] with your project namespace and [Model] with the model's class name, like in the next example:

    'metaboxer_models' => [
        'myapp_post' => 'MyNamespace\Models\Post',
    ],

Register via filter hook

Use the following filter hook to do so:

use [Namespace]\Models\Post;

add_filter( 'metaboxer_models', function( $models ) {
    $models['myapp_post'] = Post::class;
    return $models;
} );