Home › Forums › Add-ons › Gallery Field › Delete All Images In Gallery At Once
Is there anyway of either being able to press a button that deletes all of the images in the gallery or being able to select all of the images and then deleting them?
There currently isn’t anyway to do this in ACF. You could try submitting a feature request here https://support.advancedcustomfields.com/new-ticket.
You might also use the acf/render_field https://www.advancedcustomfields.com/resources/acf-render_field/ hook to add additional controls to the gallery field and then figure out how to add custom javascript to the field to make it work https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/. Unfortunately, that’s all I can offer because I don’t know exactly how to do this.
Thanks. I managed to resolve this myself thanks to your advice.
For anyone who is interested, I just did the following:
function clear_button( $field ) {
echo '<p><a href="#" class="acf-button button button-primary acf-gallery-clear">Clear Images</a></p>';
}
add_action( 'acf/render_field/type=gallery', 'clear_button', 10, 1 );
function my_acf_input_admin_footer() {
?>
<script type="text/javascript">
(function($) {
$('.acf-gallery-clear').click(function() {
$('.acf-gallery-remove').click();
});
})(jQuery);
</script>
<?php
}
add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
This just clears the images from the gallery rather than deletes them off the website which I may do in the future but for now I just needed a way of starting again with the gallery.
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;
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.