If you want to delete media from WordPress’ media library when you delete any media from rtMedia’s activity or media page, paste the below code in your theme’s functions.php
file.
if ( ! function_exists( 'rtmedia_custom_code_delete_attachment' ) ) {
/**
* Check whether media files are being deleted from WordPress.
*
* @param int $post_id Attachment post ID.
* @return void
*/
function rtmedia_custom_code_delete_attachment( $post_id ) {
if ( empty( $post_id ) ) {
return;
}
global $rt_wp_delete_attachments;
if ( empty( $rt_wp_delete_attachments ) || ! is_array( $rt_wp_delete_attachments ) ) {
$rt_wp_delete_attachments = array();
}
$rt_wp_delete_attachments[ $post_id ] = 1;
}
}
add_action( 'delete_attachment', 'rtmedia_custom_code_delete_attachment', 1 );
if ( ! function_exists( 'rtmedia_custom_code_before_delete_media' ) ) {
/**
* Get media ID to be deleted inside global, to access it after deletion.
*
* @param int $rtmedia_id Media ID related to rtMedia.
* @return void
*/
function rtmedia_custom_code_before_delete_media( $rtmedia_id ) {
if ( empty( $rtmedia_id ) ) {
return;
}
$media_id = rtmedia_media_id( $rtmedia_id );
if ( ! empty( $media_id ) ) {
global $rt_media_to_be_deleted, $rt_wp_delete_attachments;
if ( ! empty( $rt_wp_delete_attachments[ $media_id ] ) ) {
return;
}
$rt_media_to_be_deleted = array( $rtmedia_id => $media_id );
}
}
}
add_action( 'rtmedia_before_delete_media', 'rtmedia_custom_code_before_delete_media', 999 );
if ( ! function_exists( 'rtmedia_custom_code_after_delete_media' ) ) {
/**
* Delete attachment related to rtMedia's media.
*
* @param int $rtmedia_id Media ID related to rtMedia.
* @return void
*/
function rtmedia_custom_code_after_delete_media( $rtmedia_id ) {
if ( empty( $rtmedia_id ) ) {
return;
}
global $rt_media_to_be_deleted;
if ( ! empty( $rt_media_to_be_deleted[ $rtmedia_id ] ) ) {
wp_delete_attachment( $rt_media_to_be_deleted[ $rtmedia_id ] );
}
}
}
add_action( 'rtmedia_after_delete_media', 'rtmedia_custom_code_after_delete_media', 999 );
Note: If you want to bypass the trash and force the deletion, you can replace wp_delete_attachment( $rt_media_to_be_deleted[ $rtmedia_id ] );
with wp_delete_attachment( $rt_media_to_be_deleted[ $rtmedia_id ], true );
in above code.