2015-05-28 34 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

为什么当你正确地做某件事时会显示警告或错误? –