WP_DELETE_ATTACHING不删除wp-content/ploads/中的图像

时间:2015-03-17 作者:dkl

我的功能允许用户在前端删除上传的图像。但是,它们仅在媒体库中删除。这意味着它们仍在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附件?

4 个回复
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设置中选中此选项(但它适用于所有帖子类型,而不仅仅是附件)

enter image description here

SO网友:DiverseAndRemote.com

您需要提供wp_delete_attachment 第二个参数的值为trueforce_delete. 您的代码需要更改为wp_delete_attachment( $attachment, true );

SO网友:grandcoder

您忘记为函数添加第二个参数wp_delete_attachment

 <?php wp_delete_attachment( $attachmentid, $force_delete ); ?> 
$attachmentid(整数)(必需)要删除的附件的ID
默认值:无

$force\\u delete(bool)(可选)是否绕过垃圾并强制删除(添加在WordPress 2.9中)
默认值:false

SO网友:Calara Ionut

内部wp-includes/post.phpwp_delete_attachment_files 检查其他附件是否使用相同的文件。这就是我的情况。因为我最初手动删除了文件,但没有删除附件,所以当我再次上载时,使用了与以前相同的名称。

因此,当为新上载的附件id调用delete函数时,旧附件引用了同一个文件,因此删除了DB附件,而不是文件。

请确保手动删除媒体库中可能指向同一文件的所有附件。

结束

相关推荐

将WordPress附件导入wp-Content/Uploads/中的自定义目录

我希望有人能阐明我试图解决的这个问题,即使只是简单地改变我的思维过程,告诉我如何解决这个问题。我正在将一个基于CakePHP的定制CMS web应用程序导入WordPress,方法是遍历数据库,进行大量SQL查询,构建数组,最后输出必要的XML文件,以便使用WordPress导入工具导入。目前我已经很好地导入了类别、标签和帖子/页面,没有问题,但问题似乎出在图像上。基于CakePHP的CMS中当前的图像格式是images/year/month/day/post_id/image_name.ext据我所知,