简体   繁体   中英

Making wordpress metaboxes using herbert wordPress plugin framework

I'm using herbert to create a module for wordpress. I'm trying to make a custom meta box for a specific page so I did the following class

<?php 

namespace Testing\MetaBoxes;

use Testing\Helper;

class ExtendMetaBoxes {

    const WORDPRESS_CONTEXT = ['normal' => 'normal', 'advanced' => 'advanced', 'side' => 'side'];
    const WORDPRESS_PRIORITY = ['high' => 'high', 'default' => 'default', 'low' => 'low'];

    /**
     * Metabox id.
     *
     * @var string
     */
    private $id;

    /**
     * Metabox title.
     *
     * @var string
     */
    private $title;

    /**
     * Metabox name.
     *
     * @var string
     */
    protected $name;

    /**
     * Metabox nonce.
     *
     * @var string
     */
    protected $nonce;

    /**
     * Metabox page.
     *
     * @var string
     */
    private $page;

    /**
     * Metabox context.
     *
     * Allowed values from wordpress: "normal", "advanced" and "side"
     * @var string
     */
    private $context;

    /**
     * Metabox priority.
     *
     * Allowed values from wordpress: "high", "default" and "low"
     * @var string
     */
    private $priority;

    /**
     * Metabox callback_args.
     *
     * @var string
     */
    private $callback_args;

    /**
     * Metabox view.
     *
     * @var herbert object
     */
    private $view;

    /**
     *  Constructs the metabox.
     */
    public function __construct()
    {
        $this->id = 'properties-integretation-system';
        $this->title = Helper::get('pluginName');
        $this->page = 'property';
        $this->name = 'api_systems';
        $this->nonce = 'api_systems_nonce';
        $this->context = WORDPRESS_CONTEXT['side'];
        $this->priority = WORDPRESS_PRIORITY['low'];
        $this->view = herbert('twig');
        add_action( 'add_meta_boxes',  [$this, 'registerMetabox'] );
    }

    /**
     * Adding meta box to the given page
     */
    public function registerMetabox()
    {
        add_meta_box( $this->id, $this->title, [$this, 'metaBoxTemplate'], $this->page, $this->context, $this->priority );
    }

    /**
     * Prints out the metabox template.
     *
     * @param $post
     */
    public function metaBoxTemplate()
    {
      echo $this->view->render('@Testing/metaboxes/api.twig');
    }
}

The class is auto-loaded so I know for sure that the class exist on the backoffice. According to this link the way that I did my class is correct but the issue is that is not calling the metaBoxTemplate function. If I changed the way I'm calling it to $this->metaBoxTemplate() , it loads the template but on a wrong position on the page. Does anyone knows why the [$this, 'metaBoxTemplate'] is not executed but the [$this, 'registerMetabox'] is executed fine and how I can solve my issue? Thanks

Ok I found the issue. The issue was coming from the way I was calling the constant array, so I fixed it like this way:

$this->context = self::WORDPRESS_CONTEXT['side'];
$this->priority = self::WORDPRESS_PRIORITY['low'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM