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.

Our next available supporter will start replying to tickets in about 1.92 hours from now. Thank you for your understanding.

This topic contains 10 replies, has 2 voices.

Last updated by Santiago 10 months, 3 weeks ago.

Assisted by: Osama Mersal.

Author Posts
June 1, 2023 at 11:31 pm #13758093

Santiago

Hello there,
I have a question about the:
WPML - SETTINGS - WHEN DELETING A POST, DELETE TRANSLATIONS AS WELL
Any way to use this feature only for MEDIA?
I need to delete all the MEDIA translations when the primary language is deleted.
BUT I DO NOT WANT TO DO THE SAME WITH THE REST OF MY CONTENT.
PRODUCTS, PAGES, POSTS, ETC.
Any way to do this?
Thank you in advance.-

June 4, 2023 at 9:51 am #13765561

Osama Mersal
Supporter

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Thanks for contacting WPML forums support. I'll be glad to help you today.

First of all, sorry for the late reply due to a higher workload. This option can't work on the exact post type. If enabled, it will work on all your post types.

The workaround for your case is to delete the media manually in all languages.

Best regards,
Osama

June 4, 2023 at 6:42 pm #13766539

Santiago

Hello and thank you for your update.

I do perfectly understand what you mentioned.

But my point here it's to find a solution to this problem.

1.- Don't get me wrong, the option to delete all translations together can't be the same for all post types, media files, etc. WPML should have a specific feature just for the media, many people need that for sure. How we can share this with your dev team to analyze and potentially add the feature in the next versions?

2.- In the middle of time, how we can find a code snippet to find a temporary solution?
Maybe something like this:

function delete_language_media_items($attachment_id) {
    $post = get_post($attachment_id);
    $post_languages = apply_filters('wpml_post_language_details', null, $post->ID);
    
    if (!empty($post_languages)) {
        foreach ($post_languages as $language) {
            $attachments_in_language = apply_filters('wpml_get_content_in_language', null, $language['language_code'], array('element_id' => $post->ID, 'element_type' => 'post_attachment'));
            
            if (!empty($attachments_in_language)) {
                foreach ($attachments_in_language as $attachment) {
                    wp_delete_attachment($attachment->element_id, true);
                }
            }
        }
    }
}

add_action('delete_attachment', 'delete_language_media_items');

But looks like is not working.
For that reason, I contact your team.
Someone from your side can help with a solution?
For sure many people are going to appreciate this, 100%

Thank you in advance.-

June 5, 2023 at 8:07 am #13767755

Osama Mersal
Supporter

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

I consulted our 2nd tier support team regarding this issue, and I'll update you as soon as I get their reply.

Best regards,
Osama

June 5, 2023 at 10:12 am #13768903

Osama Mersal
Supporter

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Our 2nd tier support team checked your code and made some changes in your code. Please try the following snippet.

function delete_language_media_items($attachment_id) {
	$trid = apply_filters( 'wpml_element_trid', NULL, $attachment_id, 'post_attachment' ); //get element TRID
	$translations = apply_filters( 'wpml_get_element_translations', NULL, $trid, 'post_attachment' ); //get translations by TRID
    foreach ($translations as $code => $element) { //iterate over translation elements
        if ($attachment_id != $element->element_id){ //run only for translated languages and hand-over control to WordPress to delete AFTERWARDS the original
            remove_action('delete_attachment', 'delete_language_media_items'); //remove yourself to prevent endless loop
            wp_delete_attachment($element->element_id); //remove attachment
        }
    }
}
add_action('delete_attachment', 'delete_language_media_items');

Best regards,

June 6, 2023 at 12:36 am #13774819

Santiago

Hello and thank you in advance for the new code you have shared.
I'm testing the code and looks like works randomly.

For my tests, I have 14 languages.
And I have uploaded 10 images.
I can see the 140 entries disappear on MySQL when I run:
SELECT * FROM wp_posts WHERE post_type = 'attachment';

Them I deleted the 10 images.
And all the time a few images are still showing in the upload folder and in different languages too.
The same situation with the MySQL entries, part of the 140 is still there.

Some idea about how to make the snippet works always?
Thank you again.-

June 6, 2023 at 1:25 am #13774839

Santiago

I can see the next error on my debug.log when I try to delete media and can't delete all the files:

[06-Jun-2023 01:22:59 UTC] WordPress database error Deadlock found when trying to get lock; try restarting transaction for query DELETE FROM `gl_options` WHERE `option_name` = 'wpml.WPML\\TM\\Settings\\ProcessNewTranslatableFields.lock' made by require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('wp_loaded'), WP_Hook->do_action, WP_Hook->apply_filters, WPML\LIB\WP\Hooks::WPML\LIB\WP\{closure}, WPML\FP\Promise->resolve, WPML\FP\Right->chain, WPML\FP\Right->map, WPML\BackgroundTask\BackgroundTaskLoader->WPML\BackgroundTask\{closure}, WPML\BackgroundTask\BackgroundTaskLoader->getSerializedTasks, WPML\FP\Fns::__callStatic, call_user_func_array, WPML\FP\Fns::WPML\FP\{closure}, call_user_func_array, WPML\FP\Fns::WPML\FP\{closure}, array_map, WPML\BackgroundTask\BackgroundTaskLoader->WPML\BackgroundTask\{closure}, WPML\BackgroundTask\BackgroundTaskViewModel::get, WPML\Utilities\Lock->create, WPML\Utilities\Lock->release, delete_option

Many files are still in the upload folder.
And looks like every entry is deleted from the SQL.

June 6, 2023 at 11:27 am #13778053

Osama Mersal
Supporter

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

Our 2nd tier support team updated the snippet and tested it on a clean installation.


function delete_language_media_items($attachment_id) {
    $trid = apply_filters( 'wpml_element_trid', NULL, $attachment_id, 'post_attachment' ); //get element TRID
    $translations = apply_filters( 'wpml_get_element_translations', NULL, $trid, 'post_attachment' ); //get translations by TRID
    foreach ($translations as $code => $element) { //iterate over translation elements
        if ($attachment_id != $element->element_id){ //run only for translated languages and hand-over control to WordPress to delete AFTERWARDS the original
            remove_action('delete_attachment', 'delete_language_media_items'); //remove yourself to prevent endless loop
            wp_delete_attachment($element->element_id); //remove attachment
            add_action('delete_attachment', 'delete_language_media_items');
        }
    }
}
add_action('delete_attachment', 'delete_language_media_items');

On our test site, and snippet worked correctly without any issues. Please check it and let me know if it works correctly on your site.

Best regards,

June 9, 2023 at 12:07 am #13796247

Santiago

Thank you again for your update.

Looks like the new line:
add_action('delete_attachment', 'delete_language_media_items');
Makes the difference.

Any plans to offer this feature in your next versions?
How someone can request features like this one?
For sure this can be very helpful for many ones.

Thank you.-

June 9, 2023 at 2:09 pm #13800353

Osama Mersal
Supporter

Languages: English (English ) Arabic (العربية )

Timezone: Africa/Cairo (GMT+03:00)

Hi,

I created a feature request ticket for you, and our dev team will check it and see if it would be a new feature in WPML.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.