• Hello people,

    I have a question about add_meta_box. Is there any way I can pass some arguments to the callback function. Here’s what I mean:

    function my_callback ($parameter1, $parameter2) {
    echo $parameter1;
    echo $parameter2;
    }
    
    function add_my_meta_box() {
    $var1 = "Blabla";
    $var2 = "Something"
    add_meta_box($id, $title, my_callback (<< Those 2 vars should be passed to this function), $page, $context, $priority);
    }

    Is there any way to do this? I would rather not use $_SESSION values.

    Thnx!

Viewing 5 replies - 1 through 5 (of 5 total)
  • thats exactly how you do it

    Thread Starter nielskreijveld

    (@nielskreijveld)

    What do you mean? $_SESSION variables or the my_callback($var1, $var2)?

    If you haven’t figured it out yet, since v2.8 there’s another parameter you didn’t show, $callback_args, i.e. here’s the function prototype:

    add_meta_box($id, $title, $callback, $page, $context = 'advanced', $priority = 'default', $callback_args=null)

    In your example passed an array containing your two variable’s values:

    function add_my_meta_box() {
    $var1 = "Blabla";
    $var2 = "Something"
    add_meta_box($id, $title, my_callback (<< Those 2 vars should be passed to this function), $page, $context, $priority, array('foo'=>$var1,'bar'=>$var2));
    }

    Then in your example it would look like this:

    function my_callback ($post, $metabox) {
    echo $metabox['args']['foo'];
    echo $metabox['args']['bar'];
    }

    Hope this helps.

    Thread Starter nielskreijveld

    (@nielskreijveld)

    Awesome! Gonna use that with my latest project right away.

    Thnx!

    @mikeschinkel – Thanks, the documentation wasn’t great on the Codex. $post, then $vars!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘add_meta_box callback with arguments?’ is closed to new replies.