ACF Remove Image From WP Media Library (with php code)

Viewed 257

I am using Advanced Custom Fields Pro and ACF Frontend. I am trying to remove the image from wordpress media library when a user deletes the image from the image field.

This is what I did:

//ACF Remove Image From WP Media Library
function delete_image( $value, $post_id, $field  ) {
    $old_value = get_field( $field['local-avatar'], $post_id, false /* Don't format the value, we want the raw ID */ );

    if ( $old_value && ( int ) $old_value !== ( int ) $value )
        wp_delete_attachment( $old_value, true );

    return $value;
}

add_filter( 'acf/update_value/type=image', 'delete_image', 10, 3 );

I got to this by googling: https://wordpress.stackexchange.com/questions/199887/wordpress-acf-delete-image-from-media-library

Furthermore, to find a solution, I also searched and documented myself on: https://www.advancedcustomfields.com/resources/

Unfortunately it's not working for me, I don't understand what I'm doing. I am new to Wordpress and php, does anyone have any idea how to solve this problem ?

0 Answers
Related