I’ve rewritten a little bit to also delete the images from the media library
functions.php
function azrClearButton( $field ) {
global $post;
echo '<p style="text-align:right"><a href="#" class="acf-button button button-primary acf-gallery-clear" data-postid="'.$post->ID.'">Delete all Images</a></p>';
}
add_action('acf/render_field/type=gallery', 'azrClearButton', 10, 1 );
function azrDeleteImagesFromGallery() {
?>
<script type="text/javascript">
(function($) {
var azr_TemplateDir = "<?php bloginfo('template_directory') ?>"
$('.acf-gallery-clear').click(function() {
console.log('test');
var postid = $(this).data('postid');
$.ajax({
type: 'post',
url: azr_TemplateDir+'/ajax/delete-gallery.ajax.php',
data: {'postid': postid},
dataType: 'json',
success: function (data) {
location.reload();
}
});
});
})(jQuery);
</script>
<?php
}
add_action('acf/input/admin_footer', 'azrDeleteImagesFromGallery');
AJAX FILE
require('../../../../wp-load.php');
global $post;
$postid = $_POST['postid'];
$args = array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_status' => 'any',
'post_parent' => $postid
);
$attachments = get_posts($args);
foreach($attachments as $attachment) :
wp_delete_attachment($attachment->ID, true );
endforeach;
Hi thanks for your suggestion but I’ve already tried this one and it’s not working either.
I think the problem could be that I am using the_field() in a foreach loop and that ACF has a little problem with that. In another place on my site I created a custom field and it worked.
I wrote ACF support about the problem and they are on it. So if a get a solution I will post ist here.
Alex