Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Wordpress wp_delete_attachment delete files from a pre-defined custom folder

We have created a custom post type which allows our client to upload files to a folder outside of the standard Wordpress uploads folder (wp-content/upload-assets). These files are to be handled seperately from the standard wp-content/uploads folder and this is why we CANNOT use

define( 'UPLOADS', 'mycustomfolder' );

in the wp-config.php.

Instead we use this to temporarily change the uploads folder to wp-content/upload-assets:

add_filter('upload_dir', 'my_upload_dir');
$uploaded_file = wp_handle_upload($_FILES['xxxx_image'], $upload_overrides);
remove_filter('upload_dir', 'my_upload_dir');

We are using this to remove all attachments from a particular post:

add_filter('upload_dir', 'my_upload_dir');
$attachments = get_posts( array(
            'post_type'      => 'attachment',
            'posts_per_page' => -1,
            'post_status'    => 'any',
            'post_parent'    => $pid
) );

foreach ( $attachments as $attachment ) {
   if ( false === wp_delete_attachment( $attachment->ID, true ) ) {
      echo 'Attachment could not be deleted.';
   }
}
remove_filter('upload_dir', 'my_upload_dir');

wp_delete_attachment should also delete all associated files from disk but it doesn't work because our files are in our custom folder (wp-content/upload-assets).

Here's the code for our my_upload_dir function:

function my_upload_dir($upload) {
    $upload['subdir']   = '';
    $upload['basedir']  = WP_CONTENT_DIR;
    $upload['baseurl']  = WP_CONTENT_URL;
    $upload['path']     = $upload['basedir'] . '/upload-assets';
    $upload['url']      = $upload['baseurl'] . '/upload-assets';
    return $upload;
}

How do we make wp_delete_attachment remove the files in our custom wp-content/upload-assets folder?

like image 702
Richard Tinkler Avatar asked Jun 30 '16 07:06

Richard Tinkler


2 Answers

Hi You can do this way if want to delete the file, but it will not remove all post meta fields, taxonomy, comments, etc. associated with the attachment.

Hope this help

foreach ( $attachments as $attachment ) {
  if ( false === wp_delete_attachment( $attachment->ID, true ) ) {
    $file = get_attached_file( $attachment->ID );
    $file = str_replace( "uploads", "upload-assets", $file);
     wp_delete_file( $file );
   }
 }

I have not tested this code, but i hope it should work

like image 124
Deepti chipdey Avatar answered Nov 08 '22 22:11

Deepti chipdey


Staying in the domain of current question you can use "get_attached_file" filter to directly alter the file path string used to grab the path of file to be deleted. Specifically add this function to your functions.php

function sr_update_uploads_directory( $file )
{
    return str_replace( "uploads", "upload-assets", $file) // Change path to upload-assets from uploads
}
add_filter( 'get_attached_file', 'sr_update_uploads_directory' );

Don't have time for testing currently. So, please excuse me for that. Hope it helps. :)

like image 3
shazyriver Avatar answered Nov 08 '22 22:11

shazyriver