How to pass variable via $callback_args for add_meta_box

I am trying to pass a variable to the callback function through add_meta_box. I know that there is $callback_args that can be used to pass the variable. But somehow I just can’t get it to work within a class. This is my code:

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,
      //the $param is some value that is saved in my option table.
      array('test' => $param) 
    );
  }

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

Thanks.

Related posts

Leave a Reply

2 comments

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

    <?php
    ! 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()
        {
            add_meta_box(
                // ID
                 'wpse65981_meta_box_example'
                // Title/"hndle"-bar
                ,__( 'WPSE Meta Box Headline', 'example_textdomain' )
                // Callback
                ,array( $this, 'render_meta_box_content' )
                // post_type
                ,'post'
                // context
                ,'advanced'
                // priority
                ,'high'
                // 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

  2. The simplest way to do this is, just remember the arguments being passed on a subset of the original array. While you are using a class structure, the basic non-class methodology still works.

    So to pass an argument within the add_meta_box call, would be like this:

      $args = array('Index'=>'1');
      add_meta_box( 'mmd_listings_List_Group1',
               __( 'Title of Meta Box' ),
                  'MyCallBack',
                  'custom post name',
                  'normal',
                  'high',
                   $args    <========== array with arguments
                 );
    
    
    
     function MyCallBack($post_ID, $Arg)
      {
      $Index = $Arg['args']['Index'];   <=========== your argument in the function
      }
    

    Check it out on:
    https://developer.wordpress.org/reference/functions/add_meta_box/