2

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
  • Why are you making multiple class? use one Class and add the code there. Do not need any separate class for every callback function. Jan 31, 2019 at 8:23
  • Making a separate class for each meta box because each one's render_meta_box function is going to be different. For example, a checkbox meta box is going to be rendered differently than a select type one, Jan 31, 2019 at 18:47

2 Answers 2

0

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!

1
  • Registering meta boxes while registering a post type works fine. I got it to work, just that I was making a stupid mistake of using an incorrect variable name in the add_meta_box() function. You can add meta boxes both ways, while registering a post type and also by using the add_meta_boxes action. Jan 31, 2019 at 18:59
0

So, I figured out what was going wrong.

It was as trivial as an incorrect class variable name. I was using a variable called field_label in the class, and accessing it incorrectly like $this->label, which meant it had NULL value.

My approach to add meta boxes during the post type registration however works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.