Page Links To - Version 2.2

Version Description

  • Cleanup, compatibility tweaks to interoperate with a few other plugins, prompt http:// and auto-add it if a URL starts with "www."
Download this release

Release Info

Developer markjaquith
Plugin Icon wp plugin Page Links To
Version 2.2
Comparing to
See all releases

Code changes from version 2.1 to 2.2

Files changed (2) hide show
  1. page-links-to.php +58 -52
  2. readme.txt +28 -5
page-links-to.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Page Links To
4
  Plugin URI: http://txfx.net/code/wordpress/page-links-to/
5
  Description: Allows you to point WordPress pages or posts to a URL of your choosing. Good for setting up navigational links to non-WP sections of your site or to off-site resources.
6
- Version: 2.1
7
  Author: Mark Jaquith
8
  Author URI: http://coveredwebservices.com/
9
  */
@@ -25,17 +25,29 @@ Author URI: http://coveredwebservices.com/
25
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
  */
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  function txfx_get_page_links_to_meta () {
29
  global $wpdb, $page_links_to_cache, $blog_id;
30
 
31
- if ( !isset( $page_links_to_cache[$blog_id] ) ) {
32
- $links_to = $wpdb->get_results(
33
- "SELECT post_id, meta_value " .
34
- "FROM $wpdb->postmeta, $wpdb->posts " .
35
- "WHERE post_id = ID AND meta_key = '_links_to' AND (post_status = 'static' OR post_status = 'publish')");
36
- } else {
37
  return $page_links_to_cache[$blog_id];
38
- }
39
 
40
  if ( !$links_to ) {
41
  $page_links_to_cache[$blog_id] = false;
@@ -51,30 +63,25 @@ function txfx_get_page_links_to_meta () {
51
  function txfx_get_page_links_to_targets () {
52
  global $wpdb, $page_links_to_target_cache, $blog_id;
53
 
54
- if ( !isset( $page_links_to_target_cache[$blog_id] ) ) {
55
- $links_to = $wpdb->get_results(
56
- "SELECT post_id, meta_value " .
57
- "FROM $wpdb->postmeta, $wpdb->posts " .
58
- "WHERE post_id = ID AND meta_key = '_links_to_target' AND (post_status = 'static' OR post_status = 'publish')");
59
- } else {
60
  return $page_links_to_target_cache[$blog_id];
61
- }
62
 
63
  if ( !$links_to ) {
64
  $page_links_to_target_cache[$blog_id] = false;
65
  return false;
66
  }
67
 
68
- foreach ( (array) $links_to as $link ) {
69
  $page_links_to_target_cache[$blog_id][$link->post_id] = $link->meta_value;
70
- }
71
 
72
  return $page_links_to_target_cache[$blog_id];
73
  }
74
 
75
  function txfx_plt_add_meta_box( $page, $context ) {
76
  if ( ( 'page' === $page || 'post' === $page ) && 'advanced' === $context )
77
- add_meta_box('page-links-to', 'Page Links To', 'txfx_plt_meta_box', $page, 'advanced', 'low');
78
  }
79
 
80
  function txfx_plt_meta_box() {
@@ -82,17 +89,23 @@ function txfx_plt_meta_box() {
82
  echo '<p>';
83
  wp_nonce_field( 'txfx_plt', '_txfx_pl2_nonce', false, true );
84
  echo '</p>';
 
 
 
85
  ?>
86
- <p>Point to this URL: <input name="txfx_links_to" type="text" style="width:75%" id="txfx_links_to" value="<?php echo attribute_escape( get_post_meta( $post->ID, '_links_to', true) ); ?>" /></p>
87
- <p><label for="txfx_links_to_new_window"><input type="checkbox" name="txfx_links_to_new_window" id="txfx_links_to_new_window" value="_blank"<?php if ( "_blank" == get_post_meta( $post->ID, '_links_to_target', true ) ) { echo ' checked="checked"'; } ?>> Open this link in a new window</label></p>
88
- <p><label for="txfx_links_to_302"><input type="checkbox" name="txfx_links_to_302" id="txfx_links_to_302" value="302"<?php if ( '302' == get_post_meta( $post->ID, '_links_to_type', true ) ) { echo ' checked="checked"'; } ?>> Use a temporary <code>302</code> redirect (default is a permanent <code>301</code> redirect)</label></p>
89
  <?php
90
  }
91
 
92
  function txfx_plt_save_meta_box( $post_ID ) {
93
  if ( wp_verify_nonce( $_REQUEST['_txfx_pl2_nonce'], 'txfx_plt' ) ) {
94
- if ( isset( $_POST['txfx_links_to'] ) && strlen( $_POST['txfx_links_to'] ) > 0 ) {
95
- update_post_meta( $post_ID, '_links_to', $_POST['txfx_links_to'] );
 
 
 
96
  if ( isset( $_POST['txfx_links_to_new_window'] ) )
97
  update_post_meta( $post_ID, '_links_to_target', '_blank' );
98
  else
@@ -103,6 +116,8 @@ function txfx_plt_save_meta_box( $post_ID ) {
103
  delete_post_meta( $post_ID, '_links_to_type' );
104
  } else {
105
  delete_post_meta( $post_ID, '_links_to' );
 
 
106
  }
107
  }
108
  return $post_ID;
@@ -111,12 +126,12 @@ function txfx_plt_save_meta_box( $post_ID ) {
111
 
112
  function txfx_filter_links_to_pages ($link, $post) {
113
  $page_links_to_cache = txfx_get_page_links_to_meta();
114
-
115
  // Really strange, but page_link gives us an ID and post_link gives us a post object
116
- $id = ($post->ID) ? $post->ID : $post;
117
 
118
  if ( $page_links_to_cache[$id] )
119
- $link = $page_links_to_cache[$id];
120
 
121
  return $link;
122
  }
@@ -133,19 +148,8 @@ function txfx_redirect_links_to_pages() {
133
  return;
134
 
135
  $redirect_type = get_post_meta( $wp_query->post->ID, '_links_to_type', true );
136
-
137
- if ( $redirect_type && $redirect_type != '302' ) {
138
- // Only supporting 301 and 302 for now.
139
- // The others aren't widely supported or needed anyway
140
- header( "HTTP/1.0 301 Moved Permanently" );
141
- header( "Status: 301 Moved Permanently" );
142
- header( "Location: $link" );
143
- exit;
144
- }
145
-
146
- // If we got this far, it's a 302 redirect
147
- header( "Status: 302 Moved Temporarily" );
148
- wp_redirect( $link );
149
  exit;
150
  }
151
 
@@ -165,19 +169,21 @@ function txfx_page_links_to_highlight_tabs( $pages ) {
165
 
166
  if ( str_replace( 'http://www.', 'http://', $this_url ) == str_replace( 'http://www.', 'http://', $page ) || ( is_home() && str_replace( 'http://www.', 'http://', trailingslashit( get_bloginfo( 'home' ) ) ) == str_replace( 'http://www.', 'http://', trailingslashit( $page ) ) ) ) {
167
  $highlight = true;
168
- $current_page = $page;
169
  }
170
  }
171
 
172
  if ( count( $targets ) ) {
173
  foreach ( $targets as $p => $t ) {
 
 
174
  $pages = str_replace( '<a href="' . $p . '" ', '<a href="' . $p . '" target="' . $t . '" ', $pages );
175
  }
176
  }
177
 
178
  if ( $highlight ) {
179
- $pages = str_replace( ' class="page_item current_page_item"', ' class="page_item"', $pages );
180
- $pages = str_replace( '<li class="page_item"><a href="' . $current_page . '"', '<li class="page_item current_page_item"><a href="' . $current_page . '"', $pages );
181
  }
182
 
183
  return $pages;
@@ -186,18 +192,18 @@ function txfx_page_links_to_highlight_tabs( $pages ) {
186
  function txfx_plt_init() {
187
  if ( get_option( 'txfx_plt_schema_version' ) < 3 ) {
188
  global $wpdb;
189
- $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to' WHERE meta_key = 'links_to'" );
190
- $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_target' WHERE meta_key = 'links_to_target'" );
191
- $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_type' WHERE meta_key = 'links_to_type'" );
192
  wp_cache_flush();
193
  update_option( 'txfx_plt_schema_version', 3 );
194
  }
195
  }
196
 
197
- add_filter( 'wp_list_pages', 'txfx_page_links_to_highlight_tabs' );
198
- add_action( 'template_redirect', 'txfx_redirect_links_to_pages' );
199
- add_filter( 'page_link', 'txfx_filter_links_to_pages', 20, 2 );
200
- add_filter( 'post_link', 'txfx_filter_links_to_pages', 20, 2 );
201
- add_action( 'do_meta_boxes', 'txfx_plt_add_meta_box', 10, 2 );
202
- add_action( 'save_post', 'txfx_plt_save_meta_box' );
203
- add_action( 'init', 'txfx_plt_init' );
3
  Plugin Name: Page Links To
4
  Plugin URI: http://txfx.net/code/wordpress/page-links-to/
5
  Description: Allows you to point WordPress pages or posts to a URL of your choosing. Good for setting up navigational links to non-WP sections of your site or to off-site resources.
6
+ Version: 2.2
7
  Author: Mark Jaquith
8
  Author URI: http://coveredwebservices.com/
9
  */
25
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
  */
27
 
28
+ // Compat functions for WP < 2.8
29
+ if ( !function_exists( 'esc_attr' ) ) {
30
+ function esc_attr( $attr ) {
31
+ return attribute_escape( $attr );
32
+ }
33
+
34
+ function esc_url( $url ) {
35
+ return clean_url( $url );
36
+ }
37
+ }
38
+
39
+ function txfx_get_post_meta_by_key( $key ) {
40
+ global $wpdb;
41
+ return $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $key ) );
42
+ }
43
+
44
  function txfx_get_page_links_to_meta () {
45
  global $wpdb, $page_links_to_cache, $blog_id;
46
 
47
+ if ( !isset( $page_links_to_cache[$blog_id] ) )
48
+ $links_to = txfx_get_post_meta_by_key( '_links_to' );
49
+ else
 
 
 
50
  return $page_links_to_cache[$blog_id];
 
51
 
52
  if ( !$links_to ) {
53
  $page_links_to_cache[$blog_id] = false;
63
  function txfx_get_page_links_to_targets () {
64
  global $wpdb, $page_links_to_target_cache, $blog_id;
65
 
66
+ if ( !isset( $page_links_to_target_cache[$blog_id] ) )
67
+ $links_to = txfx_get_post_meta_by_key( '_links_to_target' );
68
+ else
 
 
 
69
  return $page_links_to_target_cache[$blog_id];
 
70
 
71
  if ( !$links_to ) {
72
  $page_links_to_target_cache[$blog_id] = false;
73
  return false;
74
  }
75
 
76
+ foreach ( (array) $links_to as $link )
77
  $page_links_to_target_cache[$blog_id][$link->post_id] = $link->meta_value;
 
78
 
79
  return $page_links_to_target_cache[$blog_id];
80
  }
81
 
82
  function txfx_plt_add_meta_box( $page, $context ) {
83
  if ( ( 'page' === $page || 'post' === $page ) && 'advanced' === $context )
84
+ add_meta_box( 'page-links-to', 'Page Links To', 'txfx_plt_meta_box', $page, 'advanced', 'low' );
85
  }
86
 
87
  function txfx_plt_meta_box() {
89
  echo '<p>';
90
  wp_nonce_field( 'txfx_plt', '_txfx_pl2_nonce', false, true );
91
  echo '</p>';
92
+ $url = get_post_meta( $post->ID, '_links_to', true);
93
+ if ( !$url )
94
+ $url = 'http://';
95
  ?>
96
+ <p>Point to this URL: <input name="txfx_links_to" type="text" style="width:75%" id="txfx_links_to" value="<?php echo attribute_escape( $url ); ?>" /></p>
97
+ <p><label for="txfx_links_to_new_window"><input type="checkbox" name="txfx_links_to_new_window" id="txfx_links_to_new_window" value="_blank" <?php checked( '_blank', get_post_meta( $post->ID, '_links_to_target', true ) ); ?>> Open this link in a new window</label></p>
98
+ <p><label for="txfx_links_to_302"><input type="checkbox" name="txfx_links_to_302" id="txfx_links_to_302" value="302" <?php checked( '302', get_post_meta( $post->ID, '_links_to_type', true ) ); ?>> Use a temporary <code>302</code> redirect (default is a permanent <code>301</code> redirect)</label></p>
99
  <?php
100
  }
101
 
102
  function txfx_plt_save_meta_box( $post_ID ) {
103
  if ( wp_verify_nonce( $_REQUEST['_txfx_pl2_nonce'], 'txfx_plt' ) ) {
104
+ if ( isset( $_POST['txfx_links_to'] ) && strlen( $_POST['txfx_links_to'] ) > 0 && $_POST['txfx_links_to'] !== 'http://' ) {
105
+ $link = stripslashes( $_POST['txfx_links_to'] );
106
+ if ( 0 === strpos( $link, 'www.' ) )
107
+ $link = 'http://' . $link; // Starts with www., so add http://
108
+ update_post_meta( $post_ID, '_links_to', $link );
109
  if ( isset( $_POST['txfx_links_to_new_window'] ) )
110
  update_post_meta( $post_ID, '_links_to_target', '_blank' );
111
  else
116
  delete_post_meta( $post_ID, '_links_to_type' );
117
  } else {
118
  delete_post_meta( $post_ID, '_links_to' );
119
+ delete_post_meta( $post_ID, '_links_to_target' );
120
+ delete_post_meta( $post_ID, '_links_to_type' );
121
  }
122
  }
123
  return $post_ID;
126
 
127
  function txfx_filter_links_to_pages ($link, $post) {
128
  $page_links_to_cache = txfx_get_page_links_to_meta();
129
+
130
  // Really strange, but page_link gives us an ID and post_link gives us a post object
131
+ $id = ( $post->ID ) ? $post->ID : $post;
132
 
133
  if ( $page_links_to_cache[$id] )
134
+ $link = esc_url( $page_links_to_cache[$id] );
135
 
136
  return $link;
137
  }
148
  return;
149
 
150
  $redirect_type = get_post_meta( $wp_query->post->ID, '_links_to_type', true );
151
+ $redirect_type = ( $redirect_type = '302' ) ? '302' : '301';
152
+ wp_redirect( $link, $redirect_type );
 
 
 
 
 
 
 
 
 
 
 
153
  exit;
154
  }
155
 
169
 
170
  if ( str_replace( 'http://www.', 'http://', $this_url ) == str_replace( 'http://www.', 'http://', $page ) || ( is_home() && str_replace( 'http://www.', 'http://', trailingslashit( get_bloginfo( 'home' ) ) ) == str_replace( 'http://www.', 'http://', trailingslashit( $page ) ) ) ) {
171
  $highlight = true;
172
+ $current_page = esc_url( $page );
173
  }
174
  }
175
 
176
  if ( count( $targets ) ) {
177
  foreach ( $targets as $p => $t ) {
178
+ $p = esc_url( $p );
179
+ $t = esc_attr( $t );
180
  $pages = str_replace( '<a href="' . $p . '" ', '<a href="' . $p . '" target="' . $t . '" ', $pages );
181
  }
182
  }
183
 
184
  if ( $highlight ) {
185
+ $pages = preg_replace( '| class="([^"]+)current_page_item"|', ' class="$1"', $pages ); // Kill default highlighting
186
+ $pages = preg_replace( '<li class="([^"]+)"><a href="' . $current_page . '"', '<li class="$1 current_page_item"><a href="' . $current_page . '"', $pages );
187
  }
188
 
189
  return $pages;
192
  function txfx_plt_init() {
193
  if ( get_option( 'txfx_plt_schema_version' ) < 3 ) {
194
  global $wpdb;
195
+ $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to' WHERE meta_key = 'links_to' " );
196
+ $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_target' WHERE meta_key = 'links_to_target' " );
197
+ $wpdb->query( "UPDATE $wpdb->postmeta SET meta_key = '_links_to_type' WHERE meta_key = 'links_to_type' " );
198
  wp_cache_flush();
199
  update_option( 'txfx_plt_schema_version', 3 );
200
  }
201
  }
202
 
203
+ add_filter( 'wp_list_pages', 'txfx_page_links_to_highlight_tabs', 9 );
204
+ add_action( 'template_redirect', 'txfx_redirect_links_to_pages' );
205
+ add_filter( 'page_link', 'txfx_filter_links_to_pages', 20, 2 );
206
+ add_filter( 'post_link', 'txfx_filter_links_to_pages', 20, 2 );
207
+ add_action( 'do_meta_boxes', 'txfx_plt_add_meta_box', 10, 2 );
208
+ add_action( 'save_post', 'txfx_plt_save_meta_box' );
209
+ add_action( 'init', 'txfx_plt_init' );
readme.txt CHANGED
@@ -3,16 +3,18 @@ Contributors: markjaquith
3
  Donate link: http://txfx.net/code/wordpress/
4
  Tags: page, redirect, link, external link, repoint
5
  Requires at least: 2.7
6
- Tested up to: 2.8.4
7
  Stable tag: trunk
8
 
9
- Page Links To allows you to make a WordPress page or post link to a URL of your choosing, instead of its WordPress post or page.
10
 
11
  == Description ==
12
 
13
- Page Links To is a plugin that allows you to make a WordPress page or post link to a URL of your choosing, instead of its WordPress page or post URL. It also will redirect people who go to the old (or "normal") URL to the new one, using a redirect style of your choosing (`301 Moved Permanently` is standard, but you can enable `302 Moved Temporarily` redirects if you wish.)
14
 
15
- This is useful for setting up navigational links to non-WordPress sections of your site or to off-site resources.
 
 
16
 
17
  == Installation ==
18
 
@@ -34,10 +36,31 @@ This is useful for setting up navigational links to non-WordPress sections of yo
34
 
35
  1. The Page Links To meta box in action
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  == Changelog ==
38
 
 
 
 
39
  = 2.1 =
40
- * WordPress MU compatibility for when switch_to_blog() is used... it now uses $blog_id to keep their caches from stomping on each other
41
 
42
  = 2.0 =
43
  * Allow one-character URLs so that things like "#" (dummy link) are possible
3
  Donate link: http://txfx.net/code/wordpress/
4
  Tags: page, redirect, link, external link, repoint
5
  Requires at least: 2.7
6
+ Tested up to: 2.9.1
7
  Stable tag: trunk
8
 
9
+ Lets you make a WordPress page or post link to a URL of your choosing, instead of its WordPress post or page.
10
 
11
  == Description ==
12
 
13
+ This plugin allows you to make a WordPress page or post link to a URL of your choosing, instead of its WordPress page or post URL. It also will redirect people who go to the old (or "normal") URL to the new one you've chosen (`301 Moved Permanently` redirects are standard, but you can choose a `302 Moved Temporarily` redirect if you wish).
14
 
15
+ This functionality is useful for setting up navigational links to non-WordPress sections of your site or to off-site resources.
16
+
17
+ You can also use it to create a hand-crafted menu that links to pages, posts, categories, or anything within your site.
18
 
19
  == Installation ==
20
 
36
 
37
  1. The Page Links To meta box in action
38
 
39
+ == Frequently Asked Questions ==
40
+
41
+ = How do I make it so that a page doesn't link to anything? I'd like to use it as a dummy container. =
42
+
43
+ Just use "#" at the link. That won't go anywhere.
44
+
45
+ = Can this be used to repoint categories to an arbitrary URL? =
46
+
47
+ Not at this time. I'm considering it as a future feature.
48
+
49
+ = My links are sending me to http://myblog.com/www.site-i-wanted-to-link-to.com ... why? =
50
+
51
+ If you want to link to a full URL, you *must* include the `http://` portion.
52
+
53
+ = Can I link to relative URLs? =
54
+
55
+ Yes. Linking to `/my-photos.php` is a good idea, as it'll still work if you move your site to a different domain.
56
+
57
  == Changelog ==
58
 
59
+ = 2.2 =
60
+ * Cleanup, compatibility tweaks to interoperate with a few other plugins, prompt http:// and auto-add it if a URL starts with "www."
61
+
62
  = 2.1 =
63
+ * WordPress MU compatibility for when `switch_to_blog()` is used... it now uses `$blog_id` to keep their caches from stomping on each other
64
 
65
  = 2.0 =
66
  * Allow one-character URLs so that things like "#" (dummy link) are possible