Page Links To - Version 1.4

Version Description

Download this release

Release Info

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

Code changes from version 1.0 to 1.4

Files changed (2) hide show
  1. page-links-to.php +170 -54
  2. readme.txt +27 -0
page-links-to.php CHANGED
@@ -1,55 +1,171 @@
1
- <?php
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.0
7
- Author URI: http://txfx.net/
8
- */
9
-
10
- /*
11
- === INSTRUCTIONS ===
12
- 1) upload this file to /wp-content/plugins/
13
- 2) activate this plugin in the WordPress interface
14
- 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.
15
- 4) add a meta key "links_to" with a full URI value (like "http://google.com/") (obviously without the quotes)
16
-
17
- That's it! Now, when you use wp_list_page(), that page should link to the "links_to" value, instead of its page
18
- */
19
-
20
- function txfx_get_page_links_to_meta () {
21
- global $wpdb, $page_links_to_cache;
22
-
23
- if (!isset($page_links_to_cache)) {
24
-
25
- $links_to = $wpdb->get_results(
26
- "SELECT post_id, meta_value " .
27
- "FROM $wpdb->postmeta, $wpdb->posts " .
28
- "WHERE post_id = ID AND meta_key = 'links_to' AND post_status = 'static'");
29
- } else {
30
- return $page_links_to_cache;
31
- }
32
-
33
- if (!$links_to) {
34
- $page_links_to_cache = false;
35
- return false;
36
- }
37
-
38
- foreach ($links_to as $link) {
39
- $page_links_to_cache[$link->post_id] = $link->meta_value;
40
- }
41
-
42
- return $page_links_to_cache;
43
- }
44
-
45
- function txfx_filter_links_to_pages ($link, $page_id) {
46
- $page_links_to_cache = txfx_get_page_links_to_meta();
47
-
48
- if ( $page_links_to_cache[$page_id] )
49
- $link = $page_links_to_cache[$page_id];
50
-
51
- return $link;
52
- }
53
-
54
- add_filter('page_link', 'txfx_filter_links_to_pages', 10, 2);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  ?>
1
+ <?php
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
15
+ the Free Software Foundation; either version 2 of the License, or
16
+ (at your option) any later version.
17
+
18
+ This program is distributed in the hope that it will be useful,
19
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ GNU General Public License for more details.
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 " .
48
+ "WHERE post_id = ID AND meta_key = 'links_to' AND (post_status = 'static' OR post_status = 'publish')");
49
+ } else {
50
+ return $page_links_to_cache;
51
+ }
52
+
53
+ if ( !$links_to ) {
54
+ $page_links_to_cache = false;
55
+ return false;
56
+ }
57
+
58
+ foreach ( (array) $links_to as $link ) {
59
+ $page_links_to_cache[$link->post_id] = $link->meta_value;
60
+ }
61
+
62
+ return $page_links_to_cache;
63
+ }
64
+
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 " .
72
+ "WHERE post_id = ID AND meta_key = 'links_to_target' AND (post_status = 'static' OR post_status = 'publish')");
73
+ } else {
74
+ return $page_links_to_target_cache;
75
+ }
76
+
77
+ if ( !$links_to ) {
78
+ $page_links_to_target_cache = false;
79
+ return false;
80
+ }
81
+
82
+ foreach ( (array) $links_to as $link ) {
83
+ $page_links_to_target_cache[$link->post_id] = $link->meta_value;
84
+ }
85
+
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
+
92
+ // Really strange, but page_link gives us an ID and post_link gives us a post object
93
+ $id = ($post->ID) ? $post->ID : $post;
94
+
95
+ if ( $page_links_to_cache[$id] )
96
+ $link = $page_links_to_cache[$id];
97
+
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'];
136
+ $targets = array();
137
+
138
+ foreach ( (array) $page_links_to_cache as $id => $page ) {
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
  ?>
readme.txt ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Plugin Name ===
2
+ 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.
10
+
11
+ == Description ==
12
+
13
+ Page Links To is a plugin that allows you to make certain WordPress pages or posts link to a URL of your choosing, instead of their 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 (`302 Moved Temporarily` is standard, but you can enable `301 Moved Permanently` redirects if you wish.)
14
+
15
+ == Installation ==
16
+
17
+ 1. Upload `page-links-to.php` to your `/wp-content/plugins/` directory
18
+
19
+ 2. Activate the "Page Links To" plugin in your WordPress administration interface
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).