Skip to content

Instantly share code, notes, and snippets.

@shazdeh
Last active August 29, 2015 13:58
Show Gist options
  • Save shazdeh/10376522 to your computer and use it in GitHub Desktop.
Save shazdeh/10376522 to your computer and use it in GitHub Desktop.
WordPress gallery: enable linking images to different URLs
<?php
class Gallery_Images_Links {
public function __construct() {
add_filter( 'attachment_link', array( $this, 'attachment_link' ), 10, 2 );
add_filter( 'attachment_fields_to_edit', array( $this, 'attachment_fields_to_edit' ), 20, 2 );
add_filter( 'attachment_fields_to_save', array( $this, 'attachment_fields_to_save' ), 10, 2 );
}
function attachment_link( $link, $id ) {
if( $custom_link = get_post_meta( $id, '_link', true ) ) {
$link = $custom_link;
}
return $link;
}
function attachment_fields_to_edit( $fields, $post ) {
$fields['link'] = array(
'label' => __( 'Link' ),
'input' => 'text',
'value' => get_post_meta( $post->ID, '_link', true ),
);
return $fields;
}
function attachment_fields_to_save( $post, $attachment ) {
if( isset( $attachment['link'] ) ) {
update_post_meta( $post['ID'], '_link', $attachment['link'] );
}
return $post;
}
}
new Gallery_Images_Links;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment