2015-05-28 35 views
3

編寫我自己的Wordpress插件的新版本,這次使用OOP使其對其他協作者更具可讀性。它基於Wordpress Plugin Boilerplate和Cuztom,以方便創建自定義帖子類型。當我定義它時,在一個類中未定義的變量

我的CPT已創建,它們更新良好。儘管我可以在相同的函數中調用掛鉤並創建我的元框,但是我創建了CPT,但無法與其他函數分開創建元框,因爲對象(CPT)爲null

錯誤,我得到:

注意:未定義的變量:人/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php線243

致命錯誤:調用一個成員函數add_meta_box()上的空中/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php線243

我從我的插件類中調用鉤子:

$this->loader->add_action('admin_init', $plugin_admin, 'create_myplugin_custom_post_types'); 
$this->loader->add_action('add_meta_boxes', $plugin_admin, 'create_myplugin_metaboxes'); 

// Create Plugin's Menu 
$this->loader->add_action('parent_file', $plugin_admin, 'set_current_menu'); 
$this->loader->add_action('admin_menu', $plugin_admin, 'setup_admin_menus'); 
$this->loader->add_action('admin_menu', $plugin_admin, 'remove_metaboxes'); 

這裏,一切正常,這兩個函數都被調用,第一個工作並創建我的自定義帖子類型,第二個被調用沒有任何問題,但觸發錯誤。這裏都是功能:

class myPlugin_Admin { 


    private $plugin_name; 
    private $version; 

    var $person; 
    // also tried protected $person, private, public, etc... 

    public function __construct($plugin_name, $version) { 

     $this->plugin_name = $plugin_name; 
     $this->version = $version; 

    } 

    public function create_myplugin_custom_post_types() { 

     // Custom Post Type: Person 

     $person = new Cuztom_Post_Type(
      'Person', 
      array(
       'supports'    => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions'), 
       'public'    => true, 
       'capability_type'  => 'page', 
       'rewrite'    => array('slug' => 'person'), 
       'menu_position'   => 30, 
       'menu_icon'    => 'dashicons-id', 
       'show_ui'    => true, 
       'show_in_menu'   => false, 
       'show_in_nav_menus'  => true, 
       'publicly_queryable' => true, 
       'exclude_from_search' => false, 
       'has_archive'   => true, 
       'query_var'    => true, 
       'can_export'   => true,    
       'hierarchical'   => true, 
       ) 
     ); 
    } 

    public function create_myplugin_metaboxes() { 

     // Person Custom Post Type's Metaboxes 

     $person->add_meta_box(
      'person_details', 
      'Details of this person', 
      array(/* all my custom fields go here */) 
     );  

    } 

所以,當我使用create_myplugin_custom_post_types內metabox功能功能,它的工作原理都好。在/用戶/ Lazhar的/ dev /可溼性粉劑內容/插件/爲myplugin /管理/講座人:當我加入這裏面它自己的create_myplugin_metaboxes功能,它會觸發這些錯誤:

注意:未定義的變量調用一個成員函數add_meta_box()上在/空的用戶/ Lazhar的/ dev /可溼性粉劑內容/插件/爲myplugin /管理/類爲myplugin管理員:行243

致命錯誤爲myplugin-admin.php的.php 243行

這是有問題的,因爲我有很多元創建,我需要做它在它自己的功能,但$人似乎沒有定義,即使我確定了它!

_ 編輯&此外: - 我不能在__construct實例,因爲它是太早創建CPT即可。 __contrsuct被稱爲太早所以它然後觸發其他錯誤...

+1

與其他類屬性一樣,您需要'$ this-> person'。 – jeroen

+0

您需要閱讀如何OOP和/或範圍。在一種方法中定義'$ person',將其定義在該範圍內,而不是超出範圍。如果你想繼續在課堂上使用它,那麼你想創建它作爲一個實例變量,並使用例如'$ this-> person = ...'和'$ this-> person-> add_meta_box'等 –

+0

@JonStirling但我無法在__construct實例化它,因爲現在創建CPT還爲時過早。 __contrsuct被稱爲太早,所以它會觸發其他錯誤... – Lazhar

回答

2

與其他人一樣,由於範圍的原因,您需要使用$this->person。函數內部的變量只能存在於這些函數中,除非它們被傳入並返回或作爲參考傳入。

爲了解決在OP您的評論,你不需要實例化它__contruct(),但或者你可以這樣做:

class myPlugin_Admin { 
    private 
     $plugin_name, 
     $version, 
     $person = null; 

    public function __construct ($plugin_name, $version) { 
     $this->plugin_name = $plugin_name; 
     $this->version = $version; 
    } 

    public function create_myplugin_custom_post_types() { 
     // Custom Post Type: Person 
     if ($this->person === null) { 
      $this->person = new Cuztom_Post_Type('Person', array('supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions'), 'public' => true, 'capability_type' => 'page', 'rewrite' => array('slug' => 'person'), 'menu_position' => 30, 'menu_icon' => 'dashicons-id', 'show_ui' => true, 'show_in_menu' => false, 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'hierarchical' => true,)); 
     } 
    } 

    public function create_myplugin_metaboxes() { 
     if ($this->person === null) { 
      $this->create_myplugin_custom_post_types(); 
     } 
     // Person Custom Post Type's Metaboxes 
     $this->person->add_meta_box('person_details', 'Details of this person', array(/* all my custom fields go here */)); 
    } 
} 

,以解決Sourabh的回答您的評論。您應該在add_metabox()之前和/或之後var_dump($this->person),因爲這是根據您的評論看起來不工作的部分。

0

與此代碼替換您的函數:

public function create_myplugin_metaboxes() { 

      // Person Custom Post Type's Metaboxes 

      $this->person->add_meta_box(
       'person_details', 
       'Details of this person', 
       array(/* all my custom fields go here */) 
      );  

     } 

$this->被你錯過了,你需要使用$this->關鍵字來引用相同的類成員。

+0

它不再觸發錯誤,它創建自定義帖子類型但不是metabox,但它不顯示任何警告或錯誤?! – Lazhar

+0

爲什麼當你正確地做某件事時會顯示警告或錯誤? –