Undefined variable in class when i defined it

Coding a new version of my own Wordpress plugin, this time with OOP, to make it more readable for other contributors. It is based on the Wordpress Plugin Boilerplate as well as Cuztom to make it easy to create custom post types.

My CPTs are built, they update well. Although I can call the hook and create my metabox from the same function, I create my CPT, I cannot create the metabox separately from the other function as the object (CPT) is null.

The errors I am getting:

Note: variable Undefined: person at / Users / Lazhar / dev / wp -content / plugins / myplugin / admin / class-myplugin-admin.php on line 243

Fatal error: Call the add_meta_box () member function on null in / Users / Lazhar / dev / wp -content / plugins / myplugin / admin / class-myplugin-admin.php on line 243

I am calling this hook from my plugin class:

$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 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' );

      

Everything here works, both functions are called, the first one works and creates my own post type, the second one gets called without any problem but throws an error. Here are both functions:

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 Metaboxes

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

    }

      

So, when I use the metox function in the create_myplugin_custom_post_types function, it works well. As soon as I add it inside my own create_myplugin_metaboxes function, it throws the following errors:

Note: variable Undefined: person at / Users / Lazhar / dev / wp -content / plugins / myplugin / admin / class-myplugin-admin.php on line 243

Fatal error: Call the add_meta_box () member function on null in / Users / Lazhar / dev / wp -content / plugins / myplugin / admin / class-myplugin-admin.php on line 243

This is problematic because I have so many metaboxes that I have to do it within my own function, but $ person seems to be Undefined even if I were defining it !!

_ EDIT and ADDITION: - I can't instantiate in __construct because it's too early to create the CPT. __contrsuct is called too early, so it throws other errors ...

+3


source to share


2 answers


Like others, you must use $this->person

because of the area. Variables inside functions only live inside those functions, unless they are passed and returned or passed as a reference.

To answer your comment on the OP, you don't have to create it in __contruct()

, but alternatively you can do something like:



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 Metaboxes
        $this->person->add_meta_box('person_details', 'Details of this person', array( /* all my custom fields go here */));
    }
}

      

To post your comment on Suruba's answer. You probably should var_dump($this->person)

before and / or after add_metabox()

, because that's the part that doesn't work based on your comment.

+2


source


replace your function with the following code:

public function create_myplugin_metaboxes() {

            // Person Custom Post Type Metaboxes

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

        }

      



$this->

was overlooked by you, you need to use a keyword $this->

to refer to the same class members.

0


source







All Articles