Hi!
Wordpress gets an error when you define the default parameter of a field to 'max' or 'Max'.
add_action( 'cmb2_init', 'yourprefix_register_demo_metabox' );
function yourprefix_register_demo_metabox() {
$cmb = new_cmb2_box( array(
'id' => 'box_id',
'title' => 'Title',
) );
$cmb->add_field( array(
'name' => 'field_one',
'id' => 'field_one',
'type' => 'text',
'default' => 'max',
) );
$cmb->add_field( array(
'name' => 'field_two',
'id' => 'field_two',
'type' => 'hidden',
'default' => 'Max',
) );
}
Catchable fatal error: Object of class CMB2_Field could not be converted to string in /home/root/mysite.com/wp-includes/formatting.php on line 1025
Not quite sure why, but it seems like in this case, "max" is turning into the default callback, aka default_cb in the args array deep in CMB2, instead of the default value.
I know that max is a PHP core function, and I was able to recreate similar issues with other PHP core functions.
That said, changing it to "Maximum" worked fine for me.
Overall feels like a real edge case, and some odd behavior truth be told.
Yes, this is an unfortunate side-effect of allowing the 'default' field parameter to also be a callback. That functionality is now deprecated in favor of using the 'default_cb' field parameter, and my intention is to remove that functionality from the 'default' field parameter in a few releases, but need to give the deprecation enough time, since we try to be backwards compatible.
That being said, I'd suggest using a callback to define your default so that you can bypass the bug:
add_action( 'cmb2_init', 'yourprefix_register_demo_metabox' );
function yourprefix_register_demo_metabox() {
$cmb = new_cmb2_box( array(
'id' => 'box_id',
'title' => 'Title',
) );
$cmb->add_field( array(
'name' => 'field_one',
'id' => 'field_one',
'type' => 'text',
'default_cb' => 'yourprefix_set_default_to_max',
) );
$cmb->add_field( array(
'name' => 'field_two',
'id' => 'field_two',
'type' => 'hidden',
'default_cb' => 'yourprefix_set_default_to_max',
) );
}
function yourprefix_set_default_to_max() {
return 'max';
}
The problem still persists. For over 3 years.
I'm running into a similar issue with 'default' => 'Link', it's throwing an error in Xdebug.
$cmb->add_field( array(
'name' => __( 'Medium', 'cmb2' ),
'desc' => __( 'Link, Banner, CPC', 'cmb2' ),
'id' => 'utm_medium',
'default' => 'Link',
'type' => 'text'
) );
What sort of error is being thrown for you @toddbarnett ?
@tw2113 Warning: link() expects exactly 2 parameters, 3 given in CMB2_Base.php on line 315
@tw2113 Looks like back in '16 you guys had a plan to remove 'default' and use 'default_cb' instead is that right? @jtsternberg comment is a little confusing he say's 'default' is to be replaced with 'default' but in his example, he has 'default_cb' and the docs still refer to 'default'? Furthermore, the error I'm facing only happens when there is no data saved to the post meta yet.
Wow, good catch, can't believe no-one has pointed that out before. Fixed it. Also, please point me to the docs where it is default vs default_cb so I can update.
Once you change to default_cb, you will not get that error.
Justin has chimed in as well with his parts. Basically from what I see, it was being treated as a callback spot for the link() function over at https://www.php.net/manual/en/function.link.php.
@tw2113, @jtsternberg, Wow, fast reply. You guys are awesome. Thank you!
Ah yah. So the 'default' property/usage will not go away... just the usage of it as a callback. If you want a callback for the default, you will need to use default_cb
Okay, I ended up using 'default_cb' because having the word 'Link' as the 'default' text was throwing a PHP link() function error as Michael has pointed out.
Most helpful comment
Justin has chimed in as well with his parts. Basically from what I see, it was being treated as a callback spot for the
link()function over at https://www.php.net/manual/en/function.link.php.