Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 -
- 12:00 – 16:00 12:00 – 16:00 12:00 – 16:00 12:00 – 16:00 12:00 – 16:00 -

Supporter timezone: Europe/Zagreb (GMT+01:00)

Tagged: 

This topic contains 4 replies, has 0 voices.

Last updated by Dražen 1 day, 8 hours ago.

Assisted by: Dražen.

Author Posts
February 12, 2025 at 4:10 am #16693996

desireeM

Background of the issue:
wp-contentpluginswpml-media-translationclassesclass-wpml-media-attachment-by-url-query.php
line 99
Same issue also in line 163

PHP error Undefined array key 4
(or whatever number)

This happens bc you filter out array keys that already exist in line 74-77:

		$urls = array_filter( $urls, function( $url ) use ( $language ) {
			$index = md5( $language . $url );
			return ! array_key_exists( $index, $this->id_from_guid_cache );
		} );

Which then means the array isn't a list anymore, but an array with non-consecutive keys and you need to either change it to a foreach loop or check with isset in

$urls_count = count( $urls );
		for ( $i = 0; $i setItemToCache( null, 'id_from_guid_cache', $index );
		}

Correct code should be:

		foreach ( $urls as $url ) {
			$index = md5( $language . $url );
			$this->setItemToCache( null, 'id_from_guid_cache', $index );
		}

for line 160 respectively:

		foreach ( $pathes as $path ) {
			$index = md5( $language . $path );
			$this->setItemToCache( null, 'id_from_meta_cache', $index );
		}

WPML Media Translation 2.7.6

Symptoms:
PHP error 'Undefined array key 4' occurs when using the WPML Media Translation plugin.

Questions:
Fix your code

February 12, 2025 at 9:05 am #16694799

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

thanks for reporting, I have escalated this issue to 2nd tier to check and confirm.

I will update you soon on this.

Regards,
Drazen

February 12, 2025 at 10:37 am #16695714

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

our 2nd tier checked but we could not reproduce the issue. More details would be necessary. Even when we process the attachments, we always get at least one element in an array and no warnings or errors.

Can you please share more details and steps to reproduce the issue?

I am providing a test site if you can maybe show us the bug there: hidden link

Thanks,
Drazen

February 12, 2025 at 11:20 am #16695971

desireeM

Sorry, but seriously, this is such a basic issue even the most basic static analysis tool catches.
Purely from looking at your code it should be clear what the problem is, I even provided the fix (which somehow got messed up by your markdown/tag handling it seems)

>we always get at least one element in an array

I think you don't understand why your code produces that notice. It's not about whether you get one element or not. It's about whether the array is a list (= array with linear, monotonously increasing int keys) or not. Initially $urls is a list type, but then array_filter can make it into a generic array type where some keys can be missing, which then leads to the notice further down.

February 12, 2025 at 11:35 am #16696053

Dražen
Supporter

Languages: English (English )

Timezone: Europe/Zagreb (GMT+01:00)

Hello,

thank you for getting back, but as said we are not able to reproduce the same issue or cause such an error message which is why we need your further help.

We would need to confirm the issue before escalating it to our developers.

It would be very helpful if you could provide us a test case or steps to reproduce the issue in a new install and maybe PHP/DB versions.

Thanks,
Drazen