Quick Page/Post Redirect Plugin - Version 1.5

Version Description

  • Major re-Write of the plugin core function to hook WP at a later time to take advantage of the POST function - no sense re-creating the wheel.
  • Removed the 'no code' redirect, as it turns out, many browsers will not redirect properly without a code - sorry guys.
  • Can have page/post as draft and still redirect - but ONLY after the post/page has first been published and then re-saved as draft (this will hopefully be a fix for a later version). (8/31/2009)
Download this release

Release Info

Developer prophecy2040
Plugin Icon 128x128 Quick Page/Post Redirect Plugin
Version 1.5
Comparing to
See all releases

Code changes from version 1.4 to 1.5

Files changed (2) hide show
  1. page_post_redirect_plugin.php +71 -111
  2. readme.txt +10 -5
page_post_redirect_plugin.php CHANGED
@@ -2,12 +2,15 @@
2
  /*
3
  Plugin Name: Quick Page/Post Redirect
4
  Plugin URI: http://fischercreativemedia.com/wordpress-plugins/quick-pagepost-redirct-plugin/
5
- Description: Redirect Pages or Posts to another location quickly. Adds a redirect box to the page or post edit page where you can specify the redirect Location and type. Location can be to another WordPress page/post or an external URL. NOTE: Page or post must remain published for this to work correctly.
6
  Author: Don Fischer
7
  Author URI: http://www.fischercreativemedia.com/
8
- Version: 1.4
9
 
10
  Version info:
 
 
 
11
  1.4 - Add exit after header redirect function - needed on some servers and browsers. (8/19/2009)
12
  1.3 - Add Meta Re-fresh option (7/26/2009)
13
  1.2 - Add easy Post/Page Edit Box (7/25/2009)
@@ -34,76 +37,36 @@ Version info:
34
  //----------
35
  add_action('admin_menu', 'add_edit_box_ppr');
36
  add_action('save_post', 'ppr_save_postdata', 1, 2); // save the custom fields
37
-
 
38
  // Variables
39
  //----------
40
- global $wpdb;
41
- global $wp_query;
42
- $page_post_redirect_current = split('/', page_post_redirect_get_currentURL());
43
- if(count($page_post_redirect_current)>=3){
44
- $page_post_redirect_after_base = $page_post_redirect_current[count($page_post_redirect_current)-2];
45
- }else{
46
- $page_post_redirect_after_base =''; //either home or no permalinks on
47
- }
48
- $page_post_redirect_meta = page_post_redirect_get_meta();
49
-
50
- // Functions
51
- //----------
52
- function ppr_get_ID_by_page_name($page_name){
53
- global $wpdb;
54
- $page_name_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '$page_name'");
55
- return $page_name_id;
56
- }
57
- function page_post_redirect_get_meta(){
58
  global $wpdb;
59
- $page_post_redirect_limit = (int) apply_filters( 'postmeta_form_limit', 30 );
60
- $query_for_meta = "SELECT postm.post_id,postm.meta_value,postp.post_type,postp.post_name FROM $wpdb->postmeta postm, $wpdb->posts postp WHERE (postm.meta_key='pprredirect_active' and postm.meta_value='1') and (postp.post_type='page' OR postp.post_type='post') and (postp.post_status='publish' OR postp.post_status='private') and postm.post_id=postp.ID ORDER BY postm.post_id";
61
- $page_post_redirect_keys = $wpdb->get_results ($query_for_meta, ARRAY_A);
62
- return $page_post_redirect_keys;
63
- }
64
 
65
- function page_post_redirect_get_currentURL() {
66
- $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
67
- $protocol = page_post_redirect_strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
68
- $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
69
- return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
70
- }
71
-
72
- function page_post_redirect_strleft($s1, $s2) {
73
- return substr($s1, 0, strpos($s1, $s2));
74
  }
75
-
76
-
77
- if(count($page_post_redirect_meta)>0){
78
- foreach($page_post_redirect_meta as $page_post_redirect_to){
79
- $ppr_postpage=$page_post_redirect_to["post_type"];
80
- $ppr_id=$page_post_redirect_to["post_id"];
81
- $ppr_url=get_post_meta($ppr_id,'pprredirect_url',true);
82
- $ppr_type=get_post_meta($ppr_id,'pprredirect_type',true);
83
- $ppr_postname=$page_post_redirect_to["post_name"];
84
- if($ppr_type==0){$ppr_type='200';}
85
- //check if the page that is loading needs a redirect.
86
- if (isset($_GET['page_id'])){
87
- // no permalinks - using page ids
88
- $thisidis_current=$_GET['page_id'];
89
- }elseif(isset($_GET['p'])){
90
- // no permalinks - using post ids
91
- $thisidis_current=$_GET['p'];
92
- }else{
93
- if($page_post_redirect_after_base==''){
94
- // either home or no permalinks
95
-
96
- }else{
97
- $thisidis_current = ppr_get_ID_by_page_name($page_post_redirect_after_base);
98
- }
99
- }
100
- if ($thisidis_current!='' || $page_post_redirect_after_base === $ppr_postname || $page_post_redirect_after_base == $ppr_id){
101
- //echo $thisidis_current .'|'.$ppr_id.'<br />';
102
- if($ppr_type=='meta' && $thisidis_current === $ppr_id){
103
  //metaredirect
104
- echo '<meta http-equiv="refresh" content="0; URL='.get_bloginfo('url').$ppr_url.'">';
105
  exit; //stop loading page so meta can do it's job without rest of page loading.
106
- }elseif ($thisidis_current == $ppr_id){
107
  //check for http:// - as full url - then we can just redirect if it is //
108
  if( strpos($ppr_url, 'http://')=== 0 || strpos($ppr_url, 'https://')=== 0){
109
  $offsite=$ppr_url;
@@ -127,22 +90,20 @@ global $wp_query;
127
  header("Location: $onsite", true, $ppr_type);
128
  exit; //stop loading page
129
  }
130
-
131
  //check permalink or local page redirect
132
  }else{ // we assume they are using the permalink / page name??
133
  $onsite=get_bloginfo('url'). $ppr_url;
134
  header("Location: $onsite", true, $ppr_type);
135
  exit; //stop loading page
136
  }
137
- }else{
138
- //echo "nothing - we don't redirect";
139
- }
140
- }
141
- }
142
  }
143
-
144
-
145
  // Add options to post/page edit pages
 
146
  // Adds a custom section to the Post and Page edit screens
147
  function add_edit_box_ppr() {
148
  if( function_exists( 'add_meta_box' )) {
@@ -164,11 +125,11 @@ global $wp_query;
164
  // The actual fields for data entry
165
  if(get_post_meta($post->ID, 'pprredirect_active', true)!=''){$pprediractive="checked";}else{$pprediractive="";}
166
  echo '<input type="checkbox" name="pprredirect_active" value="1" '.$pprediractive.' /> ';
167
- echo '<label for="pprredirect_active">' . __(" Active?", 'sp' ) . '</label> (check to turn on)<br /><br />';
168
 
169
  echo '<label for="pprredirect_url">' . __(" Redirect URL:", 'sp' ) . '</label><br />';
170
  if(get_post_meta($post->ID, 'pprredirect_url', true)!=''){$pprredirecturl=get_post_meta($post->ID, 'pprredirect_url', true);}else{$pprredirecturl="";}
171
- echo '<input type="text" style="width:75%;margin-top:2px;margin-bottom:2px;" name="pprredirect_url" value="'.$pprredirecturl.'" /><br />(i.e., <code>http://example.com</code> or <code>/somepage/</code> or <code>p=15</code> or <code>155</code>. Use full URL including http:// for all external redirects. )<br /><br />';
172
 
173
  echo '<label for="pprredirect_type">' . __(" Type of Redirect:", 'sp' ) . '</label> ';
174
  if(get_post_meta($post->ID, 'pprredirect_type', true)!=''){$pprredirecttype=get_post_meta($post->ID, 'pprredirect_type', true);}else{$pprredirecttype="";}
@@ -185,54 +146,53 @@ global $wp_query;
185
  case "307":
186
  $ppr_option3=" selected";
187
  break;
188
- case "0":
189
- $ppr_option4=" selected";
190
- break;
191
  case "meta":
192
  $ppr_option5=" selected";
193
  break;
194
  endswitch;
195
 
196
- echo '<select style="margin-top:2px;margin-bottom:2px;" name="pprredirect_type"><option value="301" '.$ppr_option1.'>301 Permanent</option><option value="302" '.$ppr_option2.'>302 Temporary</option><option value="307" '.$ppr_option3.'>307 Temporary</option><option value="0" '.$ppr_option4.'>use no code</option><option value="meta" '.$ppr_option5.'>do meta redirect</option></select> (301 Permanent, 302 or 307 Temporary, no code or Meta Redirect. default is 302)<br /><br />';
197
 
198
  }
199
  // When the post is saved, saves our custom data
200
  function ppr_save_postdata($post_id, $post) {
 
 
 
 
 
 
 
201
 
202
- // verify authorization
203
- if ( !wp_verify_nonce( $_POST['pprredirect_noncename'], plugin_basename(__FILE__) )) {
204
- return $post->ID;
205
- }
206
-
207
- // check allowed to editing
208
- if ( 'page' == $_POST['post_type'] ) {
209
- if ( !current_user_can( 'edit_page', $post->ID ))
210
- return $post->ID;
211
- } else {
212
- if ( !current_user_can( 'edit_post', $post->ID ))
213
- return $post->ID;
214
- }
215
-
216
- // find & save the form data
217
- // Put it into an array
218
- $mydata['pprredirect_active'] = $_POST['pprredirect_active'];
219
- $mydata['pprredirect_url'] = $_POST['pprredirect_url'];
220
- if($mydata['pprredirect_url']!=''){
221
- $mydata['pprredirect_type'] = $_POST['pprredirect_type'];
222
- }
223
 
224
- // Add values of $mydata as custom fields
225
- foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!
226
- if( $post->post_type == 'revision' ) return; //don't store custom data twice
227
- $value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)
228
- if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value
229
- update_post_meta($post->ID, $key, $value);
230
- } else { //if the custom field doesn't have a value
231
- add_post_meta($post->ID, $key, $value);
232
  }
233
- if(!$value) delete_post_meta($post->ID, $key); //delete if blank
234
- }
235
-
 
 
 
 
 
 
 
 
 
 
236
  }
237
 
238
  ?>
2
  /*
3
  Plugin Name: Quick Page/Post Redirect
4
  Plugin URI: http://fischercreativemedia.com/wordpress-plugins/quick-pagepost-redirct-plugin/
5
+ Description: Redirect Pages or Posts to another location quickly. Adds a redirect box to the page or post edit page where you can specify the redirect Location and type. Location can be to another WordPress page/post or an external URL. UPDATE: Page or post NO LONGER needs to be published for this to work correctly.
6
  Author: Don Fischer
7
  Author URI: http://www.fischercreativemedia.com/
8
+ Version: 1.5
9
 
10
  Version info:
11
+ 1.5 - Re-Write plugin core function to hook WP at a later time to take advantage of the POST function - no sense re-creating the wheel.
12
+ Can have page/post as draft and still redirect - but ONLY after the post/page has first been published and
13
+ then re-saved as draft (this will hopefully be a fix for a later version). (8/31/2009)
14
  1.4 - Add exit after header redirect function - needed on some servers and browsers. (8/19/2009)
15
  1.3 - Add Meta Re-fresh option (7/26/2009)
16
  1.2 - Add easy Post/Page Edit Box (7/25/2009)
37
  //----------
38
  add_action('admin_menu', 'add_edit_box_ppr');
39
  add_action('save_post', 'ppr_save_postdata', 1, 2); // save the custom fields
40
+ add_action('wp','ppr_do_redirect', 1, 2);
41
+
42
  // Variables
43
  //----------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  global $wpdb;
45
+ global $wp_query;
 
 
 
 
46
 
47
+ // Functions
48
+ //----------
49
+ function addmetatohead_theme(){
50
+ global $ppr_url;
51
+ $meta_code = '<meta http-equiv="refresh" content="0; URL='.$ppr_url.'" />';
52
+ echo $meta_code;
 
 
 
53
  }
54
+
55
+ function ppr_do_redirect(){
56
+ global $post,$wp_query,$ppr_url,$ppr_active,$ppr_url,$ppr_type;
57
+ $thisidis_current= $post->ID;
58
+ $ppr_active=get_post_meta($thisidis_current,'pprredirect_active',true);
59
+ $ppr_url=get_post_meta($thisidis_current,'pprredirect_url',true);
60
+ $ppr_type=get_post_meta($thisidis_current,'pprredirect_type',true);
61
+
62
+ if( $ppr_active==1 && $ppr_url!='' ):
63
+ if($ppr_type===0){$ppr_type='200';}
64
+ if($ppr_type===''){$ppr_type='302';}
65
+ if($ppr_type=='meta'):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  //metaredirect
67
+ add_action('wp_head', "addmetatohead_theme",1);
68
  exit; //stop loading page so meta can do it's job without rest of page loading.
69
+ elseif($ppr_url!=''):
70
  //check for http:// - as full url - then we can just redirect if it is //
71
  if( strpos($ppr_url, 'http://')=== 0 || strpos($ppr_url, 'https://')=== 0){
72
  $offsite=$ppr_url;
90
  header("Location: $onsite", true, $ppr_type);
91
  exit; //stop loading page
92
  }
 
93
  //check permalink or local page redirect
94
  }else{ // we assume they are using the permalink / page name??
95
  $onsite=get_bloginfo('url'). $ppr_url;
96
  header("Location: $onsite", true, $ppr_type);
97
  exit; //stop loading page
98
  }
99
+
100
+ endif;
101
+ endif;
 
 
102
  }
103
+
104
+ //=======================================
105
  // Add options to post/page edit pages
106
+ //=======================================
107
  // Adds a custom section to the Post and Page edit screens
108
  function add_edit_box_ppr() {
109
  if( function_exists( 'add_meta_box' )) {
125
  // The actual fields for data entry
126
  if(get_post_meta($post->ID, 'pprredirect_active', true)!=''){$pprediractive="checked";}else{$pprediractive="";}
127
  echo '<input type="checkbox" name="pprredirect_active" value="1" '.$pprediractive.' /> ';
128
+ echo '<label for="pprredirect_active">' . __(" Active?", 'sp' ) . '</label> (check to turn on) <b>NOTE:</b> The page or post needs to be published for the redirect to happen <i><b>UNLESS</b></i> you publish it first, then save it as a Draft..<br /><br />';
129
 
130
  echo '<label for="pprredirect_url">' . __(" Redirect URL:", 'sp' ) . '</label><br />';
131
  if(get_post_meta($post->ID, 'pprredirect_url', true)!=''){$pprredirecturl=get_post_meta($post->ID, 'pprredirect_url', true);}else{$pprredirecturl="";}
132
+ echo '<input type="text" style="width:75%;margin-top:2px;margin-bottom:2px;" name="pprredirect_url" value="'.$pprredirecturl.'" /><br />(i.e., <code>http://example.com</code> or <code>/somepage/</code> or <code>p=15</code> or <code>155</code>. Use <b>FULL URL</b> <i>including</i> <code>http://</code> for all external <i>and</i> meta redirects. )<br /><br />';
133
 
134
  echo '<label for="pprredirect_type">' . __(" Type of Redirect:", 'sp' ) . '</label> ';
135
  if(get_post_meta($post->ID, 'pprredirect_type', true)!=''){$pprredirecttype=get_post_meta($post->ID, 'pprredirect_type', true);}else{$pprredirecttype="";}
146
  case "307":
147
  $ppr_option3=" selected";
148
  break;
 
 
 
149
  case "meta":
150
  $ppr_option5=" selected";
151
  break;
152
  endswitch;
153
 
154
+ echo '<select style="margin-top:2px;margin-bottom:2px;" name="pprredirect_type"><option value="301" '.$ppr_option1.'>301 Permanent</option><option value="302" '.$ppr_option2.'>302 Temporary</option><option value="307" '.$ppr_option3.'>307 Temporary</option><option value="meta" '.$ppr_option5.'>Meta Redirect</option></select> (Default is 302)<br /><br />';
155
 
156
  }
157
  // When the post is saved, saves our custom data
158
  function ppr_save_postdata($post_id, $post) {
159
+ if(isset($_POST['pprredirect_active']) || isset($_POST['pprredirect_url']) || isset($_POST['pprredirect_type']) ):
160
+ // verify authorization
161
+ if(isset($_POST['pprredirect_noncename'])){
162
+ if ( !wp_verify_nonce( $_POST['pprredirect_noncename'], plugin_basename(__FILE__) )) {
163
+ return $post->ID;
164
+ }
165
+ }
166
 
167
+ // check allowed to editing
168
+ if ( 'page' == $_POST['post_type'] ) {
169
+ if ( !current_user_can( 'edit_page', $post->ID ))
170
+ return $post->ID;
171
+ } else {
172
+ if ( !current_user_can( 'edit_post', $post->ID ))
173
+ return $post->ID;
174
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
+ // find & save the form data
177
+ // Put it into an array
178
+ $mydata['pprredirect_active'] = $_POST['pprredirect_active'];
179
+ $mydata['pprredirect_url'] = $_POST['pprredirect_url'];
180
+ if($mydata['pprredirect_url']!=''){
181
+ $mydata['pprredirect_type'] = $_POST['pprredirect_type'];
 
 
182
  }
183
+
184
+ // Add values of $mydata as custom fields
185
+ foreach ($mydata as $key => $value) { //Let's cycle through the $mydata array!
186
+ if( $post->post_type == 'revision' ) return; //don't store custom data twice
187
+ $value = implode(',', (array)$value); //if $value is an array, make it a CSV (unlikely)
188
+ if(get_post_meta($post->ID, $key, FALSE)) { //if the custom field already has a value
189
+ update_post_meta($post->ID, $key, $value);
190
+ } else { //if the custom field doesn't have a value
191
+ add_post_meta($post->ID, $key, $value);
192
+ }
193
+ if(!$value) delete_post_meta($post->ID, $key); //delete if blank
194
+ }
195
+ endif;
196
  }
197
 
198
  ?>
readme.txt CHANGED
@@ -4,7 +4,7 @@ Donate link: http://fischercreativemedia.com/
4
  Tags: redirect, 301, 302, meta, post, plugin, page, forward, re-direct
5
  Requires at least: 2.5
6
  Tested up to: 2.8.4
7
- Stable tag: 1.4
8
 
9
  Redirect Pages/Posts to another page/post or external URL. Adds edit box to admin edit so user can specify the redirect Location and type.
10
 
@@ -15,7 +15,9 @@ It adds an option box to the edit section where you can specify the redirect loc
15
 
16
  The redirect Location can be to another WordPress page/post or any other website with an external URL. It allows the use of a full URL path, the post or page ID, permalink or page-name.
17
 
18
- PLEASE NOTE: At this time, the page or post needs to remain Published in order for the redirect to happen. This plugin is also not compatible with WordPress versions less than 2.5, and has not been tested with WordPress MU. If anyone using WPMU has tested this plugin and would like to let me know, I would love to know your results.
 
 
19
 
20
  == Installation ==
21
 
@@ -32,12 +34,11 @@ PLEASE NOTE: At this time, the page or post needs to remain Published in order f
32
  == Frequently Asked Questions ==
33
 
34
  = Does the Page/Post need to be Published to redirect? =
35
- Yes. At this time, you need to keep the page or post published for the redirect to work. If it is not published, you will yet the same 404 error you would get if you turned off the page or post.
36
 
37
  = Can I do a permanent 301 Redirect? =
38
 
39
- Yes. You can perform a 301 Permanent Redirect. Additionally, you can select a 302 Temporary or a 307 Temporary redirect, a no code redirect which gives the
40
- browser a 200 status code for the redirect, or a Meta redirect.
41
 
42
  = What the heck is a 301 or 302 redirect anyway? =
43
 
@@ -66,6 +67,10 @@ Yes, you can, but you do not always need to. If you are redirecting to an extern
66
 
67
 
68
  == Changelog ==
 
 
 
 
69
  = 1.4 =
70
  * Add exit script command after header redirect function - needed on some servers and browsers. (8/19/2009)
71
  = 1.3 =
4
  Tags: redirect, 301, 302, meta, post, plugin, page, forward, re-direct
5
  Requires at least: 2.5
6
  Tested up to: 2.8.4
7
+ Stable tag: 1.5
8
 
9
  Redirect Pages/Posts to another page/post or external URL. Adds edit box to admin edit so user can specify the redirect Location and type.
10
 
15
 
16
  The redirect Location can be to another WordPress page/post or any other website with an external URL. It allows the use of a full URL path, the post or page ID, permalink or page-name.
17
 
18
+ PLEASE NOTE: At this time, the a new page or post needs to be Published in order for the redirect to happen. It WILL work on a DRAFT Status Post/Page ONLY, and I mean ONLY, if the Post/Page has FIRST been Published and the re-saved as a Draft - WordPress does not set up the post meta for the permalink until after first publish for some reason - will hopefully fix this in the future.
19
+
20
+ This plugin is also not compatible with WordPress versions less than 2.5, and has not been tested with WordPress MU. If anyone using WPMU has tested this plugin and would like to let me know, I would love to know your results.
21
 
22
  == Installation ==
23
 
34
  == Frequently Asked Questions ==
35
 
36
  = Does the Page/Post need to be Published to redirect? =
37
+ YES... and NO... The redirect will always work on a Published Post/Page. For it to work correctly on a Post/Page in DRAFT status, you need to fist publish the page, then re-save it as a draft. If you don't follow that step, you will get a 404 error.
38
 
39
  = Can I do a permanent 301 Redirect? =
40
 
41
+ Yes. You can perform a 301 Permanent Redirect. Additionally, you can select a 302 Temporary or a 307 Temporary redirect or a Meta redirect.
 
42
 
43
  = What the heck is a 301 or 302 redirect anyway? =
44
 
67
 
68
 
69
  == Changelog ==
70
+ = 1.5 =
71
+ * Major re-Write of the plugin core function to hook WP at a later time to take advantage of the POST function - no sense re-creating the wheel.
72
+ * Removed the 'no code' redirect, as it turns out, many browsers will not redirect properly without a code - sorry guys.
73
+ * Can have page/post as draft and still redirect - but ONLY after the post/page has first been published and then re-saved as draft (this will hopefully be a fix for a later version). (8/31/2009)
74
  = 1.4 =
75
  * Add exit script command after header redirect function - needed on some servers and browsers. (8/19/2009)
76
  = 1.3 =