如何通过$CALLBACK_ARGS为ADD_META_BOX传递变量

时间:2012-09-22 作者:dev-jim

我试图通过以下方式将变量传递给回调函数add_meta_box. 我知道有$callback_args 可用于传递变量的。但不知怎么的,我就是不能让它在课堂上发挥作用。这是我的代码:

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();
谢谢

2 个回复
最合适的回答,由SO网友:kaiser 整理而成

有时,一个例子能说出一千多个单词。这里有一个小插件可以指导您:

<?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;
如果你想知道,我是如何识别那些用于静态init 要加载的函数,然后take a look at my »current admin info« plugin here.

enter image description here

SO网友:Debbie Kurth

最简单的方法是,只需记住在原始数组的子集上传递的参数。当您使用类结构时,基本的非类方法仍然有效。

因此,要在add\\u meta\\u box调用中传递参数,如下所示:

  $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
  }
请访问:https://developer.wordpress.org/reference/functions/add_meta_box/

结束

相关推荐

在编辑帖子或页面时,如何创建带有编辑说明的HTML内容的Metabox?

我想在屏幕中添加一个带有HTML内容的自定义元框新建和编辑帖子/页面(/wp-admin/post.php?post=POST_ID&action=edit), 这样我就可以显示一个编辑指南列表,供我的编辑遵循。我不想在许多不同的地方编辑代码来更新指令。我正在寻找一些更通用的东西,我可以在一个地方编辑。我怎样才能做到这一点?