我的功能允许用户在前端删除上传的图像。但是,它们仅在媒体库中删除。这意味着它们仍在wp内容/上传/中。
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\']);
}
}
这是如何添加图像的:
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;
}
更新:设置
wp_delete_attachment
,
true
- 不起作用。
我怀疑:substr($uploads[\'subdir\'], 1).\'/\'.$filename);
似乎要将文件上载到子文件夹。但图像仍会上载到wp内容/上载/无论;组织“;设置为打开或关闭。
这是否真的会影响wp\\u delete\\u附件?
SO网友:Tofandel
如果您的附件被删除但文件未被删除,以供将来参考
有一个过滤器wp_delete_file
, 检查此过滤器是否挂在某处,如果它返回null
不会删除您的文件,但会删除附件
对我来说,这是由于WPML插件,他们复制了附件,然后在实际删除文件之前检查是否删除了所有附件
您可以在主题/插件中添加此操作,这将在使用时删除所有翻译$force_delete
在里面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\' );
}
或者在WPML设置中选中此选项(但它适用于所有帖子类型,而不仅仅是附件)