Last active
December 30, 2015 03:09
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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