Home › Forums › Add-ons › Gallery Field › Add download link to input in gallery field (backend) › Reply To: Add download link to input in gallery field (backend)
I solved the tropic by myself (and with the Help of John based on another tropics), so if someone had the same issue :
// Add a AJAX action to get attachement URL by ID in JS
add_action( 'wp_ajax_get_media_url', 'get_media_url' );
add_action( 'wp_ajax_nopriv_get_media_url', 'get_media_url' );
function get_media_url() {
if ( isset( $_POST['att_id'] )) {
$attachement_id = $_POST['att_id'];
$attachement_url = wp_get_attachment_url( $attachement_id );
echo $attachement_url;
} else {
echo '#';
}
wp_die();
}
// JS to add the download link
function acf_gallery_download_link() {
if ( !is_admin() ) { return; }?>
<script type='text/javascript'>
( function ( $ ) { $( document ).ready( function () {
// Add download link to attachement on hover
$('.acf-gallery-attachment').each(function(i, item){
var attachement_id = $(this).attr("data-id");
var data = {
'action': 'get_media_url',
'att_id': attachement_id,
};
$.post(ajaxurl, data, function(response){
var attachement_url = response;
$(item).children(".actions").children(".acf-gallery-remove").before('<a class="acf-icon -see dark" href="'+ attachement_url +'" target="_blank" data-id="'+attachement_id+'" title="Toto">Toto</a>');
});
});
// Add download link to gallery side data
acf.addAction('append', function( $el ){
var attachement_id = $el.find(".actions").children(".acf-gallery-edit").attr("data-id");
var data = {
'action': 'get_media_url',
'att_id': attachement_id,
};
$.post(ajaxurl, data, function(response){
var attachement_url = response;
$el.find(".actions").children(".acf-gallery-edit").before('<a href="'+ attachement_url +'" target="_blank" class="acf-gallery-see" data-id="'+attachement_id+'">See</a>');
});
});
});
}( jQuery ) );
</script>
<?php}
add_action( 'admin_footer', 'acf_gallery_download_link' );
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.