PHP 8.1 Uncaught TypeError with plugin

I have recently updated my Wordpress sites to PHP 8.1 — and I have run into a problem with a plugin that I am using on several sites. The plugin is no longer being actively maintained – but I would like to keep it, and I think the problem is fixable, but I don’t have the coding skill needed to do it.

Here’s the issue – I’m seeing this error:

PHP Fatal error:  Uncaught TypeError: Cannot access offset of type string on string in [name of file]:250

Here is the code section of the file that throwing the error:

public function additional_fields_meta_box_output( $post ) {
		$values = $this->get_additional_field_values( $post->ID );

		$names = simple_links()->get_additional_fields();

		if ( is_array( $names ) ) {
			foreach ( $names as $key => $value ) {
				if ( empty( $values[ $value ] ) ) {
[250]				$values[ $value ] = null;               
				}

				printf(
					'<p>%s:  <input type="text" name="%s[%s]" value="%s" size="70" class="SL-additonal-input">',
					esc_html( $value ),
					esc_attr( self::ADDITIONAL_FIELDS ),
					esc_attr( $value ),
					esc_attr( $values[ $value ] )
				);
			}
		}

		if ( isset( $this->meta_box_descriptions['additional_fields'] ) ) {
			echo '<p>' . wp_kses( $this->meta_box_descriptions['additional_fields'], array( 'code' => array() ) ) . '</p>';

			

The line throwing the error is #250 (noted above)

So basically, PHP 8.1 wants to see something different than null.

The code is creating meta boxes that can be edited with extra information on the back end; everything is still working on the front end, but the error is generated on the edit screen for the post type created by this plugin.

So if anyone can suggest specific changes I can make to the code to fix this… I would be very appreciative.

The code is expecting $values to be an array. It is however a string. As a first debugging step, find out what it actually is. Add the following line of code after the call to $this->get_additional_field_values( $post->ID ); -

echo '<pre>'; var_dump($values); echo '</pre>';

Thanks!

I get this output;

string(0) ""

For more info, this is a plugin that creates a custom post type that also has a meta box function – that is, I can create additional meta boxes to store information specific to the post.

I have determined that this error is being thrown only for older posts that were either created before the creation of the metaboxes, or imported from another source (without the metaboxes).

(This means that the fix isn’t really critical – as it is only tied to back-end operations and isn’t tied to any loss of data, but this wasn’t happening with PHP 7.4)

Then the call to $this->get_additional_field_values(…) is returning an empty string, when is should return an empty array.

It would take seeing what the code for $this->get_additional_field_values() is, to make sure, but either initializing the return value to an empty array before fetching the data OR detecting an empty string as the data and returning an empty array would be how to fix this.

Here’s the code I think you are looking for:

	public function get_additional_field_values( $post_id ) {
		return get_post_meta( $post_id, self::ADDITIONAL_FIELDS, true );
	}

get_post_meta() is a wordpress function - get_post_meta() | Function | WordPress Developer Resources

For the parameters being used, it should return a single value in all cases. What does the var_dump() show for the cases where there is a metabox?

In those cases I see something like this:

array(3) {
  ["Field Name 1"]=>
  string(14) "field1 data"
  ["Field Name 2"]=>
  string(15) "field2 data"
  ["Field Name3"]=>
  string(0) ""
}

In the above example, the third meta field is blank – hence the empty string value (but no error in that case).

It’s returning an array, rather than a string, so when the code references it, the data type is the expected type.

In previous php versions, the existing code produces a warning, and if these were hidden due to php’s error related settings, the code ‘worked’.

This produces a fatal error in php8+. To correct this, after the $values = $this->get_additional_field_values( $post->ID ); line of code, add the following -

$values = $values === '' ? [] : $values;

Perfect! Thank you so much!

Sponsor our Newsletter | Privacy Policy | Terms of Service