WP Gallery Custom Links - Version 1.1.0

Version Description

  • Added support for replacing links to all sizes of an uploaded image instead of the full version only
  • Replaced lightbox removal with a more advanced javascript method
Download this release

Release Info

Developer fourlightsweb
Plugin Icon wp plugin WP Gallery Custom Links
Version 1.1.0
Comparing to
See all releases

Code changes from version 1.0.5 to 1.1.0

Files changed (2) hide show
  1. readme.txt +12 -4
  2. wp-gallery-custom-links.php +61 -38
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://www.fourlightsweb.com/wordpress-plugins/wp-gallery-custom-li
4
  Tags: gallery links, gallery link, gallery
5
  Requires at least: 3.3.2
6
  Tested up to: 3.3.2
7
- Stable tag: 1.0.5
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -17,9 +17,9 @@ is included in a gallery, the "Gallery Link URL" value will be used as the link
17
  the image instead of the raw file or the attachment post. It's designed to work
18
  even if customizations have been made via the post_gallery filter; instead of
19
  replacing the entire post_gallery function, it calls the normal function
20
- and simply replaces the link hrefs in the generated output. Any "rel" attribute
21
- or *box classes are also stripped to "break" any lightbox functionality and
22
- allow the link to function as a regular link.
23
 
24
  == Installation ==
25
 
@@ -38,6 +38,10 @@ As soon as someone asks me something. :)
38
 
39
  == Changelog ==
40
 
 
 
 
 
41
  = 1.0.5 =
42
  * Moving the $post_id code above first_call to avoid messing that up if a return does occur due to a missing post_id
43
 
@@ -58,6 +62,10 @@ As soon as someone asks me something. :)
58
 
59
  == Upgrade Notice ==
60
 
 
 
 
 
61
  = 1.0.5 =
62
  * Moving the $post_id code above first_call to avoid messing that up if a return does occur due to a missing post_id
63
 
4
  Tags: gallery links, gallery link, gallery
5
  Requires at least: 3.3.2
6
  Tested up to: 3.3.2
7
+ Stable tag: 1.1.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
17
  the image instead of the raw file or the attachment post. It's designed to work
18
  even if customizations have been made via the post_gallery filter; instead of
19
  replacing the entire post_gallery function, it calls the normal function
20
+ and simply replaces the link hrefs in the generated output. Javascript is
21
+ also in place to attempt to remove any lightbox functionality on the custom link
22
+ and allow it to function as a regular link.
23
 
24
  == Installation ==
25
 
38
 
39
  == Changelog ==
40
 
41
+ = 1.1.0 =
42
+ * Added support for replacing links to all sizes of an uploaded image instead of the full version only
43
+ * Replaced lightbox removal with a more advanced javascript method
44
+
45
  = 1.0.5 =
46
  * Moving the $post_id code above first_call to avoid messing that up if a return does occur due to a missing post_id
47
 
62
 
63
  == Upgrade Notice ==
64
 
65
+ = 1.1.0 =
66
+ * Added support for replacing links to all sizes of an uploaded image instead of the full version only
67
+ * Replaced lightbox removal with a more advanced javascript method
68
+
69
  = 1.0.5 =
70
  * Moving the $post_id code above first_call to avoid messing that up if a return does occur due to a missing post_id
71
 
wp-gallery-custom-links.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: WP Gallery Custom Links
4
  Plugin URI: http://www.fourlightsweb.com/wordpress-plugins/wp-gallery-custom-links/
5
  Description: Specifiy custom links for WordPress gallery images (instead of attachment or file only).
6
- Version: 1.0.5
7
  Author: Four Lights Web Development
8
  Author URI: http://www.fourlightsweb.com
9
  License: GPL2
@@ -114,11 +114,11 @@ class WPGalleryCustomLinks {
114
 
115
  // Get the attachments for this post
116
  $attachments = get_children( array( 'post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image' ) );
117
- foreach ( $attachments as $id => $attachment ) {
118
  $link = '';
119
 
120
  // See if we have a custom url for this attachment image
121
- $attachment_meta = get_post_meta( $id, '_gallery_link_url', true );
122
  if( $attachment_meta ) {
123
  $link = $attachment_meta;
124
  }
@@ -126,46 +126,69 @@ class WPGalleryCustomLinks {
126
  if( $link != '' ) {
127
  // If we have a non-blank custom url, swap out the href on the image
128
  // in the generated gallery code with the custom url
129
- if( isset( $attr['link'] ) && $attr['link'] == 'file' ) {
130
- // Get file href
131
- list( $needle ) = wp_get_attachment_image_src( $id, '' );
132
- } else {
133
- // Get the attachment href
134
- $needle = get_attachment_link( $id );
135
- } // End if gallery setting is to file or attachment
136
 
137
- // Build the regex for matching/replacing
138
- $needle = preg_quote( $needle );
139
- $needle = str_replace( '/', '\/', $needle );
140
- $needle = '/href\s*=\s*["\']' . $needle . '["\']/';
141
- if( preg_match( $needle, $output ) > 0 ) {
142
- // If we found the href to swap out, perform
143
- // the href replacement
144
- $output = preg_replace( $needle, 'href="' . $link . '"', $output );
145
-
146
- // ...also remove any rel attribute and *box
147
- // classes so (most) lightboxes won't kick in:
148
-
149
- // Clean up the href for regex-ing
150
- $link = preg_quote( $link );
151
- $link = str_replace( '/', '\/', $link );
152
-
153
- // href comes before rel
154
- $output = preg_replace( '/(<a[^>]*href="' . $link . '"[^>]*)rel\s*=\s*["\'][^"\']*["\']([^>]*>)/', '$1$2', $output );
155
-
156
- // href comes after rel
157
- $output = preg_replace( '/(<a[^>]*)rel\s*=\s*["\'][^"\']*["\']([^>]*href="' . $link . '"[^>]*>)/', '$1$2', $output );
158
-
159
- // href comes before class
160
- $output = preg_replace( '/(<a[^>]*href="' . $link . '"[^>]*class\s*=\s*["\'][^"\']*)box([^"\']*["\'][^>]*>)/', '$1$2', $output );
161
-
162
- // href comes after class
163
- $output = preg_replace( '/(<a[^>]*class\s*=\s*["\'][^"\']*)box([^"\']*["\'][^>]*href="' . $link . '"[^>]*>)/', '$1$2', $output );
164
- } // End if we found the attachment to replace in the output
165
  } // End if we have a custom url to swap in
166
  } // End foreach post attachment
 
 
 
 
 
 
 
 
 
 
 
167
 
168
  return $output;
169
  } // End function apply_filter_post_gallery()
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  } // End class WPGalleryCustomLinks
3
  Plugin Name: WP Gallery Custom Links
4
  Plugin URI: http://www.fourlightsweb.com/wordpress-plugins/wp-gallery-custom-links/
5
  Description: Specifiy custom links for WordPress gallery images (instead of attachment or file only).
6
+ Version: 1.1.0
7
  Author: Four Lights Web Development
8
  Author URI: http://www.fourlightsweb.com
9
  License: GPL2
114
 
115
  // Get the attachments for this post
116
  $attachments = get_children( array( 'post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image' ) );
117
+ foreach ( $attachments as $attachment_id => $attachment ) {
118
  $link = '';
119
 
120
  // See if we have a custom url for this attachment image
121
+ $attachment_meta = get_post_meta( $attachment_id, '_gallery_link_url', true );
122
  if( $attachment_meta ) {
123
  $link = $attachment_meta;
124
  }
126
  if( $link != '' ) {
127
  // If we have a non-blank custom url, swap out the href on the image
128
  // in the generated gallery code with the custom url
 
 
 
 
 
 
 
129
 
130
+ // Replace the attachment href
131
+ $needle = get_attachment_link( $attachment_id );
132
+ $output = self::replace_link( $needle, $link, $output );
133
+
134
+ // Replace the file href
135
+ list( $needle ) = wp_get_attachment_image_src( $attachment_id, '' );
136
+ $output = self::replace_link( $needle, $link, $output );
137
+
138
+ // Replace all possible file sizes - some themes etc.
139
+ // may use sizes other than the full version
140
+ $attachment_metadata = wp_get_attachment_metadata( $attachment_id );
141
+ $attachment_sizes = $attachment_metadata['sizes'];
142
+ foreach( $attachment_sizes as $attachment_size => $attachment_info ) {
143
+ list( $needle ) = wp_get_attachment_image_src( $attachment_id, $attachment_size );
144
+ $output = self::replace_link( $needle, $link, $output );
145
+ }
 
 
 
 
 
 
 
 
 
 
 
 
146
  } // End if we have a custom url to swap in
147
  } // End foreach post attachment
148
+
149
+ // Javascript to override lightboxes
150
+ $output .= "<script type=\"text/javascript\">\n";
151
+ $output .= "/* <![CDATA[ */\n";
152
+ $output .= "jQuery(document).ready(function () {\n";
153
+ $output .= " jQuery('a.no-lightbox').unbind();\n";
154
+ $output .= " jQuery('a.no-lightbox').off();\n";
155
+ $output .= " jQuery('a.no-lightbox').click(function(){window.location=this.href; return false;});\n";
156
+ $output .= "});";
157
+ $output .= "/* ]]> */\n";
158
+ $output .= "</script>";
159
 
160
  return $output;
161
  } // End function apply_filter_post_gallery()
162
 
163
+ private static function replace_link( $default_link, $custom_link, $output ) {
164
+ // Build the regex for matching/replacing
165
+ $needle = preg_quote( $default_link );
166
+ $needle = str_replace( '/', '\/', $needle );
167
+ $needle = '/href\s*=\s*["\']' . $needle . '["\']/';
168
+ if( preg_match( $needle, $output ) > 0 ) {
169
+ // If we found the href to swap out, perform the href replacement,
170
+ // and add some javascript to prevent lightboxes from kicking in
171
+ $output = preg_replace( $needle, 'href="' . $custom_link . '"', $output );
172
+
173
+ // Clean up the new href for regex-ing
174
+ $custom_link = preg_quote( $custom_link );
175
+ $custom_link = str_replace( '/', '\/', $custom_link );
176
+
177
+ // Add a class to the link so we can manipulate it with
178
+ // javascript later
179
+ if( preg_match( '/<a[^>]*href="' . $custom_link . '"[^>]*class\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) {
180
+ // href comes before class
181
+ $output = preg_replace( '/(<a[^>]*href="' . $custom_link . '"[^>]*class\s*=\s*["\'][^"\']*)(["\'][^>]*>)/', '$1no-lightbox$2', $output );
182
+ } elseif( preg_match( '/<a[^>]*class\s*=\s*["\'][^"\']*["\'][^>]*href="' . $custom_link . '"[^>]*>/', $output ) > 0 ) {
183
+ // href comes after class
184
+ $output = preg_replace( '/(<a[^>]*class\s*=\s*["\'][^"\']*)(["\'][^>]*href="' . $custom_link . '"[^>]*>)/', '$1no-lightbox$2', $output );
185
+ } else {
186
+ // No previous class
187
+ $output = preg_replace( '/(<a[^>]*href="' . $custom_link . '"[^>]*)(>)/', '$1 class="no-lightbox"$2', $output );
188
+ } // End if we have a class on the a tag or not
189
+ } // End if we found the attachment to replace in the output
190
+
191
+ return $output;
192
+ } // End function replace_link()
193
+
194
  } // End class WPGalleryCustomLinks