Gallery Custom Links

Gallery Custom Links allows you to link images from galleries to a specified URL. Tested with WordPress Gallery, Gutenberg, the Meow Gallery, and others. You can download it for free on the WordPress Repository.

Usage

Two fields are added to your images, in your Media Library: Link URL and Link Target. If at least, the Link URL is set up, this image will link to that URL every time it is used within a gallery. Lightbox will be automatically disabled for those images.

The .custom-link and .no-lightbox classes will be added to the A-Link. That way, you can style the new link the way you would like it. For example, it is recommended to use the following CSS to make sure the image behaves like a link when there is a mouse hover.

​.custom-link {
     cursor: pointer !important;
 }

Compatibility

It currently works with the native WP Gallery, the Gutenberg Gallery, and the Meow Gallery. It should actually work with any gallery plugin using the ‘gallery’ class and Responsive Images (src-set). Let me know if you would like more galleries to be supported, it should be easy.

Filters

There are filters available, through them, you can customize the behavior of the plugin. You can implement those filters by adding code in your functions.php or by using the Code Snippet plugin.

Decide if it is active or not

Through the gallery_custom_links_enabled filter, you can decide if Gallery Custom Links should be enabled or not. Usually, you would like to do this depending on the post/page. For example, the following code will only enable the plugin if the page being loaded has the ID 123.

add_filter( 'gallery_custom_links_enabled', 'my_gallery_custom_links_enabled', 25, 1 );

function my_gallery_custom_links_enabled( $isEnabled ) {
	$id = get_the_ID();
	if ( $id === 123 )
		return true;
	return false;
}

If the code above is not working, make sure the ID is actually retrieved. Depending on the page or post, the get_the_id might be not the right function to call.

Allow it only on specific classes

You will need to use the gallery_custom_links_classes filter. If it is empty, the plugin will go through all the images. If it is an array containing classes (or the IDs), it will look for the images matching it.

add_filter( 'gallery_custom_links_classes', 'my_gallery_custom_links_classes', 25, 1 );

function my_gallery_custom_links_classes( $classes ) {
	return array( '.post', '.gallery' );
}

Debugging & Support

I spent a lot of time helping users with this plugin, but now I am getting a lot of users with very issues very specific to their install and that is getting hard to find the time to support everyone. The plugin can always be improved, but it works perfect on a good install.

First: check your PHP Error Logs. That is the first thing to do when there is an issue on your WordPress install, whatever this issue is.

The URL doesn’t get applied, or the URL is wrong

Most of the action happens in the linkify_element function, in the mgcl_core.php file. If your gallery is well made, then the image should have a class called wp-image-xxxx (where x represents the ID of that image). If that class is not present, then you should probably find a better gallery system, but the standard WordPress Gallery (which is sometimes totally overridden by themes) and the Meow Gallery do that, of course.

If that class is not present, the plugin gets the file, virtually remove the resolution (if there is one) in its filename in order to get the original file. The plugin then queries the database to find to which Media entry this file belongs to.

The content becomes empty or the layout seems changed

Adding links around the images can have an effect, and the CSS rules could work differently in this case. You need to learn a bit of CSS or ask someone around you to have a look at your website and to help.

The layout could be also strongly changed, or the content could be gone. That’s a totally different kind of issue. By default, the plugin is using Output Buffering, it is the only way with WordPress to get access to the whole HTML page in order to analyze it and modify it. If disabled, the plugin will only analyze the content of the page/post. Then, in both case, the plugin is using a HTML parser (HtmlDomParser or DiDom) to analyze your content and to modify it. The problem is if your HTML is not properly formatted (it means there are HTML errors), the parser will not work well, will try to work anyway, and at the end, the HTML generated might be different from what you expect. That is very hard to debug unfortunately, as your whole page would need to be checked, and the theme to be fixed. This issue doesn’t appear if there are no parser involved so usually the developers of the theme don’t really know about the issue and will maybe not really care about it neither.

About Support

I don’t have the time anymore to look into everyone’s install. I worked on this plugin for many weeks very intensively, and decided that from now I will spend 10 hours/week on it at the maximum. Don’t forget, it is currently free, so I am giving those hours away, and I have many other plugins to handle and paying customers.

If you want my support, make sure that:

– there is an error in your PHP Error Logs, concerning my plugin; I will definitely fix it; or
– the class wp-image-xxxx (as explained above) or another attribute containing the ID exists

Without this, the issue will be to difficult for me to guess/debug just by looking at your website.

Don’t hesitate to hire a developer (or better, a friend) to help you finding the real issue. If there is a technical issue on the plugin, and you found it, contact me and I will apply the fix, of course. You can also hire me but as I am really busy with my plugins, my rates are higher than average.