admin管理员组文章数量:1279179
My function lets users delete their uploaded images in the front-end. However, they're only deleted in the Media Library. That means that they're still in wp-content/uploads/.
public static function removePhoto($ID, $attachment) {
if(isset(self::$data['user']['photos'][$attachment])) {
unset(self::$data['user']['photos'][$attachment]);
wp_delete_attachment($attachment);
core:updateUserMeta($ID, 'photos', self::$data['user']['photos']);
}
}
This is how how the images are added:
public static function uploadImage($file) {
require_once(ABSPATH.'wp-admin/includes/image.php');
$attachment=array('ID' => 0);
if(JBSUser::$data['user']['membership']['photos']<=0) {
JBSInterface::$messages[]=__('You have exceeded the number of photos', 'epic');
return $file;
}
if(!empty($file['name'])) {
$uploads=wp_upload_dir();
$filetype=wp_check_filetype($file['name'], null);
$filename=wp_unique_filename($uploads['path'], 'image-1.'.$filetype['ext']);
$filepath=$uploads['path'].'/'.$filename;
//validate file
if (!in_array($filetype['ext'], array('jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG'))) {
JBSInterface::$messages[]=__('Only JPG and PNG images are allowed.', 'epic');
} else if(move_uploaded_file($file['tmp_name'], $filepath)) {
//upload image
$attachment=array(
'guid' => $uploads['url'].'/'.$filename,
'post_mime_type' => $filetype['type'],
'post_title' => sanitize_title(current(explode('.', $filename))),
'post_content' => '',
'post_status' => 'inherit',
'post_author' => get_current_user_id(),
);
//add image
$attachment['ID']=wp_insert_attachment($attachment, $attachment['guid'], 0);
update_post_meta($attachment['ID'], '_wp_attached_file', substr($uploads['subdir'], 1).'/'.$filename);
//add thumbnails
$metadata=wp_generate_attachment_metadata($attachment['ID'], $filepath);
wp_update_attachment_metadata($attachment['ID'], $metadata);
} else {
JBSInterface::$messages[]=__('This image is too large for uploading.','epic');
}
}
return $attachment;
}
Update: Setting wp_delete_attachment
, true
- does not work.
I'm suspicious of: substr($uploads['subdir'], 1).'/'.$filename);
Which seems to upload the files to subfolder. But the images are still uploaded to the wp-content/uploads/ regardless if the "organize" setting is on or off.
Should this really effect the wp_delete_attachment?
My function lets users delete their uploaded images in the front-end. However, they're only deleted in the Media Library. That means that they're still in wp-content/uploads/.
public static function removePhoto($ID, $attachment) {
if(isset(self::$data['user']['photos'][$attachment])) {
unset(self::$data['user']['photos'][$attachment]);
wp_delete_attachment($attachment);
core:updateUserMeta($ID, 'photos', self::$data['user']['photos']);
}
}
This is how how the images are added:
public static function uploadImage($file) {
require_once(ABSPATH.'wp-admin/includes/image.php');
$attachment=array('ID' => 0);
if(JBSUser::$data['user']['membership']['photos']<=0) {
JBSInterface::$messages[]=__('You have exceeded the number of photos', 'epic');
return $file;
}
if(!empty($file['name'])) {
$uploads=wp_upload_dir();
$filetype=wp_check_filetype($file['name'], null);
$filename=wp_unique_filename($uploads['path'], 'image-1.'.$filetype['ext']);
$filepath=$uploads['path'].'/'.$filename;
//validate file
if (!in_array($filetype['ext'], array('jpg', 'JPG', 'jpeg', 'JPEG', 'png', 'PNG'))) {
JBSInterface::$messages[]=__('Only JPG and PNG images are allowed.', 'epic');
} else if(move_uploaded_file($file['tmp_name'], $filepath)) {
//upload image
$attachment=array(
'guid' => $uploads['url'].'/'.$filename,
'post_mime_type' => $filetype['type'],
'post_title' => sanitize_title(current(explode('.', $filename))),
'post_content' => '',
'post_status' => 'inherit',
'post_author' => get_current_user_id(),
);
//add image
$attachment['ID']=wp_insert_attachment($attachment, $attachment['guid'], 0);
update_post_meta($attachment['ID'], '_wp_attached_file', substr($uploads['subdir'], 1).'/'.$filename);
//add thumbnails
$metadata=wp_generate_attachment_metadata($attachment['ID'], $filepath);
wp_update_attachment_metadata($attachment['ID'], $metadata);
} else {
JBSInterface::$messages[]=__('This image is too large for uploading.','epic');
}
}
return $attachment;
}
Update: Setting wp_delete_attachment
, true
- does not work.
I'm suspicious of: substr($uploads['subdir'], 1).'/'.$filename);
Which seems to upload the files to subfolder. But the images are still uploaded to the wp-content/uploads/ regardless if the "organize" setting is on or off.
Should this really effect the wp_delete_attachment?
Share Improve this question edited Oct 12, 2021 at 8:52 xXx 2351 silver badge4 bronze badges asked Mar 17, 2015 at 22:36 dkldkl 1411 gold badge2 silver badges11 bronze badges 3 |4 Answers
Reset to default 3For future reference, if your attachment gets deleted but not your files
There is a filter wp_delete_file
, check if this filter is hooked on somewhere, if it returns null
your file won't be deleted but the attachment will
For me it was due to the WPML plugin, they duplicate attachments and then checks that all the attachments are deleted before actually deleting the file
You can either add this action in your theme/plugin, that will delete all translations when using $force_delete
in wp_delete_attachment
if ( ! function_exists( 'delete_attachment_translations' ) ) {
function delete_attachment_translations( $id ) {
remove_action( 'delete_attachment', 'delete_attachment_translations' );
$langs = apply_filters( 'wpml_active_languages', [] );
if ( ! empty( $langs ) ) {
global $wp_filter;
//We need to temporarly remove WPML's hook or they will cache the result and not delete the file
// Sadly they used a filter on a non global class instance, so we cannot access it via regular wp hook
$hooks = $wp_filter['wp_delete_file']->callbacks[10];
unset( $wp_filter['wp_delete_file']->callbacks[10] );
foreach ( $langs as $code => $info ) {
$post_id = apply_filters( 'wpml_object_id', $id, 'attachment', false, $code );
if ( $id == $post_id ) {
continue;
}
wp_delete_attachment( $post_id, true );
}
$wp_filter['wp_delete_file']->callbacks[10] = $hooks;
}
add_action( 'delete_attachment', 'delete_attachment_translations' );
}
add_action( 'delete_attachment', 'delete_attachment_translations' );
}
Or check this option in your WPML settings (But it applies to all posts types, not just attachments)
You need to provide wp_delete_attachment a value of true for it's second argument force_delete
. Your code needs to change to wp_delete_attachment( $attachment, true );
You forgot to add second parameter for the function wp_delete_attachment
<?php wp_delete_attachment( $attachmentid, $force_delete ); ?>
$attachmentid
(integer) (required) The ID of the attachment you would like to delete.
Default: None$force_delete (bool) (optional) Whether to bypass trash and force deletion (added in WordPress 2.9).
Default: false
Inside the wp-includes/post.php
in the wp_delete_attachment_files
there's a check to see if other attachments use the same file. This is what was happening in my case. Because I initially deleted the file manually but not the attachment, when I was uploading it again, the same name as before was used.
So when the delete function was called for the newly uploaded attachment id, the old attachment had a reference to the same file, so the DB attachment was deleted and not the file.
Make sure you manually delete all the attachments that could point to the same file from the media library.
本文标签: wpdeleteattachment doesn39t delete images in wpcontentuploads
版权声明:本文标题:wp_delete_attachment doesn't delete images in wp-contentuploads 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741275557a2369707.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
media_handle_upload
though (after sanitization) - which basically callswp_handle_upload
inside – jave.web Commented Oct 10, 2018 at 9:46