• I’m trying to use The WordPress Gallery (without a Plugin) to create a 3 X 3 Page (not a Post) of 9 images each individually redirected to different external links (browser tabbed). When I go to Gallery Settings, there are only three Link To options available:

    1. Attachment Page
    2. Media File, or
    3. none

    The WordPress Visual Editor will not allow me to add links to individual images, and when I tried to use the Text Editor to add an external link to my first image…the whole Gallery disappeared.

    Can a 4th option be added the The WordPress Gallery (Link To Settings) that will provide the ability to redirect each image to an external link? I previously had this working using a Columns Plugin, but was trying to see if I could get it to work with The WordPress Gallery instead (The goal is to reduce my number of Plugins, by using Core Components as often as possible).

    My current setup is: WordPress 4.2-alpha-31464, the Stargazer Theme, and quite a few plugins .

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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:

    1. Add your gallery to a page or post like normal – but select the attachment page as the link for the image.
    2. Then edit your media items in the media library with the new redirect URL where you want them to go.
    3. 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’.
    4. Add the redirect URL there and save the media item.
    5. Clear any Cache files if you are using a Caching Plugin.
    6. 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

    Thread Starter Shapeshifter 3

    (@shapeshifter-3)

    prophecy2040,
    Super Thank You for your response !

    I tried pasting a copy of your above code into the functions.php file of my Stargazer Child Theme, created by this Plugin: https://www.ads-software.com/plugins/child-theme-configurator/ . Here’s what shows in that file BEFORE I pasted your code:

    <?php
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) ) exit;
    
    // BEGIN ENQUEUE PARENT ACTION
    // END ENQUEUE PARENT ACTION

    I pasted your code AFTER the last line of the above code. It Did Not produce any of the desired changes to my Media Files or Settings. Did I paste it in the wrong location?

    That should be an appropriate location to paste the code.

    I just tested a theme out using the plugin and the above code the way you pasted it, and it still works fine.

    One thing to note – you have to edit the redirect URL under the Media Library edit screen or it will not be displayed for you to add the redirect.

    Here are 2 screenshots for you to see how it should look:
    Screen 1
    Screen 2

    Note that screen 2 uses a slight variation to the code to add the metabox to the side under the save box (easier to see there).
    To achieve this, make a change to this line of code:
    add_meta_box( 'redirect-box-media', 'Media Redirect', 'media_meta_box_custom', 'attachment', 'normal', 'high' );
    and change to this:
    add_meta_box( 'redirect-box-media', 'Media Redirect', 'media_meta_box_custom', 'attachment', 'side', 'default' );

    Let me know if you have issues.
    Regards,
    Don

    Thread Starter Shapeshifter 3

    (@shapeshifter-3)

    Don,
    Thank you for your help!

    I tried both of your recommendations using my current theme (Stargazer) and could not get the desired result using either method. So I went to another Plugin Combo (not your fault, the theme’s) and am still using your Quick Page/Post Redirect Plugin installed as the base. What I don’t get is why WordPress decided to hide the Link Manager several versions ago, and why an additional Link To option still can’t be added to the WordPress Gallery Settings for simplicity of use. I guess I’m being stubborn.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘The WordPress Gallery – Image Link – To An External URL ?’ is closed to new replies.