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: Custom Work, Feature request
This topic contains 10 replies, has 2 voices.
Last updated by Santiago 1 year, 8 months ago.
Assisted by: Osama Mersal.
Author | Posts |
---|---|
June 1, 2023 at 11:31 pm #13758093 | |
Santiago |
Hello there, |
June 4, 2023 at 9:51 am #13765561 | |
Osama Mersal Supporter
Languages: English (English ) Arabic (العربية ) Timezone: Africa/Cairo (GMT+02: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, |
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? 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. Thank you in advance.- |
June 5, 2023 at 8:07 am #13767755 | |
Osama Mersal Supporter
Languages: English (English ) Arabic (العربية ) Timezone: Africa/Cairo (GMT+02: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, |
June 5, 2023 at 10:12 am #13768903 | |
Osama Mersal Supporter
Languages: English (English ) Arabic (العربية ) Timezone: Africa/Cairo (GMT+02: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. For my tests, I have 14 languages. Them I deleted the 10 images. Some idea about how to make the snippet works always? |
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. |
June 6, 2023 at 11:27 am #13778053 | |
Osama Mersal Supporter
Languages: English (English ) Arabic (العربية ) Timezone: Africa/Cairo (GMT+02: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: Any plans to offer this feature in your next versions? Thank you.- |
June 9, 2023 at 2:09 pm #13800353 | |
Osama Mersal Supporter
Languages: English (English ) Arabic (العربية ) Timezone: Africa/Cairo (GMT+02: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. |