Think this does what I was asking for, if anyone else is interested:
function delete_activity_images( $args ) {
// This function is designed to delete images added by
// BuddyPress Activity Plus (BPAP), so we first check for the constant
// which defines the path to uploaded images.
// If its not there, assume we use the default value
if (!defined('BPFB_BASE_IMAGE_DIR')) {
define('BPFB_BASE_IMAGE_DIR', $wp_upload_dir['basedir'] . '/bpfb/', true);
}
// Get the contents of the activity we are about to delete
global $wpdb;
$content = $wpdb->get_var( $wpdb->prepare( "SELECT content FROM ".$wpdb->prefix."bp_activity WHERE id = %d;", $args['id'] ) );
if ($content != '') {
// Look for the shortcode surrounding image filenames uploaded the BPAP
$matches = array();
preg_match('/\[bpfb_images\](.*?)\[\/bpfb_images\]/s', $content, $matches);
// If there are any images, delete each one and its thumbnail
if ($matches) {
foreach ($matches as $match) {
$images = array();
$images = explode('\n', trim(strip_tags($match)));
if (!empty($images)) {
foreach ($images as $image) {
unlink(BPFB_BASE_IMAGE_DIR.$image);
$image_fn = substr($image, 0, strrpos($image, '.'));
$image_ext = substr($image, strrpos($image, '.') + 1);
unlink(BPFB_BASE_IMAGE_DIR.$image_fn.'-bpfbt.'.$image_ext);
}
}
}
}
}
}
add_action( 'bp_before_activity_delete', 'delete_activity_images');
Any suggestions on improving efficiency would be very welcome.
where do I need to place this piece of code? in the functions.php?
@angelsih: Yes, put it in your theme’s functions.php file.
@davidhunt btw you forgot one } at the end of the code