Shapeshifter 3,
If you want to do this without using plugins, you can, just keep in mind you cannot always eliminate plugins as they do add helpful functionality to WordPress and, if updated regularly by the author, make your job easier – you do not have to worry about maintaining or updating code when things change.
That said, here is what I would do if I was trying to achieve what you are trying to do without a plugin:
This code should be added to the functions.php file of the theme or child theme (after the opening <?php
tag – preferably at the end of the file – before the closing ?>
tag if there is one).
function action_fix_attachemnt_redirects(){
global $post;
if( !( is_object($post) || is_array($post) ) )
return;
if(get_post_type($post) == 'attachment'){
$redurl = get_post_meta($post->ID,'_media_redirect_URL',true);
if( $redurl != '' ){
wp_redirect($redurl,301);
exit;
}
}
return;
}
add_action('template_redirect','action_fix_attachemnt_redirects',1);
function action_media_save_metadata($post_id) {
// saves the meta data on media post edit
global $post;
$metaData = array();
$postType = $post->post_type;
$nonce = isset($_REQUEST['media_meta_nonce']) && $_REQUEST['media_meta_nonce'] != '' ? $_REQUEST['media_meta_nonce'] : '';
if(!current_user_can('edit_posts', $post_id) || $postType != 'attachment' || !wp_verify_nonce( $nonce, 'save_media_post_meta_custom' ))
return $post_id;
if( isset( $_POST['_media_redirect_URL'] ) ):
$metaData['_media_redirect_URL'] = $_POST['_media_redirect_URL'] != '' ? esc_url_raw( $_POST['_media_redirect_URL'] ) : '';
if($metaData['_media_redirect_URL'] == 'https://' || $metaData['_media_redirect_URL'] == 'https://')
$metaData['_media_redirect_URL'] = '';
if(!empty($metaData) && is_array($metaData)){
foreach ($metaData as $key => $value) {
delete_post_meta($post_id, $key);
if(!($value == '' || $value == NULL || $value == ','))
add_post_meta($post_id, $key, $value);
}
}
endif;
}
add_action( 'edit_attachment', 'action_media_save_metadata', 10);
function media_meta_box_custom() {
// this is the meta box form for the Redirect URL
global $post;
wp_nonce_field( 'save_media_post_meta_custom','media_meta_nonce' );
$mediaRedirctURL = get_post_meta($post->ID, '_media_redirect_URL', true) !='' ? get_post_meta($post->ID, '_media_redirect_URL', true) : '';
echo '<label for="_media_redirect_URL"><b>Redirect URL:</b></label><br />';
echo '<input type="text" class="widefat" id="_media_redirect_URL" name="_media_redirect_URL" value="'.$mediaRedirctURL.'" />';
}
function action_media_add_metabox(){
//adds meta box to the media edit page
add_meta_box( 'redirect-box-media', 'Media Redirect', 'media_meta_box_custom', 'attachment', 'normal', 'high' );
}
add_action( 'admin_menu', 'action_media_add_metabox');
To use this:
- Add your gallery to a page or post like normal – but select the attachment page as the link for the image.
- Then edit your media items in the media library with the new redirect URL where you want them to go.
- When you go into the media library and click to edit an item, you will see the new meta box at the bottom of the page called ‘Media Redirect’.
- Add the redirect URL there and save the media item.
- Clear any Cache files if you are using a Caching Plugin.
- Now, when you click on a Gallery item on the page/post in the front end of the site, it will redirect you to the new URL you added.
That should be all you need to do to make it happen.
– sorry this code is not more commented, but I created it on the fly and did a quick test to make sure it worked as intended. If anyone needs it commented more in the future, let me know and I will try to add more comments to it for instructional reasons.
Warm regards,
Don