add_meta_box : Pass Params to Callback? (4 posts)

  1. udo kupsch
    Posted 6 years ago #

    Hello Community,

    I need to pass some Params to "my_callback":
    <?php add_meta_box( $id, $title, 'my_callback', $page, $context, $priority ); ?>

    First try was something like this:

    function my_callback() {
    global $params;
    function add_my_meta_box() {
    global $param;
    $param = 'somevalue';
    add_meta_box( $id, $title, <strong>'my_callback'</strong>, $page, $context, $priority );

    Did not work...

    Next try was to wrap it up in a Class to pass the params like
    $this->param = 'somevalue';
    Good plan, but when it came to my_callback, I tried something like

    'my_class::my_callback' or
    array ($this, 'my_callback') or

    Yes, I know, some things I tried are stupid, but I did... so what...

    I finally came up with

    $_SESSION['MYPARAM'] = 'somevalue';

    At least, it works. But I donĀ“t like it.
    Is there any way to pass params to my_callback, or use add_meta_box from within a class?

    Thanks in advance!

    Best regards


  2. udo kupsch
    Posted 6 years ago #

    Found it.
    The Problem was not in the script, but about 1 meter in front of my monitor.
    If anyone else has this problem, try this:
    <?php add_meta_box( id, title, array(&$this, 'new_meta_boxes'), page, context, priority); ?>

    Shame on me for not knowing how to set a classmethod as callback...

  3. Russell Heimlich
    Posted 5 years ago #

    I'm having trouble wrapping my head around this...

    Lets say I have the following:

    add_meta_box( $id, $title, $callback='display_box', $page, $context, $priority );

    function display_box(){
    //Stuff happens here...

    and I want to pass an object to display_box called $box. How would I do that?

    I tried replacing the callback with array(&$box,'display_box') but that didn't work. Error log says "First argument is expected to be a valid callback" aka a string?

  4. Mike Schinkel
    Posted 5 years ago #

    See http://wordpress.org/support/topic/379614 for how to do this since v2.8.

Topic Closed

This topic has been closed to new replies.

About this Topic