Make Wordpress wp_delete_attachment delete files from a pre-defined custom folder
<p>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 </p>

<pre><code>define( 'UPLOADS', 'mycustomfolder' );
</code></pre>

<p>in the wp-config.php.</p>

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

<pre><code>add_filter('upload_dir', 'my_upload_dir');
$uploaded_file = wp_handle_upload($_FILES['xxxx_image'], $upload_overrides);
remove_filter('upload_dir', 'my_upload_dir');
</code></pre>

<p>We are using this to remove all attachments from a particular post:</p>

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

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

<p>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).</p>

<p>Here's the code for our my_upload_dir function:</p>

<pre><code>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;
}
</code></pre>

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