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!

2 Answers
2

and welcome!

It would be helpful to have code that is more complete and shows how you are calling these methods. However, one thing I see that is likely responsible for at least part of your problem is the way in which you are calling add_meta_box().

add_meta_box() should be called during the add_meta_boxes action. I’ve never tried running it as a callback of register_post_type() before. But, assuming that you are running register_post_type() during init, I imagine that add_meta_box() would run during init too – before the function is available.

If WP is actually trying to run the add_meta_box() before it is available, you should be able to see a Fatal Error in your PHP logs because of calling an undefined function.

To troubleshoot, I’d first confirm that your register_meta_box_cb is working by simply having it call a function that will die; or write a something to a file.

After you confirm that it is working, you can try having the register_meta_box_cb call a function that will add an action to add_meta_boxes. Have the callback to that add_meta_boxes action run your add_meta_box() loop.

Let me konw if that doesn’t work or you need some more help!

Leave a Comment