WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I trying to pass some variable to the callback function through add_meta_box. Knowing that there is $callback_args that can be use to pass the variable. But somehow I just can't get it work within a class. This is my codes

class myclass{
 //.....blah blah....adding action
  function add_ex_field() {

add_meta_box( 'apextadmin', __( 'My Custom Field', 'myplugin_textdomain' ),array($this,'the_callback_function'),post,array('test'=> $param));}//the $param is some value that saved in my option table.

 function the_callback_function($post, $metabox){//do I need to include these two parameters? because they gave me error! 
 echo '<h1>testing field</h1>';
 echo $metabox['args']['test'];

global $myclas; 
$myclas = new myclas();


share|improve this question
up vote 1 down vote accepted

Sometimes an example says more than a thousand words. Here's a small plugin that will guide you:

! defined( 'ABSPATH' ) AND exit;
 * Plugin Name: (#65981) »kaiser« Meta Box example
 * Plugin URI:  http://goo.gl/ls6Q6
 * Description: Example showing how to add a meta box with callback args
 * Author:      Franz Josef Kaiser
 * Author URI:  http://unserkaiser.com

if ( ! class_exists( 'WPSE65981_MetaBox_Example' ) )
    add_action( 'load-post.php', array( 'WPSE65981_MetaBox_Example', 'init' ) );
    add_action( 'load-post-new.php', array( 'WPSE65981_MetaBox_Example', 'init' ) );

 * The Class
class WPSE65981_MetaBox_Example
    protected static $instance;

    public static function init()
        null === self :: $instance AND self :: $instance = new self;
        return self :: $instance;

    public function __construct()
        add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );

     * Adds the meta box container
     * @return void
    public function add_meta_box()
            // ID
            // Title/"hndle"-bar
            ,__( 'WPSE Meta Box Headline', 'example_textdomain' )
            // Callback
            ,array( $this, 'render_meta_box_content' )
            // post_type
            // context
            // priority
            // callback_args
            ,array( 'Hello', "{$GLOBALS['current_user']->display_name}!" )

     * Render Meta Box content (Callback)
     * @return void
    public function render_meta_box_content( $post, $callback_args ) 
        // Argument dump: Uncomment for insights
        # var_dump( $post );
        # var_dump( $callback_args );

        $args = array_map( 'esc_html', $callback_args['args'] );
        return print "<h2>".implode( ' ', $args )."</h2>";
} // END Class WPSE65981_MetaBox_Example

} // endif;

If you're wondering, how I identified those hooks that are used for the static init function to load, then take a look at my »current admin info« plugin here.

enter image description here

share|improve this answer
You don't need to use &$this in add_(action|filter) calls now that we don't have to support PHP4 anymore. Objects that are stored in variables aren't actually stored in variables -- it's an identifier that the caller (call_user_func in this case) uses to look up the object. php.net/manual/en/language.oop5.references.php – chrisguitarguy Sep 22 '12 at 21:39
@chrisguitarguy Yes, I know. Thanks for the "heads up". I just took the (old) Codex example and modified it to fit the question. As you can see from the edits, I just overlooked the first one :) – kaiser Sep 22 '12 at 21:40
What if the variable that I want to pass is in an array? like this foreach($options['rules'] as $v){add_meta_box( 'pluginid', __( 'my plugin', 'myplugin_textdomain' ),array($this,'render_content'),$v['cpt'], array($v['variable']));} ? This give me Illegal offset type. The options is stored in the wp_option table. – dev-jim Sep 23 '12 at 8:37
@t.ck Your way of adding the var is not correct. Define the array before and just add the variable. $v['wrong']; vs. just $v. If you need to extract some part before, just do $n = $v['right'];. If you need to define an array, then just do it, save it in a var and add this var then as argument. – kaiser Sep 23 '12 at 8:40
I tried to define the var first like what you said.But I still get the error. – dev-jim Sep 23 '12 at 9:06

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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