使用herbert wordpress插件框架制作wordpress元盒


Making wordpress metaboxes using herbert wordPress plugin framework

我正在使用herbert为wordpress创建一个模块。我正在尝试为特定页面制作一个自定义的元框,所以我做了以下类

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

这个类是自动加载的,所以我确信这个类存在于后台。根据这个链接,我上课的方式是正确的,但问题是没有调用metaBoxTemplate函数。如果我将它的调用方式更改为$this->metaBoxTemplate(),它会加载模板,但在页面上的错误位置。有人知道为什么[$this, 'metaBoxTemplate']没有执行,但[$this, 'registerMetabox']执行得很好,以及我如何解决我的问题吗?感谢

好的,我发现了问题。问题来自于我调用常量数组的方式,所以我这样修复它:

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