Page Links To - Version 1.5

Version Description

Download this release

Release Info

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

Code changes from version 1.4 to 1.5

Files changed (2) hide show
  1. page-links-to.php +64 -55
  2. readme.txt +4 -4
page-links-to.php CHANGED
@@ -2,13 +2,13 @@
2
  /*
3
  Plugin Name: Page Links To
4
  Plugin URI: http://txfx.net/code/wordpress/page-links-to/
5
- Description: Allows you to set a "links_to" meta key with a URI value that will be be used when listing WP pages. Good for setting up navigational links to non-WP sections of your
6
- Version: 1.4
7
  Author: Mark Jaquith
8
- Author URI: http://txfx.net/
9
  */
10
 
11
- /* Copyright 2005-2006 Mark Jaquith (email: mark.gpl@txfx.net)
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 as published by
@@ -22,26 +22,13 @@ Author URI: http://txfx.net/
22
 
23
  You should have received a copy of the GNU General Public License
24
  along with this program; if not, write to the Free Software
25
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
  */
27
 
28
- /*
29
- === INSTRUCTIONS ===
30
- 1) upload this file to /wp-content/plugins/
31
- 2) activate this plugin in the WordPress interface
32
- 3) create a new page with a title of your choosing, and with the parent page of your choosing. Leave the content of the page blank.
33
- 4) add a meta key "links_to" with a full URI value (like "http://google.com/") (obviously without the quotes)
34
-
35
- That's it! Now, when you use wp_list_page(), that page should link to the "links_to" value, instead of its page
36
-
37
- You can also use links_to_type to set the redirect type (default is 302, but you can specify 301)
38
- You can also use links_to_target to set the target of the link (like _new). This will only work for wp_list_pages()
39
- */
40
-
41
  function txfx_get_page_links_to_meta () {
42
  global $wpdb, $page_links_to_cache;
43
 
44
- if ( !isset($page_links_to_cache) ) {
45
  $links_to = $wpdb->get_results(
46
  "SELECT post_id, meta_value " .
47
  "FROM $wpdb->postmeta, $wpdb->posts " .
@@ -65,7 +52,7 @@ function txfx_get_page_links_to_meta () {
65
  function txfx_get_page_links_to_targets () {
66
  global $wpdb, $page_links_to_target_cache;
67
 
68
- if ( !isset($page_links_to_target_cache) ) {
69
  $links_to = $wpdb->get_results(
70
  "SELECT post_id, meta_value " .
71
  "FROM $wpdb->postmeta, $wpdb->posts " .
@@ -86,6 +73,30 @@ function txfx_get_page_links_to_targets () {
86
  return $page_links_to_target_cache;
87
  }
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  function txfx_filter_links_to_pages ($link, $post) {
90
  $page_links_to_cache = txfx_get_page_links_to_meta();
91
 
@@ -98,38 +109,39 @@ function txfx_filter_links_to_pages ($link, $post) {
98
  return $link;
99
  }
100
 
101
- function txfx_redirect_links_to_pages () {
102
- if ( is_single() || is_page() ) :
103
- global $wp_query;
104
 
105
- $link = get_post_meta($wp_query->post->ID, 'links_to', true);
106
 
107
- if ( !$link )
108
- return;
109
 
110
- $redirect_type = get_post_meta($wp_query->post->ID, 'links_to_type', true);
 
111
 
112
- if ( $redirect_type && $redirect_type != '302' ) {
113
- // Only supporting 301 and 302 for now.
114
- // The others aren't widely supported or needed anyway
115
- header("HTTP/1.0 301 Moved Permanently");
116
- header("Status: 301 Moved Permanently");
117
- header("Location: $link");
118
- exit;
119
- }
120
 
121
- // If we got this far, it's a 302 redirect
122
- header("Status: 302 Moved Temporarily");
123
- wp_redirect($link);
 
 
 
124
  exit;
125
- endif;
 
 
 
 
 
126
  }
127
 
128
- function txfx_page_links_to_highlight_tabs($pages) {
129
  $page_links_to_cache = txfx_get_page_links_to_meta();
130
  $page_links_to_target_cache = txfx_get_page_links_to_targets();
131
 
132
- if ( !$page_links_to_cache && !$page_links_to_target_cache)
133
  return $pages;
134
 
135
  $this_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
@@ -139,33 +151,30 @@ function txfx_page_links_to_highlight_tabs($pages) {
139
  if ( $page_links_to_target_cache[$id] )
140
  $targets[$page] = $page_links_to_target_cache[$id];
141
 
142
- echo "<!--TEST\n";
143
- echo str_replace('http://www.', 'http://', trailingslashit(get_bloginfo('home'))) . "\n";
144
- echo str_replace('http://www.', 'http://', trailingslashit($page)) . "\n";
145
- echo "-->";
146
-
147
- 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)) ) ) {
148
  $highlight = true;
149
  $current_page = $page;
150
  }
151
  }
152
 
153
- if ( count($targets) ) {
154
  foreach ( $targets as $p => $t ) {
155
- $pages = str_replace('<a href="' . $p . '" ', '<a href="' . $p . '" target="' . $t . '" ', $pages);
156
  }
157
  }
158
 
159
  if ( $highlight ) {
160
- $pages = str_replace(' class="page_item current_page_item"', ' class="page_item"', $pages);
161
- $pages = str_replace('<li class="page_item"><a href="' . $current_page . '"', '<li class="page_item current_page_item"><a href="' . $current_page . '"', $pages);
162
  }
163
 
164
  return $pages;
165
  }
166
 
167
- add_filter('wp_list_pages', 'txfx_page_links_to_highlight_tabs');
168
- add_action('template_redirect', 'txfx_redirect_links_to_pages');
169
- add_filter('page_link', 'txfx_filter_links_to_pages', 20, 2);
170
- add_filter('post_link', 'txfx_filter_links_to_pages', 20, 2);
 
 
171
  ?>
2
  /*
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: 1.5
7
  Author: Mark Jaquith
8
+ Author URI: http://coveredwebservices.com/
9
  */
10
 
11
+ /* Copyright 2005-2008 Mark Jaquith (email: mark.gpl@txfx.net)
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 as published by
22
 
23
  You should have received a copy of the GNU General Public License
24
  along with this program; if not, write to the Free Software
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;
30
 
31
+ if ( !isset( $page_links_to_cache ) ) {
32
  $links_to = $wpdb->get_results(
33
  "SELECT post_id, meta_value " .
34
  "FROM $wpdb->postmeta, $wpdb->posts " .
52
  function txfx_get_page_links_to_targets () {
53
  global $wpdb, $page_links_to_target_cache;
54
 
55
+ if ( !isset( $page_links_to_target_cache ) ) {
56
  $links_to = $wpdb->get_results(
57
  "SELECT post_id, meta_value " .
58
  "FROM $wpdb->postmeta, $wpdb->posts " .
73
  return $page_links_to_target_cache;
74
  }
75
 
76
+ function txfx_plt_add_meta_box( $page, $context ) {
77
+ if ( ( 'page' === $page || 'post' === $page ) && 'advanced' === $context )
78
+ add_meta_box('page-links-to', 'Page Links To', 'txfx_plt_meta_box', $page, 'advanced', 'low');
79
+ }
80
+
81
+ function txfx_plt_meta_box() {
82
+ global $post;
83
+
84
+ if ( get_post_meta( $post->ID, 'links_to', true) )
85
+ echo '<p>';
86
+ wp_nonce_field( 'txfx_plt', '_txfx_pl2_nonce', false, true );
87
+ echo '</p>';
88
+ ?>
89
+ Point to this URL: <input name="txfx_links_to" type="text" size="65" id="txfx_links_to" value="<?php echo attribute_escape( get_post_meta( $post->ID, 'links_to', true) ); ?>" />
90
+ <?php
91
+ }
92
+
93
+ function txfx_plt_save_meta_box( $post_ID ) {
94
+ if ( wp_verify_nonce( $_REQUEST['_txfx_pl2_nonce'], 'txfx_plt' ) && isset( $_POST['txfx_links_to'] ) && strlen( $_POST['txfx_links_to'] ) > 7 )
95
+ update_post_meta( $post_ID, 'links_to', $_POST['txfx_links_to'] );
96
+ return $post_ID;
97
+ }
98
+
99
+
100
  function txfx_filter_links_to_pages ($link, $post) {
101
  $page_links_to_cache = txfx_get_page_links_to_meta();
102
 
109
  return $link;
110
  }
111
 
112
+ function txfx_redirect_links_to_pages() {
113
+ if ( !is_single() && !is_page() )
114
+ return;
115
 
116
+ global $wp_query;
117
 
118
+ $link = get_post_meta( $wp_query->post->ID, 'links_to', true );
 
119
 
120
+ if ( !$link )
121
+ return;
122
 
123
+ $redirect_type = get_post_meta( $wp_query->post->ID, 'links_to_type', true );
 
 
 
 
 
 
 
124
 
125
+ if ( $redirect_type && $redirect_type != '302' ) {
126
+ // Only supporting 301 and 302 for now.
127
+ // The others aren't widely supported or needed anyway
128
+ header( "HTTP/1.0 301 Moved Permanently" );
129
+ header( "Status: 301 Moved Permanently" );
130
+ header( "Location: $link" );
131
  exit;
132
+ }
133
+
134
+ // If we got this far, it's a 302 redirect
135
+ header( "Status: 302 Moved Temporarily" );
136
+ wp_redirect( $link );
137
+ exit;
138
  }
139
 
140
+ function txfx_page_links_to_highlight_tabs( $pages ) {
141
  $page_links_to_cache = txfx_get_page_links_to_meta();
142
  $page_links_to_target_cache = txfx_get_page_links_to_targets();
143
 
144
+ if ( !$page_links_to_cache && !$page_links_to_target_cache )
145
  return $pages;
146
 
147
  $this_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
151
  if ( $page_links_to_target_cache[$id] )
152
  $targets[$page] = $page_links_to_target_cache[$id];
153
 
154
+ 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 ) ) ) ) {
 
 
 
 
 
155
  $highlight = true;
156
  $current_page = $page;
157
  }
158
  }
159
 
160
+ if ( count( $targets ) ) {
161
  foreach ( $targets as $p => $t ) {
162
+ $pages = str_replace( '<a href="' . $p . '" ', '<a href="' . $p . '" target="' . $t . '" ', $pages );
163
  }
164
  }
165
 
166
  if ( $highlight ) {
167
+ $pages = str_replace( ' class="page_item current_page_item"', ' class="page_item"', $pages );
168
+ $pages = str_replace( '<li class="page_item"><a href="' . $current_page . '"', '<li class="page_item current_page_item"><a href="' . $current_page . '"', $pages );
169
  }
170
 
171
  return $pages;
172
  }
173
 
174
+ add_filter( 'wp_list_pages', 'txfx_page_links_to_highlight_tabs' );
175
+ add_action( 'template_redirect', 'txfx_redirect_links_to_pages' );
176
+ add_filter( 'page_link', 'txfx_filter_links_to_pages', 20, 2 );
177
+ add_filter( 'post_link', 'txfx_filter_links_to_pages', 20, 2 );
178
+ add_action( 'do_meta_boxes', 'txfx_plt_add_meta_box', 10, 2 );
179
+ add_action( 'save_post', 'txfx_plt_save_meta_box' );
180
  ?>
readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: markjaquith
3
  Donate link: http://txfx.net/code/wordpress/
4
  Tags:
5
  Requires at least: 1.5.1.3
6
- Tested up to: 2.3.3
7
  Stable tag: trunk
8
 
9
  Page Links To allows you to make certain WordPress pages or posts link to a URL of your choosing, instead of their WordPress post or page.
@@ -20,8 +20,8 @@ Page Links To is a plugin that allows you to make certain WordPress pages or pos
20
 
21
  3. Create (or edit) a page to have a title of your choosing, and a parent page of your choosing (leave the content blank)
22
 
23
- 4. Down below, add a meta key of `links_to` and give a full URL as its value
24
 
25
- 5. Done! Now that post/page will point to the URL you chose.
26
 
27
- 6. Optionally, you can create a `links_to_target` meta key, and provide the target you would like for the link (like `_new`, to open the link in a new window).
3
  Donate link: http://txfx.net/code/wordpress/
4
  Tags:
5
  Requires at least: 1.5.1.3
6
+ Tested up to: 2.6
7
  Stable tag: trunk
8
 
9
  Page Links To allows you to make certain WordPress pages or posts link to a URL of your choosing, instead of their WordPress post or page.
20
 
21
  3. Create (or edit) a page to have a title of your choosing, and a parent page of your choosing (leave the content blank)
22
 
23
+ 4. Down below, in the advanced section, find the Page Links To widget and add a URL of your choosing, and save the post
24
 
25
+ 5. Done! Now that post/page will point to the URL you chose
26
 
27
+ 6. Optionally, you can create a `links_to_target` Custom Field, and provide the target you would like for the link (like `_new`, to open the link in a new window)