Hi all,
I use gallery field to store multiple file type, never used in frontend because it’s an admin working area. If someone put a picture in the field everything is fine because I can directly see the picture, but if it’s another file type I have to clik on the modify button to get the file link and copy it in my browser to read it.
So, I want to add directly in the acf gallery field in the aside attachement data, near the modify and delete links, a download link.
Does Someone know how I can edit this field parts ? And add the link by getting the href of the current attachement ?
I don’t find how to add the html in this specific area.
Thanks,
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' );