Site icon Hip-Hop Website Design and Development

Adding Meta Boxes while Registering a Post Type in a Class

I am trying to add a few meta boxes for a Custom Post Type through a Class. I am using the "register_meta_box_cb" parameter while registering the Custom Post Type to specify the callback function for adding meta boxes.

Sample code – (outline of my code) –

class ABC
{
  public static function add_customposttype()
  {
    $abc_o = new ABC();
    $abc_o->reg();
  }

  public function reg()
  {
    $args = array(
      "label" => "ABC",
      "description" => "New CPT",
      "public" => true,
      "register_meta_box_cb" => array($this, "meta_box_callback")
    );
    register_post_type("abc", $args);
  } 

    public function meta_box_callback()
    {
      $meta_fields = get_all_meta_fields();   //  This gets me an array of objects of classes 'c_box, b_box and a_box'

      foreach($meta_fields as $object)
      {
        add_meta_box($object->name, $object->label, array($object, 'render_meta_box'), 'abc', $object->context, $object->priority);
      }
    }
}

class c_box
{
  public $name;
  public $label;
  public $context;
  public $priority;

  public function __construct()
  {
    $this->name = 'c_box';
    $this->label = 'C Box';
    $this->context = 'normal';
    $this->priority = 'high';
  }

  public function render_meta_box($post)
  {
    echo "<p>This is a C Box</p>";    //  Never executes
  }
}

class b_box
{
  public $name;
  public $label;
  public $context;
  public $priority;

  public function __construct()
  {
    $this->name = 'b_box';
    $this->label = 'B Box';
    $this->context = 'normal';
    $this->priority = 'high';
  }

  public function render_meta_box($post)
  {
    echo "<p>This is a B Box</p>";    //  Never executes
  }
}

class a_box
{
  public $name;
  public $label;
  public $context;
  public $priority;

  public function __construct()
  {
    $this->name = 'a_box';
    $this->label = 'A Box';
    $this->context = 'normal';
    $this->priority = 'high';
  }

  public function render_meta_box($post)
  {
    echo "<p>This is a A Box</p>";    //  Never executes
  }
}

So I have different Classes defined for different meta boxes. I want all of them to have a common render_meta_box function so that I can create objects of all these Classes, put them in an array and traverse through it so that I can call the render_meta_box function for all of them one after the other.

Basically, a meta box will have its own class with a render_meta_box function and some other functions, and I can create an object of each of such classes and run their functions one after the other, thus making it easy to add new meta boxes in the future.

The problem I am facing is, when I define the render_meta_box method in separate classes with HTML code in it to be echoed, it looks like add_meta_box never executes that callback function. I tried to see if I could get it to print something in the render_meta_box method but it never reached that print statement.

Am I missing something here? I thought I could call the render_meta_box methods using different objects to have them all execute differently, is my approach incorrect?

Thanks in advance!