Skip to content Skip to sidebar

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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Europe/Sofia (GMT+02:00)

Tagged: ,

This topic contains 5 replies, has 0 voices.

Last updated by George Botsev 7 months, 1 week ago.

Assisted by: George Botsev.

Author Posts
April 6, 2025 at 11:15 am #16901184

desireeM

Background of the issue:
In my theme I need to hook onto WP core "wp_delete_file"
This filter expects a string param and return type:
hidden link

But you return null: wp-contentpluginssitepress-multilingual-cmsclassesmediaclass-wpml-attachment-action.php line 180

<code>
public function delete_file_filter( $file ) {
if ( $file ) {
$file_name = $this->get_file_name( $file );
$scaled_file_name = $this->get_file_name( $file, true );
$sql = "SELECT pm.meta_id, pm.post_id FROM {$this->wpdb->postmeta} AS pm WHERE pm.meta_value IN ( %s, %s ) AND pm.meta_key='_wp_attached_file'";
$attachment_prepared = $this->wpdb->prepare( $sql, $file_name, $scaled_file_name );
$attachment = $this->wpdb->get_row( $attachment_prepared );

if ( ! empty( $attachment ) ) {
$file = null;
}
}

return $file;
}
</code>

Which then leads to a PHP fatal error:
TypeError: is_file() expects parameter 1 to be a valid path, null given

The code in my functions.php

<code>
add_filter('wp_delete_file', function ( $file ) {
if ( ! is_file( $file ) ) {
return $file;
}

// some custom logic

return '';
}, 20);
</code>

I know I can validate $file for null in my code, however that's not the point. Please fix your code to return empty string isntead of null.

Symptoms:
Fatal error: wp_delete_file expects string return type you return null - TypeError: is_file() expects parameter 1 to be a valid path, null given. The code in WPML returns null instead of an empty string, leading to a PHP fatal error.

Questions:
Can you fix your code to return an empty string instead of null

April 7, 2025 at 12:00 pm #16904099

George Botsev
WPML Supporter since 02/2014

Languages: English (English )

Timezone: Europe/Sofia (GMT+02:00)

Hello and thank you for your report.
I will bring this up to our developers, however could you please provide more information about the issue, as you have not submitted the extra debug information for this ticket and I have to guess and assume things about your current setup.
For example, is there a specific PHP version that is causing the problem?

At present I can only reproduce a deprecated warnings in my test setup and not a fatal error.
We look extremely seriously on fatal error messages.

April 8, 2025 at 6:42 am #16906899

desireeM

Add `declare(strict_types=1);` on top of your functions.php (or wherever you added my sample code snippet), then you'll get the fatal

April 8, 2025 at 6:54 am #16906926

George Botsev
WPML Supporter since 02/2014

Languages: English (English )

Timezone: Europe/Sofia (GMT+02:00)

Okay, I was asking in a more general manner on how this could happen - like if a plugin, theme or a PHP version is explicitly making the fatal error.

We ask these questions in order to determine the case impact and severity.

The issue, as I mentioned above, is escalated to our developers team.

April 8, 2025 at 8:01 am #16907290

desireeM

What do you mean in a more general manner?

If a plugin/theme uses that filter and uses strict types, it will result in that fatal.
e.g. if a theme or plugin has code like this:

declare(strict_types=1);
add_filter('wp_delete_file', function ( $file ) {
if ( ! is_file( $file ) ) {
return $file;
}

// some custom logic

return '';
}, 20);

you'll get a fatal error when deleting an attachment that is still used in translations/exists in translations, due to the code in wp-content/plugins/sitepress-multilingual-cms/classes/media/class-wpml-attachment-action.php line 180

April 8, 2025 at 8:23 am #16907390

George Botsev
WPML Supporter since 02/2014

Languages: English (English )

Timezone: Europe/Sofia (GMT+02:00)

Yes, I understand all too good about forcing string types.
This is rather rare set of circumstances - hooking on `wp_delete_file`, having strict_types and so on.

I understood your case and added all the details to the ticket in our internal system that are going to be handled by a developer.