Skip to content

Instantly share code, notes, and snippets.

@shazdeh
Last active December 30, 2015 03:09
Show Gist options
  • Save shazdeh/7767860 to your computer and use it in GitHub Desktop.
Save shazdeh/7767860 to your computer and use it in GitHub Desktop.
Attachment page redirect with a custom field. Useful when you want to link your images in a [gallery] shortcode.
<?php
function media_custom_field_support() {
add_post_type_support( 'attachment', 'custom-fields' );
}
add_action( 'init', 'media_custom_field_support' );
function do_attachment_redirect() {
if( is_attachment() && $redirect = get_post_meta( $GLOBALS['post']->ID, 'redirect', true ) ) {
wp_redirect( $redirect );
exit;
}
}
add_action( 'template_redirect', 'do_attachment_redirect' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment