WP Gallery Custom Links - Version 1.2.0

Version Description

  • By popular demand, added an option to open gallery image links in a new window.
Download this release

Release Info

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

Code changes from version 1.1.2 to 1.2.0

Files changed (3) hide show
  1. readme.txt +19 -7
  2. screenshot-1.png +0 -0
  3. wp-gallery-custom-links.php +103 -35
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.4.1
7
- Stable tag: 1.1.2
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
@@ -12,14 +12,20 @@ Specifiy custom links for WordPress gallery images (instead of attachment or fil
12
 
13
  == Description ==
14
 
 
 
 
15
  This plugin adds a "Gallery Link URL" field when editing post images. If the image
16
  is included in a gallery, the "Gallery Link URL" value will be used as the link on
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
 
@@ -49,6 +55,9 @@ that function outside of the WordPress [gallery] shortcode.
49
 
50
  == Changelog ==
51
 
 
 
 
52
  = 1.1.2 =
53
  * Added a check to prevent javascript from showing up in feeds.
54
 
@@ -80,6 +89,9 @@ that function outside of the WordPress [gallery] shortcode.
80
 
81
  == Upgrade Notice ==
82
 
 
 
 
83
  = 1.1.2 =
84
  * Added a check to prevent javascript from showing up in feeds.
85
 
4
  Tags: gallery links, gallery link, gallery
5
  Requires at least: 3.3.2
6
  Tested up to: 3.4.1
7
+ Stable tag: 1.2.0
8
  License: GPLv2 or later
9
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
 
12
 
13
  == Description ==
14
 
15
+ If you've ever had a WordPress gallery of staff, product, or other images and needed
16
+ to link them to other pages but couldn't, this plugin is for you!
17
+
18
  This plugin adds a "Gallery Link URL" field when editing post images. If the image
19
  is included in a gallery, the "Gallery Link URL" value will be used as the link on
20
+ the image instead of the raw file or the attachment post. There is also an option
21
+ to open gallery images in a new window.
22
+
23
+ It's designed to work even if customizations have been made via the
24
+ post_gallery filter; instead of replacing the entire post_gallery function, it
25
+ calls the normal function and simply replaces the link hrefs in the generated
26
+ output. Javascript is also in place to attempt to remove any lightbox
27
+ functionality on the custom link and allow it to function as a regular link.
28
+
29
 
30
  == Installation ==
31
 
55
 
56
  == Changelog ==
57
 
58
+ = 1.2.0 =
59
+ * By popular demand, added an option to open gallery image links in a new window.
60
+
61
  = 1.1.2 =
62
  * Added a check to prevent javascript from showing up in feeds.
63
 
89
 
90
  == Upgrade Notice ==
91
 
92
+ = 1.2.0 =
93
+ * By popular demand, added an option to open gallery image links in a new window.
94
+
95
  = 1.1.2 =
96
  * Added a check to prevent javascript from showing up in feeds.
97
 
screenshot-1.png CHANGED
Binary file
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.1.2
7
  Author: Four Lights Web Development
8
  Author URI: http://www.fourlightsweb.com
9
  License: GPL2
@@ -57,7 +57,17 @@ class WPGalleryCustomLinks {
57
  'label' => __( 'Gallery Link URL' ),
58
  'input' => 'text',
59
  'value' => get_post_meta( $post->ID, '_gallery_link_url', true ),
60
- 'helps' => 'Will replace "Image File" or "Attachment Page" link for this thumbnail in the gallery.'
 
 
 
 
 
 
 
 
 
 
61
  );
62
  return $form_fields;
63
  } // End function apply_filter_attachment_fields_to_edit()
@@ -66,6 +76,9 @@ class WPGalleryCustomLinks {
66
  if( isset( $attachment['gallery_link_url'] ) ) {
67
  update_post_meta( $post['ID'], '_gallery_link_url', $attachment['gallery_link_url'] );
68
  }
 
 
 
69
  return $post;
70
  } // End function apply_filter_attachment_fields_to_save()
71
 
@@ -116,24 +129,27 @@ class WPGalleryCustomLinks {
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
  }
125
-
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
@@ -143,11 +159,12 @@ class WPGalleryCustomLinks {
143
  if( is_array( $attachment_sizes ) && count( $attachment_sizes ) > 0 ) {
144
  foreach( $attachment_sizes as $attachment_size => $attachment_info ) {
145
  list( $needle ) = wp_get_attachment_image_src( $attachment_id, $attachment_size );
146
- $output = self::replace_link( $needle, $link, $output );
147
  } // End of foreach attachment size
148
  } // End if we have attachment sizes
149
  } // End if we have attachment metadata (specifically sizes)
150
- } // End if we have a custom url to swap in
 
151
  } // End foreach post attachment
152
 
153
  // Javascript to override lightboxes
@@ -159,8 +176,18 @@ class WPGalleryCustomLinks {
159
  $output .= "jQuery(document).ready(function () {\n";
160
  $output .= " jQuery('a.no-lightbox').unbind();\n";
161
  $output .= " jQuery('a.no-lightbox').off();\n";
162
- $output .= " jQuery('a.no-lightbox').click(function(){window.location=this.href; return false;});\n";
163
- $output .= "});";
 
 
 
 
 
 
 
 
 
 
164
  $output .= "/* ]]> */\n";
165
  $output .= "</script>";
166
  } // End if it's not a feed
@@ -168,35 +195,76 @@ class WPGalleryCustomLinks {
168
  return $output;
169
  } // End function apply_filter_post_gallery()
170
 
171
- private static function replace_link( $default_link, $custom_link, $output ) {
172
  // Build the regex for matching/replacing
173
  $needle = preg_quote( $default_link );
174
  $needle = str_replace( '/', '\/', $needle );
175
  $needle = '/href\s*=\s*["\']' . $needle . '["\']/';
176
- if( preg_match( $needle, $output ) > 0 ) {
177
- // If we found the href to swap out, perform the href replacement,
178
- // and add some javascript to prevent lightboxes from kicking in
179
- $output = preg_replace( $needle, 'href="' . $custom_link . '"', $output );
180
-
181
- // Clean up the new href for regex-ing
182
- $custom_link = preg_quote( $custom_link );
183
- $custom_link = str_replace( '/', '\/', $custom_link );
 
 
184
 
185
- // Add a class to the link so we can manipulate it with
186
- // javascript later
187
- if( preg_match( '/<a[^>]*href="' . $custom_link . '"[^>]*class\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) {
188
- // href comes before class
189
- $output = preg_replace( '/(<a[^>]*href="' . $custom_link . '"[^>]*class\s*=\s*["\'][^"\']*)(["\'][^>]*>)/', '$1no-lightbox$2', $output );
190
- } elseif( preg_match( '/<a[^>]*class\s*=\s*["\'][^"\']*["\'][^>]*href="' . $custom_link . '"[^>]*>/', $output ) > 0 ) {
191
- // href comes after class
192
- $output = preg_replace( '/(<a[^>]*class\s*=\s*["\'][^"\']*)(["\'][^>]*href="' . $custom_link . '"[^>]*>)/', '$1no-lightbox$2', $output );
193
- } else {
194
- // No previous class
195
- $output = preg_replace( '/(<a[^>]*href="' . $custom_link . '"[^>]*)(>)/', '$1 class="no-lightbox"$2', $output );
196
- } // End if we have a class on the a tag or not
197
  } // End if we found the attachment to replace in the output
198
 
199
  return $output;
200
  } // End function replace_link()
201
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
202
  } // 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.2.0
7
  Author: Four Lights Web Development
8
  Author URI: http://www.fourlightsweb.com
9
  License: GPL2
57
  'label' => __( 'Gallery Link URL' ),
58
  'input' => 'text',
59
  'value' => get_post_meta( $post->ID, '_gallery_link_url', true ),
60
+ 'helps' => 'Will replace "Image File" or "Attachment Page" link for this image in the gallery.'
61
+ );
62
+ $target_value = get_post_meta( $post->ID, '_gallery_link_target', true );
63
+ $form_fields['gallery_link_target'] = array(
64
+ 'label' => __( 'Gallery Link Target' ),
65
+ 'input' => 'html',
66
+ 'html' => '
67
+ <select name="attachments['.$post->ID.'][gallery_link_target]" id="attachments['.$post->ID.'][gallery_link_target]">
68
+ <option value="">Same Window</option>
69
+ <option value="_blank"'.($target_value == '_blank' ? ' selected="selected"' : '').'>New Window</option>
70
+ </select>'
71
  );
72
  return $form_fields;
73
  } // End function apply_filter_attachment_fields_to_edit()
76
  if( isset( $attachment['gallery_link_url'] ) ) {
77
  update_post_meta( $post['ID'], '_gallery_link_url', $attachment['gallery_link_url'] );
78
  }
79
+ if( isset( $attachment['gallery_link_target'] ) ) {
80
+ update_post_meta( $post['ID'], '_gallery_link_target', $attachment['gallery_link_target'] );
81
+ }
82
  return $post;
83
  } // End function apply_filter_attachment_fields_to_save()
84
 
129
  $attachments = get_children( array( 'post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image' ) );
130
  foreach ( $attachments as $attachment_id => $attachment ) {
131
  $link = '';
132
+ $target = '';
133
 
134
  // See if we have a custom url for this attachment image
135
  $attachment_meta = get_post_meta( $attachment_id, '_gallery_link_url', true );
136
  if( $attachment_meta ) {
137
  $link = $attachment_meta;
138
  }
139
+ // See if we have a target for this attachment image
140
+ $attachment_meta = get_post_meta( $attachment_id, '_gallery_link_target', true );
141
+ if( $attachment_meta ) {
142
+ $target = $attachment_meta;
143
+ }
144
+
145
+ if( $link != '' || $target != '' ) {
146
  // Replace the attachment href
147
  $needle = get_attachment_link( $attachment_id );
148
+ $output = self::replace_link( $needle, $link, $target, $output );
149
+
150
  // Replace the file href
151
  list( $needle ) = wp_get_attachment_image_src( $attachment_id, '' );
152
+ $output = self::replace_link( $needle, $link, $target, $output );
153
 
154
  // Replace all possible file sizes - some themes etc.
155
  // may use sizes other than the full version
159
  if( is_array( $attachment_sizes ) && count( $attachment_sizes ) > 0 ) {
160
  foreach( $attachment_sizes as $attachment_size => $attachment_info ) {
161
  list( $needle ) = wp_get_attachment_image_src( $attachment_id, $attachment_size );
162
+ $output = self::replace_link( $needle, $link, $target, $output );
163
  } // End of foreach attachment size
164
  } // End if we have attachment sizes
165
  } // End if we have attachment metadata (specifically sizes)
166
+ } // End if we have a link to swap in or a target to add
167
+
168
  } // End foreach post attachment
169
 
170
  // Javascript to override lightboxes
176
  $output .= "jQuery(document).ready(function () {\n";
177
  $output .= " jQuery('a.no-lightbox').unbind();\n";
178
  $output .= " jQuery('a.no-lightbox').off();\n";
179
+ $output .= " jQuery('a.no-lightbox').click(wp_gallery_custom_links_click);\n";
180
+ $output .= " jQuery('a.set-target').unbind();\n";
181
+ $output .= " jQuery('a.set-target').off();\n";
182
+ $output .= " jQuery('a.set-target').click(wp_gallery_custom_links_click);\n";
183
+ $output .= "});\n";
184
+ $output .= "function wp_gallery_custom_links_click() {\n";
185
+ $output .= " if(!this.target || this.target == '')\n";
186
+ $output .= " window.location = this.href;\n";
187
+ $output .= " else\n";
188
+ $output .= " window.open(this.href,this.target);\n";
189
+ $output .= " return false;\n";
190
+ $output .= "}\n";
191
  $output .= "/* ]]> */\n";
192
  $output .= "</script>";
193
  } // End if it's not a feed
195
  return $output;
196
  } // End function apply_filter_post_gallery()
197
 
198
+ private static function replace_link( $default_link, $custom_link, $target, $output ) {
199
  // Build the regex for matching/replacing
200
  $needle = preg_quote( $default_link );
201
  $needle = str_replace( '/', '\/', $needle );
202
  $needle = '/href\s*=\s*["\']' . $needle . '["\']/';
203
+ if( preg_match( $needle, $output ) > 0 ) {
204
+ // Custom Target
205
+ if( $target != '' ) {
206
+ // Replace the link target
207
+ $output = self::add_target( $default_link, $target, $output );
208
+
209
+ // Add a class to the link so we can manipulate it with
210
+ // javascript later
211
+ $output = self::add_class( $default_link, 'set-target', $output );
212
+ }
213
 
214
+ // Custom Link
215
+ if( $custom_link != '' ) {
216
+ // If we found the href to swap out, perform the href replacement,
217
+ // and add some javascript to prevent lightboxes from kicking in
218
+ $output = preg_replace( $needle, 'href="' . $custom_link . '"', $output );
219
+
220
+ // Add a class to the link so we can manipulate it with
221
+ // javascript later
222
+ $output = self::add_class( $custom_link, 'no-lightbox', $output );
223
+ } // End if we have a custom link to swap in
 
 
224
  } // End if we found the attachment to replace in the output
225
 
226
  return $output;
227
  } // End function replace_link()
228
 
229
+ private static function add_class( $needle, $class, $output ) {
230
+ // Clean up our needle for regexing
231
+ $needle = preg_quote( $needle );
232
+ $needle = str_replace( '/', '\/', $needle );
233
+
234
+ // Add a class to the link so we can manipulate it with
235
+ // javascript later
236
+ if( preg_match( '/<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*class\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) {
237
+ // href comes before class
238
+ $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*class\s*=\s*["\'][^"\']*)(["\'][^>]*>)/', '$1 '.$class.'$2', $output );
239
+ } elseif( preg_match( '/<a[^>]*class\s*=\s*["\'][^"\']*["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>/', $output ) > 0 ) {
240
+ // href comes after class
241
+ $output = preg_replace( '/(<a[^>]*class\s*=\s*["\'][^"\']*)(["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>)/', '$1 '.$class.'$2', $output );
242
+ } else {
243
+ // No previous class
244
+ $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*)(>)/', '$1 class="'.$class.'"$2', $output );
245
+ } // End if we have a class on the a tag or not
246
+
247
+ return $output;
248
+ } // End function add_class()
249
+
250
+ private static function add_target( $needle, $target, $output ) {
251
+ // Clean up our needle for regexing
252
+ $needle = preg_quote( $needle );
253
+ $needle = str_replace( '/', '\/', $needle );
254
+
255
+ // Add a target to the link (or overwrite what's there)
256
+ if( preg_match( '/<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*target\s*=\s*["\'][^"\']*["\'][^>]*>/', $output ) > 0 ) {
257
+ // href comes before target
258
+ $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*target\s*=\s*["\'])[^"\']*(["\'][^>]*>)/', '$1'.$target.'$2', $output );
259
+ } elseif( preg_match( '/<a[^>]*target\s*=\s*["\'][^"\']*["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>/', $output ) > 0 ) {
260
+ // href comes after target
261
+ $output = preg_replace( '/(<a[^>]*target\s*=\s*["\'])[^"\']*(["\'][^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*>)/', '$1'.$target.'$2', $output );
262
+ } else {
263
+ // No previous target
264
+ $output = preg_replace( '/(<a[^>]*href\s*=\s*["\']' . $needle . '["\'][^>]*)(>)/', '$1 target="'.$target.'"$2', $output );
265
+ } // End if we have a class on the a tag or not
266
+
267
+ return $output;
268
+ } // End function add_target()
269
+
270
  } // End class WPGalleryCustomLinks