WordPress.org

Forums

add_meta_box callback with arguments? (6 posts)

  1. nielskreijveld
    Member
    Posted 5 years ago #

    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!

  2. westondeboer
    Member
    Posted 5 years ago #

    thats exactly how you do it

  3. nielskreijveld
    Member
    Posted 5 years ago #

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

  4. Mike Schinkel
    Member
    Posted 5 years ago #

    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.

  5. nielskreijveld
    Member
    Posted 5 years ago #

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

    Thnx!

  6. Zack Katz
    Member
    Posted 4 years ago #

    @MikeSchinkel - Thanks, the documentation wasn't great on the Codex. $post, then $vars!

Topic Closed

This topic has been closed to new replies.

About this Topic