• Resolved David Gewirtz

    (@dgewirtz)


    I am having considerable difficulty accessing CMB2 data structures inside a WordPress filter. I have a function of the form:

    $options = new_cmb2_box($args);
    $options = apply_filters ('my_filter', $options );

    Inside the filter, I’m finding the $options array to be uncountable and I’m having difficulty with call by value vs. call by reference. My filter needs to be able to add, modify, and remove fields from the metabox.

    I’ve tried defining it both as:

    function my_filter($box)

    and

    function my_filter(& $box)

    with no real success. I’m able to call:

    $box->remove_field("my_field");

    but I’ve been unable to call:

    $box->add_field(array(
      'id'  => 'id_value',
      [more items],
      ));

    I’m getting errors of the form “Warning: Parameter 1 to my_filter() expected to be a reference, value given in /app/public/wp-includes/class-wp-hook.php on line 288” and so on.

    What’s the right way to manipulate the contents and specification of a metabox within a WordPress filter? Any examples would help.

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    As far as I am able to see, determine, and based on experiences, it should be no different than if you were doing it all within the same space and same callback.

    I have my local dev site set up where I have a metabox object instantiated on cmb2_admin_init and set up one field. Then I pass the cmb2 object variable into an apply filters, and inside a callback for that filter, I add another field, and then return. Not seeing any errors or issues thus far. Both fields get registered and are usable. Didn’t have to use the & part for the passed in parameter or anything.

    I guess my only question at the moment is if this is within your own class, or is it all done with procedural code? Mine for sake of quick scaffolding, is just procedural.

    Thread Starter David Gewirtz

    (@dgewirtz)

    Yeah, this might have been my error. Some old code was also executing, causing errors. Oops.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    No worries. 😀

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Accessing CMB2 data structures inside filters’ is closed to new replies.