Hi @tristup,
The error is caused by insufficient checks for $metabox_value
.
In the screenshot I shared previously, you can see that the default value is null
.
However, just after that, there is some code that will check if $metabox_value
is empty which is good.
The code:
<?php
if ( empty ( $metabox_value) && array_key_exists( 'default_value', $metabox ) ) {
$metabox_value = $this->get_default_value( $metabox['default_value'], $post_id );
}
The problem with this is that the new default value for $metabox_value
will only be set if array_key_exists( 'default_value', $metabox )
is also true
.
In the case of @Jloupf and myself, array_key_exists( 'default_value', $metabox )
is false
which means the code block above does not happen and $metabox_value
stays as null
.
This is because $metabox
does not contain default_value
.
Here is what I see when I var_dump($metabox)
:
array(6) {
["id"]=> string(37) "_snippet_article_alternative_headline",
["name"]=> string(19) "alternativeHeadline",
["label"]=> string(20) "Alternative Headline",
["description"]=> string(38) "A secondary title of the CreativeWork.",
["schema_type"]=> string(4) "Text",
["type"]=> string(4) "text"
}
You can see that the default_value
key is missing which is why the conditional statement fails to run.
All of the following code assumes that $metabox_value
is not null
and does not perform any checks to handle a possible empty or null
value.
take this code in the same renderMeta
method for example:
you can see here there are no checks if $metabox_value
is an array
or not, it just assumes it is which is why there are PHP errors.
The simple fix is to update the conditional statement that sets a new default value for $metabox_value
to ensure it sets an appropriate value, or just check if they are empty before output/reference.
If you are leveraging PHP 8 features, you can change
value="<?php echo esc_attr( $metabox_value['name'] ); ?>" class="postbox">
to
value="<?php echo esc_attr( $metabox_value['name'] ?? '' ); ?>" class="postbox">
to solve the problem.
Please take the time to read Tco_Snippet_Meta_Box::renderMeta
and understand the logic for the default values on $metabox_value
and how the code is prone to error.
Thanks