Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
You are encountering a PHP error 'Undefined array key 4' in the WPML Media Translation plugin. This error occurs because the array keys are filtered out, making the array have non-consecutive keys. This issue is present in the file wp-content/plugins/wpml-media-translation/classes/class-wpml-media-attachment-by-url-query.php at lines 99 and 163.
Solution:
We recommend modifying the code to handle arrays with non-consecutive keys correctly. Instead of using a for loop, switch to a foreach loop. Here is the corrected code:

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

And for line 160 respectively:

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

Please note that this solution might be outdated or not applicable to your specific case. We highly recommend checking related known issues at https://wpml.org/known-issues/, verifying the version of the permanent fix, and confirming that you have installed the latest versions of themes and plugins. If the issue persists, please open a new support ticket.

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.

Tagged: 

This topic contains 9 replies, has 0 voices.

Last updated by Dražen 1 day, 23 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

February 14, 2025 at 2:16 am #16704824

desireeM

If you also want me to write your PHPUnit tests let me know 😉 I already provided you the fix for your code, unless you pay me I can't do even more

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

Why? The issue is self-explanatory on a purely static level without executing the code at all.

February 14, 2025 at 7:14 am #16705073

Dražen
Supporter

Languages: English (English )

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

Hello,

okay, thanks for getting back and reporting this.

I will share your reply with our 2nd tier, but we can not promise that we will be able to confirm and fix this in future versions, without having more required information.

Kind regards,
Drazen

February 14, 2025 at 9:42 am #16705534

Dražen
Supporter

Languages: English (English )

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

Hello,

the issue will be escalated to our developers for a fix.

Thanks and kind regards,
Drazen

February 16, 2025 at 10:14 am #16710338

desireeM

ok

February 17, 2025 at 7:20 am #16711755

Dražen
Supporter

Languages: English (English )

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

Hello,

thanks, I will be closing this ticket for now, since you already have a workaround that you provided.

Kind regards,
Drazen