0

When you click "Add Media" and from there select an image from that media library screen on the right you get a breakdown (the attachment details) related to that particular image.

Right below the image there are some image details and then the links > Edit Image > Delete Permanently.

How can I add a custom link to appear right there (preferably) above the "Edit Image" link with a function?

I searched around and the closest I could find it how to add custom fields to the media upload screen nearest to the bottom options in the right when an image is selected. I couldn't find and I'm not sure how to take that snippet and take the field area to be just a link instead.

I used in my description above "image" just for visual purposes. I want the link to appear for all files from the media upload screen, not just images if someone is thinking that.

Thanks a lot!

1 Answer 1

0

A hacky way to do it is to do a string replace on the tmpl-attachment-details template:

// See wp_print_media_templates() in "wp-includes/media-template.php"
function wpse157297_print_media_templates() {
    $my_link = '<a class="my_link-attachment" href="my_link.php?url={{ data.url }}" target="_blank">' . __( 'My Link' ) . '</a>';
    $nonimage = '<# if ( "image" !== data.type && ! data.uploading ) { #>' . $my_link . '<# } #>';
    $image = $my_link;
?>
<script>
(function ($) {
    var tmpl_attachment_details = $('#tmpl-attachment-details'), html = tmpl_attachment_details.html();
    tmpl_attachment_details.html(
        html.replace( /<div class="uploaded">[^<]*<\/div>/, '$&<?php echo $nonimage; ?>' ).replace( '<a class="edit-attachment"', '<?php echo $image; ?>$&' )
    );
})(jQuery);
</script>
<?php
}
add_action( 'print_media_templates', 'wpse157297_print_media_templates', 11 );

Apart from {{ data.url }}, there's {{ data.id }}, {{ data.filename }} etc - see wp_prepare_attachment_for_js() in "wp-includes/media.php"

1
  • Very awesome @bonger. Thanks! Works perfectly. I had to make one adjust which was to take out the "?url={{ data.url }}" for my url. So this link just opens a backend page the site. I really appreciate it. Commented Aug 12, 2014 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.