WP Gallery Custom Links - Version 1.0.0

Version Description

  • Initial release

=

Download this release

Release Info

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

Version 1.0.0

Files changed (3) hide show
  1. readme.txt +47 -0
  2. screenshot-1.png +0 -0
  3. wp-gallery-custom-links.php +160 -0
readme.txt ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === WP Gallery Custom Links ===
2
+ Contributors: fourlightsweb
3
+ Donate link: http://www.fourlightsweb.com/wordpress-plugins/wp-gallery-custom-links/#donate
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.0
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ Specifiy custom links for WordPress gallery images (instead of attachment or file only).
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. 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
+
26
+ 1. Upload the 'wp-gallery-custom-links' folder to the '/wp-content/plugins/' directory
27
+ 1. Activate the plugin through the 'Plugins' menu in WordPress
28
+
29
+ == Frequently Asked Questions ==
30
+
31
+ = When will you have FAQs? =
32
+
33
+ As soon as someone asks me something. :)
34
+
35
+ == Screenshots ==
36
+
37
+ 1. The added "Gallery Link URL" field.
38
+
39
+ == Changelog ==
40
+
41
+ = 1.0.0 =
42
+ * Initial release
43
+
44
+ == Upgrade Notice ==
45
+
46
+ = 1.0.0 =
47
+ * Initial release
screenshot-1.png ADDED
Binary file
wp-gallery-custom-links.php ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
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.0
7
+ Author: Four Lights Web Development
8
+ Author URI: http://www.fourlightsweb.com
9
+ License: GPL2
10
+
11
+ Copyright 2012 Four Lights Web Development, LLC. (email : development@fourlightsweb.com)
12
+
13
+ This program is free software; you can redistribute it and/or modify
14
+ it under the terms of the GNU General Public License, version 2, as
15
+ published by the Free Software Foundation.
16
+
17
+ This program is distributed in the hope that it will be useful,
18
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
+ GNU General Public License for more details.
21
+
22
+ You should have received a copy of the GNU General Public License
23
+ along with this program; if not, write to the Free Software
24
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
+ */
26
+
27
+ add_action( 'init', array( 'WPGalleryCustomLinks', 'init' ) );
28
+
29
+ class WPGalleryCustomLinks {
30
+ // We will always be "replacing" the gallery shortcode function
31
+ // via the post_gallery filter, although usually it will be with
32
+ // the gallery shortcode function content itself unless there's
33
+ // an additional theme filter or something.
34
+ // [gallery] ->
35
+ // $GLOBALS['shortcode_tags']['gallery'] ->
36
+ // apply_filter('post_gallery') *
37
+ // apply_filter('post_gallery') (first call) ->
38
+ // $GLOBALS['shortcode_tags']['gallery'] ->
39
+ // apply_filter('post_gallery') *
40
+ // apply_filter('post_gallery') (second call, simply returns output passed in)
41
+ // return "filter" $output to original $GLOBALS['shortcode_tags']['gallery'] call
42
+ private static $first_call = true;
43
+
44
+ public static function init() {
45
+ // Add the filter for editing the custom url field
46
+ add_filter( 'attachment_fields_to_edit', array( 'WPGalleryCustomLinks', 'apply_filter_attachment_fields_to_edit' ), null, 2);
47
+
48
+ // Add the filter for saving the custom url field
49
+ add_filter( 'attachment_fields_to_save', array( 'WPGalleryCustomLinks', 'apply_filter_attachment_fields_to_save' ), null , 2);
50
+
51
+ // Add the filter for when the post_gallery is written out
52
+ add_filter( 'post_gallery', array( 'WPGalleryCustomLinks', 'apply_filter_post_gallery' ), 10, 2 );
53
+ } // End function init()
54
+
55
+ public static function apply_filter_attachment_fields_to_edit( $form_fields, $post ) {
56
+ $form_fields['gallery_link_url'] = array(
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()
64
+
65
+ public static function apply_filter_attachment_fields_to_save( $post, $attachment ) {
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
+
72
+ public static function apply_filter_post_gallery( $output, $attr ) {
73
+ global $post;
74
+
75
+ if( self::$first_call ) {
76
+ // Our first run, so the gallery function thinks it's being
77
+ // overwritten. Set the variable to prevent actual endless
78
+ // overwriting later.
79
+ self::$first_call = false;
80
+ } else {
81
+ // We're inside our dynamically called gallery function and
82
+ // don't want to spiral into an endless loop, so return
83
+ // whatever output we were given so the gallery function
84
+ // will run as normal. Also reset the first call variable
85
+ // so if there's two galleries or something it will run the
86
+ // same next time.
87
+ self::$first_call = true;
88
+ return $output;
89
+ }
90
+
91
+ // Get the normal gallery shortcode function
92
+ if ( isset( $GLOBALS['shortcode_tags'] ) && isset( $GLOBALS['shortcode_tags']['gallery'] ) ) {
93
+ $gallery_shortcode_function = $GLOBALS['shortcode_tags']['gallery'];
94
+ }
95
+
96
+ // Run whatever gallery shortcode function has been set up,
97
+ // default, theme-specified or whatever
98
+ $output = call_user_func( $gallery_shortcode_function, $attr );
99
+
100
+ // Get the shortcode attributes - really we just need "link"
101
+ extract( shortcode_atts( array(), $attr ) );
102
+
103
+ // Get the attachments for this post
104
+ $post_id = intval( $post->ID );
105
+ $attachments = get_children( array( 'post_parent' => $post_id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
106
+ foreach ( $attachments as $id => $attachment ) {
107
+ $link = '';
108
+
109
+ // See if we have a custom url for this attachment image
110
+ $attachment_meta = get_post_meta( $id, '_gallery_link_url', true );
111
+ if( $attachment_meta ) {
112
+ $link = $attachment_meta;
113
+ }
114
+
115
+ if( $link != '' ) {
116
+ // If we have a non-blank custom url, swap out the href on the image
117
+ // in the generated gallery code with the custom url
118
+ if( $attr['link'] == 'file' ) {
119
+ // Get file href
120
+ list( $needle ) = wp_get_attachment_image_src( $id, '' );
121
+ } else {
122
+ // Get the attachment href
123
+ $needle = get_attachment_link( $id );
124
+ } // End if gallery setting is to file or attachment
125
+
126
+ // Build the regex for matching/replacing
127
+ $needle = preg_quote( $needle );
128
+ $needle = str_replace( '/', '\/', $needle );
129
+ $needle = '/href\s*=\s*["\']' . $needle . '["\']/';
130
+ if( preg_match( $needle, $output ) > 0 ) {
131
+ // If we found the href to swap out, perform
132
+ // the href replacement
133
+ $output = preg_replace( $needle, 'href="' . $link . '"', $output );
134
+
135
+ // ...also remove any rel attribute and *box
136
+ // classes so (most) lightboxes won't kick in:
137
+
138
+ // Clean up the href for regex-ing
139
+ $link = preg_quote( $link );
140
+ $link = str_replace( '/', '\/', $link );
141
+
142
+ // href comes before rel
143
+ $output = preg_replace( '/(<a[^>]*href="' . $link . '"[^>]*)rel\s*=\s*["\'][^"\']*["\']([^>]*>)/', '$1$2', $output );
144
+
145
+ // href comes after rel
146
+ $output = preg_replace( '/(<a[^>]*)rel\s*=\s*["\'][^"\']*["\']([^>]*href="' . $link . '"[^>]*>)/', '$1$2', $output );
147
+
148
+ // href comes before class
149
+ $output = preg_replace( '/(<a[^>]*href="' . $link . '"[^>]*class\s*=\s*["\'][^"\']*)box([^"\']*["\'][^>]*>)/', '$1$2', $output );
150
+
151
+ // href comes after class
152
+ $output = preg_replace( '/(<a[^>]*class\s*=\s*["\'][^"\']*)box([^"\']*["\'][^>]*href="' . $link . '"[^>]*>)/', '$1$2', $output );
153
+ } // End if we found the attachment to replace in the output
154
+ } // End if we have a custom url to swap in
155
+ } // End foreach post attachment
156
+
157
+ return $output;
158
+ } // End function apply_custom_links()
159
+
160
+ } // End class WPGalleryCustomLinks