2016-05-12 11 views
1

Ich verwende herbert, um ein Modul für WordPress zu erstellen. Ich versuche, eine benutzerdefinierte Meta-Box für eine bestimmte Seite zu machen, so habe ich die folgende KlasseMaking WordPress Metaboxen mit Herbert WordPress Plugin Framework

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

Die Klasse wird automatisch geladen, so weiß ich sicher, dass die Klasse auf dem Back-Office existiert. Nach dieser link ist die Art, wie ich meine Klasse gemacht hat, korrekt, aber das Problem ist, dass die metaBoxTemplate Funktion nicht aufruft. Wenn ich die Art, wie ich es anrufe, auf $this->metaBoxTemplate() änderte, lädt es die Vorlage aber an einer falschen Stelle auf der Seite. Weiß jemand, warum die [$this, 'metaBoxTemplate'] nicht ausgeführt wird, aber die [$this, 'registerMetabox'] ist gut ausgeführt und wie kann ich mein Problem lösen? Danke

Antwort

1

Ok ich fand das Problem. Das Problem kam von der Art, wie ich das konstante Array anrief, also reparierte ich es wie folgt:

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