I am developing a simple plugin based custom post type. The post type is registered fine. Now I want to create some meta boxes and pass a few property values via callback arguments. This is what I tried:
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'
)
);
}
And here is my callback function with an argument:
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;
}
But I cannot get the values like $args->id, $args->name
etc inside the function. To be precise, all the values are empty, where I did not check if(isset(...))
. And where I did those are simply ignored.
With my above code I am expecting the following markup as output:
<input type="text" id="first_name" name="last_name" required placeholder="Enter firstname" maxlength="30" autocomplete="off" spellcheck="true" />
while the actual output is
<input type="" id="" name="" />
Attributes type
, id
and name
are not wrapped inside an if(isset())
block so they are being generated (with empty values) and whatever is wrapped inside if(isset())
block, are simply being ignored, as if they are not set at all!
What I am missing or doing wrong? Any suggestion would be a life saver for me.
$args['type']
etc. ?$args['type']
already and got this:Fatal error: Cannot use object of type WP_Post as array in /var/www/wp-projects/subratasarkar.com/wp-content/plugins/product-dealer/product-dealer.php on line 68
@MattGibson would you please tell me the right way to do this?