Last active
December 1, 2025 00:14
-
-
Save g-maclean/311a9a531ac470b078144b25cd615d8e to your computer and use it in GitHub Desktop.
Property Hive - add a bulk option for removing property media
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //adds a new option to the wp bulk options dropdown | |
| // Add a custom bulk action to the dropdown | |
| function add_bulk_delete_media_action($bulk_actions) { | |
| $bulk_actions['delete_attached_media'] = __('Delete Attached Media', 'text-domain'); | |
| return $bulk_actions; | |
| } | |
| add_filter('bulk_actions-edit-property', 'add_bulk_delete_media_action'); | |
| // Handle the custom bulk action | |
| function handle_bulk_delete_media_action($redirect_to, $doaction, $post_ids) { | |
| if ($doaction !== 'delete_attached_media') { | |
| return $redirect_to; | |
| } | |
| $deleted_media_count = 0; | |
| foreach ($post_ids as $post_id) { | |
| // Get all media attached to the post | |
| ph_delete_media( $post_id, '_epcs' ); | |
| ph_delete_media( $post_id, '_brochures' ); | |
| ph_delete_media( $post_id, '_floorplans' ); | |
| ph_delete_media( $post_id, '_photos', true ); //***change to true to keep the first image, false to delete all images | |
| $deleted_media_count++; | |
| } | |
| // Redirect back with a notice | |
| $redirect_to = add_query_arg( | |
| 'deleted_media_count', | |
| $deleted_media_count, | |
| $redirect_to | |
| ); | |
| return $redirect_to; | |
| } | |
| add_filter('handle_bulk_actions-edit-property', 'handle_bulk_delete_media_action', 10, 3); | |
| // Display an admin notice for deleted media | |
| function display_bulk_delete_media_notice() { | |
| if (!empty($_REQUEST['deleted_media_count'])) { | |
| $deleted_media_count = intval($_REQUEST['deleted_media_count']); | |
| printf( | |
| '<div id="message" class="updated fade"><p>' . | |
| esc_html(_n('%s properties had media deleted.', '%s properties had media deleted.', $deleted_media_count, 'text-domain')) . | |
| '</p></div>', | |
| $deleted_media_count | |
| ); | |
| } | |
| } | |
| add_action('admin_notices', 'display_bulk_delete_media_notice'); | |
| function ph_delete_media( $post_id, $meta_key, $except_first = false ) | |
| { | |
| $media_ids = get_post_meta( $post_id, $meta_key, TRUE ); | |
| if ( !empty( $media_ids ) ) | |
| { | |
| $i = 0; | |
| foreach ( $media_ids as $media_id ) | |
| { | |
| if ( !$except_first || ( $except_first && $i > 0 ) ) | |
| { | |
| if ( wp_delete_attachment( $media_id, TRUE ) !== FALSE ) | |
| { | |
| // Deleted succesfully. Now remove from array | |
| if( ($key = array_search($media_id, $media_ids)) !== false) | |
| { | |
| unset($media_ids[$key]); | |
| } | |
| } | |
| else | |
| { | |
| WP_Error( 'media_delete_error', 'Error deleting media with ID ' . $media_id ); | |
| } | |
| } | |
| ++$i; | |
| } | |
| } | |
| update_post_meta( $post_id, $meta_key, $media_ids ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment