Media Library Attachments: How to add a Custom URL Field into the HTML?


Lately I’ve been working on building a page dedicated to displaying specific images from the media library. I’ve specified which images to show in the page by adding custom fields directly to the attachment details screen.

For example, the code below adds a category field to the attachment’s details, which I can then call in my pages args to only show images from the library with that specific category.

function customAttachmentCategories() {
     register_taxonomy_for_object_type( 'category', 'attachment' );
}
add_action( 'init' , 'customAttachmentCategories' );

Below is the args markeup I’ve written for my gallery page.

<?php $gallery_args('paged') = get_query_var( 'page' ) ? get_query_var( 'page' ) : 1;

            $gallery_args = array (
                'post_type' => 'attachment',
                'post_mime_type' => 'image',
                'post_status' => 'inherit',
                'posts_per_page' => -1,
                'category_name' => 'flowers',
                'order' => 'ASC',
                'orderby' => 'menu_order'
            );
            $gallery = new wp_query( $gallery_args ); $wp_query = NULL; $wp_query = $gallery; if( $gallery->have_posts() ) : while( $gallery->have_posts() ): $gallery->the_post();
            $attachments = wp_prepare_attachment_for_js( $attachment->ID ); $category = get_the_category( $attachment->ID ); ?>

Now, I have done the same with adding a Custom URL Field. Here is the function I’m using to add a Custom URL Field into the attachment’s details.

function cutsomAttachmentUrl( $attachment_url_field, $post ) {
     $attachment_url_field('custom-url') = array(
          'label' => 'Custom URL:',
          'input' => 'text',
          'value' => get_post_meta( $post->ID, $attachment->ID, 'custom-url', true ),
     );
     return $attachment_url_field;
}
add_filter( 'attachment_fields_to_edit', 'cutsomAttachmentUrl', 10, 2 );

/**
 * Save values of URL in media uploader
 *
 * @param $post array, the post data for database
 * @param $attachment array, attachment fields from $_POST form
 * @return $post array, modified post data
*/

function be_attachment_field_credit_save( $post, $attachment ) {
     if( isset( $mediaattachmenturl('custom-url') ) )
          update_post_meta( $post('ID'), 'custom-url', $mediaattachmenturl('custom-url') );

     return $post;
}
add_filter( 'attachment_fields_to_save', 'be_attachment_field_credit_save', 10, 2 );

The above function works great and adds the Custom URL Field to the attachment details, however my current issue is trying to call said link into my HTML, like how you would with a posts permalink.

/* Example of what I'm looking for: */
<a href="<?php the_permalink() ?>"</a>

/* What I have tried, along with similar variations: */
<a href="<?php echo get_post_meta( $post->ID, $attachment->ID, '_custom-url', true );?></a>

I cannot seem to figure out how to call the custom url link. Any thoughts or help would be greatly appreciated!