WordPress:无法访问回调函数内的 callback_args 键值

WordPress: cannot access callback_args key values inside callback function

我正在开发一个基于自定义 post 类型的简单插件。 post 类型注册正常。现在我想创建一些元框并通过回调参数传递一些 属性 值。这是我试过的:

function wpcd_add_dealer_meta_boxes() {
        add_meta_box(
            'dealer_first_name',
            'First Name',
            array($this, 'wpcd_meta_box_first_name_markup'),
            'dealers',
            'normal',
            'default',
            array(
                'id' => 'first_name',
                'name' => 'first_name',
                'type' => 'text',
                'placeholder' => 'Enter first name',
                'maxlength' => '30',
                'spellcheck' => 'true',
                'autocomplete' => 'off'
            )
        );
    }

这是我的带有参数的回调函数:

function wpcd_meta_box_first_name_markup($args) {
        $html = '<input ';
        $html.= 'type="' . $args->type . '" ';
        $html.= 'id="' .$args->id . '" ';
        $html.= 'name="' .$args->name . '" ';
        if( isset($args->required) && ($args->required == 'true' || $args->required == '1' ) ) {
            $html.= 'required ';
        }

        if( isset($args->placeholder) && $args->placeholder != '' ) {
            $html.= 'placeholder="' . esc_attr( $args->placeholder ) . '" ';
        }

        if( isset($args->maxlength) ) {
            $html.= 'maxlength="' . $args->maxlength . '" ';
        }

        if( isset($args->spellcheck) && ($args->spellcheck == 'true' ) ) {
            $html.= 'spellcheck="' . $args->spellcheck . '" ';
        }

        if( isset($args->autocomplete) && ($args->autocomplete == 'on' ) ) {
            $html.= 'autocomplete="' . $args->autocomplete . '" ';
        }

        $html.= '/>';

        echo $html;
    }

但我无法在函数内部获取 $args->id, $args->name 等值。准确的说,所有的值都是空的,这里我没有勾选if(isset(...))。而我所做的那些都被忽略了。

使用上面的代码,我希望输出以下标记:

<input type="text" id="first_name" name="last_name" required placeholder="Enter firstname" maxlength="30" autocomplete="off" spellcheck="true" />

而实际输出是

<input type="" id="" name="" />

属性 typeidname 未包含在 if(isset()) 块中,因此正在生成它们(具有空值)以及包含在 if(isset()) 块,只是被忽略了,就好像它们根本没有设置一样!

我遗漏了什么或做错了什么? 任何建议对我来说都是救命稻草。

如果您仔细检查 documentation for add_meta_box(),您会看到:

($callback_args (array) (Optional) Data that should be set as the $args property of the box array (which is the second parameter passed to your callback).

传递给回调的第一个参数是一个WP_Post对象。 second 是您的参数数组。所以,试试:

function wpcd_meta_box_first_name_markup($post, $args) { ...

然后按照您的预期访问您的参数:

$args['type']