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

时间:2016-05-12 13:26:12

标签: php wordpress herbert

我正在使用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');
    }
}

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

1 个答案:

答案 0 :(得分:1)

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

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