Custom Permalinks - Version 0.4

Version Description

Download this release

Release Info

Developer michaeltyson
Plugin Icon Custom Permalinks
Version 0.4
Comparing to
See all releases

Code changes from version 0.3.1 to 0.4

Files changed (2) hide show
  1. custom-permalinks.php +92 -15
  2. readme.txt +5 -3
custom-permalinks.php CHANGED
@@ -4,7 +4,7 @@ Plugin Name: Custom Permalinks
4
  Plugin URI: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
5
  Donate link: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
6
  Description: Set custom permalinks on a per-post basis
7
- Version: 0.3.1
8
  Author: Michael Tyson
9
  Author URI: http://michael.tyson.id.au
10
  */
@@ -48,6 +48,22 @@ function custom_permalinks_post_link($permalink, $post) {
48
  }
49
 
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  /**
52
  * Filter to replace the term permalink with the custom one
53
  *
@@ -86,10 +102,10 @@ function custom_permalinks_redirect() {
86
  $original_permalink = '';
87
 
88
  // If the post/tag/category we're on has a custom permalink, get it and check against the request
89
- if ( is_single() ) {
90
  $post = $wp_query->post;
91
  $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
92
- $original_permalink = custom_permalinks_original_post_link( $post->ID );
93
  } else if ( is_tag() || is_category() ) {
94
  $theTerm = $wp_query->get_queried_object();
95
  $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
@@ -106,6 +122,7 @@ function custom_permalinks_redirect() {
106
  trim($request,'/') != trim($original_permalink,'/') ) {
107
  // This is the original link; we can use this url to derive the new one
108
  $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request));
 
109
  }
110
  wp_redirect( get_option('home')."/".$url, 301 );
111
  exit();
@@ -133,7 +150,7 @@ function custom_permalinks_request($query) {
133
 
134
  if ( !$request ) return $query;
135
 
136
- $sql = "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value FROM $wpdb->posts ".
137
  " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
138
  " meta_key = 'custom_permalink' AND ".
139
  " meta_value != '' AND ".
@@ -148,9 +165,11 @@ function custom_permalinks_request($query) {
148
  if ( trim($request,'/') == trim($posts[0]->meta_value,'/') )
149
  $_CPRegisteredURL = $request;
150
 
151
- $originalUrl = str_replace(trim($posts[0]->meta_value,'/'),
152
- custom_permalinks_original_post_link($posts[0]->ID),
153
- trim($request,'/'));
 
 
154
  }
155
 
156
  if ( !$originalUrl ) {
@@ -180,20 +199,37 @@ function custom_permalinks_request($query) {
180
  }
181
 
182
  if ( $originalUrl ) {
183
- $originalUrl = preg_replace("@//*@", "/", $originalUrl);
 
 
 
 
184
 
185
  // Now we have the original URL, run this back through WP->parse_request, in order to
186
  // parse parameters properly. We set $_SERVER variables to fool the function.
187
- $oldPathInfo = $_SERVER["PATH_INFO"]; $oldRequestUri = $_SERVER["REQUEST_URI"];
188
- $_SERVER["PATH_INFO"] = $originalUrl; $_SERVER["REQUEST_URI"] = $originalUrl;
 
 
 
 
 
 
 
 
189
 
 
190
  remove_filter( 'request', 'custom_permalinks_request', 10, 1 );
191
  global $wp;
192
  $wp->parse_request();
193
  $query = $wp->query_vars;
194
  add_filter( 'request', 'custom_permalinks_request', 10, 1 );
195
 
196
- $_SERVER["PATH_INFO"] = $oldPathInfo; $_SERVER["REQUEST_URI"] = $oldRequestUri;
 
 
 
 
197
  }
198
  return $query;
199
  }
@@ -245,6 +281,32 @@ function custom_permalinks_post_options() {
245
  }
246
 
247
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
248
  /**
249
  * Per-category/tag options
250
  *
@@ -403,6 +465,7 @@ function custom_permalinks_options_page() {
403
  list($kind, $id) = explode('.', $identifier);
404
  switch ( $kind ) {
405
  case 'post':
 
406
  delete_post_meta( $id, 'custom_permalink' );
407
  break;
408
  case 'tag':
@@ -492,7 +555,7 @@ function custom_permalinks_admin_rows() {
492
  }
493
  }
494
 
495
- // List posts
496
  global $wpdb;
497
  $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
498
  "$wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != ''";
@@ -501,9 +564,9 @@ function custom_permalinks_admin_rows() {
501
  $row = array();
502
  $row['id'] = 'post.'.$post->ID;
503
  $row['permalink'] = get_permalink($post->ID);
504
- $row['type'] = 'Post';
505
  $row['title'] = $post->post_title;
506
- $row['editlink'] = 'post.php?action=edit&post='.$post->ID;
507
  $rows[] = $row;
508
  }
509
 
@@ -512,7 +575,7 @@ function custom_permalinks_admin_rows() {
512
 
513
 
514
  /**
515
- * Get original permalink
516
  *
517
  * @package CustomPermalinks
518
  * @since 0.1
@@ -524,6 +587,18 @@ function custom_permalinks_original_post_link($post_id) {
524
  return $originalPermalink;
525
  }
526
 
 
 
 
 
 
 
 
 
 
 
 
 
527
 
528
 
529
  /**
@@ -584,12 +659,14 @@ function custom_permalinks_setup_admin() {
584
 
585
  add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
586
  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
 
587
  add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
588
  add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
589
  add_filter( 'request', 'custom_permalinks_request', 10, 1 );
590
  add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
591
 
592
  add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
 
593
  add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
594
  add_action( 'edit_category_form', 'custom_permalinks_term_options' );
595
  add_action( 'save_post', 'custom_permalinks_save_post' );
4
  Plugin URI: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
5
  Donate link: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
6
  Description: Set custom permalinks on a per-post basis
7
+ Version: 0.4
8
  Author: Michael Tyson
9
  Author URI: http://michael.tyson.id.au
10
  */
48
  }
49
 
50
 
51
+ /**
52
+ * Filter to replace the page permalink with the custom one
53
+ *
54
+ * @package CustomPermalinks
55
+ * @since 0.4
56
+ */
57
+ function custom_permalinks_page_link($permalink, $page) {
58
+ $custom_permalink = get_post_meta( $page, 'custom_permalink', true );
59
+ if ( $custom_permalink ) {
60
+ return get_option('home')."/".$custom_permalink;
61
+ }
62
+
63
+ return $permalink;
64
+ }
65
+
66
+
67
  /**
68
  * Filter to replace the term permalink with the custom one
69
  *
102
  $original_permalink = '';
103
 
104
  // If the post/tag/category we're on has a custom permalink, get it and check against the request
105
+ if ( is_single() || is_page() ) {
106
  $post = $wp_query->post;
107
  $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
108
+ $original_permalink = ( $post->post_type == 'post' ? custom_permalinks_original_post_link( $post->ID ) : custom_permalinks_original_page_link( $post->ID ) );
109
  } else if ( is_tag() || is_category() ) {
110
  $theTerm = $wp_query->get_queried_object();
111
  $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
122
  trim($request,'/') != trim($original_permalink,'/') ) {
123
  // This is the original link; we can use this url to derive the new one
124
  $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request));
125
+ $url = preg_replace('@([^?]*)&@', '\1?', $url);
126
  }
127
  wp_redirect( get_option('home')."/".$url, 301 );
128
  exit();
150
 
151
  if ( !$request ) return $query;
152
 
153
+ $sql = "SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type FROM $wpdb->posts ".
154
  " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
155
  " meta_key = 'custom_permalink' AND ".
156
  " meta_value != '' AND ".
165
  if ( trim($request,'/') == trim($posts[0]->meta_value,'/') )
166
  $_CPRegisteredURL = $request;
167
 
168
+ $originalUrl = str_replace( trim($posts[0]->meta_value,'/' ),
169
+ ( $posts[0]->post_type == 'post' ?
170
+ custom_permalinks_original_post_link($posts[0]->ID)
171
+ : custom_permalinks_original_page_link($posts[0]->ID) ),
172
+ trim( $request,'/' ) );
173
  }
174
 
175
  if ( !$originalUrl ) {
199
  }
200
 
201
  if ( $originalUrl ) {
202
+
203
+ if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) {
204
+ $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1);
205
+ $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars;
206
+ }
207
 
208
  // Now we have the original URL, run this back through WP->parse_request, in order to
209
  // parse parameters properly. We set $_SERVER variables to fool the function.
210
+ $oldPathInfo = $_SERVER['PATH_INFO']; $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING'];
211
+ $_SERVER['PATH_INFO'] = $originalUrl; $_SERVER['REQUEST_URI'] = $originalUrl;
212
+ $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : '');
213
+ parse_str($_SERVER['QUERY_STRING'], $queryArray);
214
+ $oldValues = array();
215
+ if ( is_array($queryArray) )
216
+ foreach ( $queryArray as $key => $value ) {
217
+ $oldValues[$key] = $_REQUEST[$key];
218
+ $_REQUEST[$key] = $_GET[$key] = $value;
219
+ }
220
 
221
+ // Re-run the filter, now with original environment in place
222
  remove_filter( 'request', 'custom_permalinks_request', 10, 1 );
223
  global $wp;
224
  $wp->parse_request();
225
  $query = $wp->query_vars;
226
  add_filter( 'request', 'custom_permalinks_request', 10, 1 );
227
 
228
+ // Restore values
229
+ $_SERVER['PATH_INFO'] = $oldPathInfo; $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString;
230
+ foreach ( $oldValues as $key => $value ) {
231
+ $_REQUEST[$key] = $value;
232
+ }
233
  }
234
  return $query;
235
  }
281
  }
282
 
283
 
284
+ /**
285
+ * Per-page options
286
+ *
287
+ * @package CustomPermalinks
288
+ * @since 0.4
289
+ */
290
+ function custom_permalinks_page_options() {
291
+ global $post;
292
+ $post_id = $post;
293
+ if (is_object($post_id)) {
294
+ $post_id = $post_id->ID;
295
+ }
296
+
297
+ $permalink = get_post_meta( $post_id, 'custom_permalink', true );
298
+
299
+ ?>
300
+ <div class="postbox closed">
301
+ <h3><?php _e('Custom Permalink', 'custom-permalink') ?></h3>
302
+ <div class="inside">
303
+ <?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?>
304
+ </div>
305
+ </div>
306
+ <?php
307
+ }
308
+
309
+
310
  /**
311
  * Per-category/tag options
312
  *
465
  list($kind, $id) = explode('.', $identifier);
466
  switch ( $kind ) {
467
  case 'post':
468
+ case 'page':
469
  delete_post_meta( $id, 'custom_permalink' );
470
  break;
471
  case 'tag':
555
  }
556
  }
557
 
558
+ // List posts/pages
559
  global $wpdb;
560
  $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
561
  "$wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != ''";
564
  $row = array();
565
  $row['id'] = 'post.'.$post->ID;
566
  $row['permalink'] = get_permalink($post->ID);
567
+ $row['type'] = ucwords( $post->post_type );
568
  $row['title'] = $post->post_title;
569
+ $row['editlink'] = ( $post->post_type == 'post' ? 'post.php' : 'page.php' ) . '?action=edit&post='.$post->ID;
570
  $rows[] = $row;
571
  }
572
 
575
 
576
 
577
  /**
578
+ * Get original permalink for post
579
  *
580
  * @package CustomPermalinks
581
  * @since 0.1
587
  return $originalPermalink;
588
  }
589
 
590
+ /**
591
+ * Get original permalink for page
592
+ *
593
+ * @package CustomPermalinks
594
+ * @since 0.4
595
+ */
596
+ function custom_permalinks_original_page_link($post_id) {
597
+ remove_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
598
+ $originalPermalink = ltrim(str_replace(get_option('home'), '', get_permalink( $post_id )), '/');
599
+ add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
600
+ return $originalPermalink;
601
+ }
602
 
603
 
604
  /**
659
 
660
  add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
661
  add_filter( 'post_link', 'custom_permalinks_post_link', 10, 2 );
662
+ add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
663
  add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
664
  add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
665
  add_filter( 'request', 'custom_permalinks_request', 10, 1 );
666
  add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
667
 
668
  add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
669
+ add_action( 'edit_page_form', 'custom_permalinks_page_options' );
670
  add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
671
  add_action( 'edit_category_form', 'custom_permalinks_term_options' );
672
  add_action( 'save_post', 'custom_permalinks_save_post' );
readme.txt CHANGED
@@ -4,13 +4,13 @@ Donate link: http://michael.tyson.id.au/wordpress/plugins/custom-permalinks
4
  Tags: permalink, url, link, address, custom, redirect
5
  Requires at least: 2.6
6
  Tested up to: 2.6.2
7
- Stable tag: 0.3.1
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
11
  == Description ==
12
 
13
- Lay out your site the way *you* want it. Set the URL of any post, tag or category to anything you want.
14
  Old permalinks will redirect properly to the new address. Custom Permalinks gives you ultimate control
15
  over your site structure.
16
 
@@ -19,10 +19,12 @@ over your site structure.
19
 
20
  1. Unzip the package, and upload `custom-permalinks` to the `/wp-content/plugins/` directory
21
  2. Activate the plugin through the 'Plugins' menu in WordPress
22
- 3. Edit any post, tag or category to set a custom permalink.
23
 
24
  == Changelog ==
25
 
 
 
26
  0.3.1: Discovered a typo that broke categories
27
 
28
  0.3: Largely rewritten to provide more robust handling of trailing slashes, proper support for trailing URL components (eg. paging)
4
  Tags: permalink, url, link, address, custom, redirect
5
  Requires at least: 2.6
6
  Tested up to: 2.6.2
7
+ Stable tag: 0.4
8
 
9
  Set custom permalinks on a per-post, per-tag or per-category basis.
10
 
11
  == Description ==
12
 
13
+ Lay out your site the way *you* want it. Set the URL of any post, page, tag or category to anything you want.
14
  Old permalinks will redirect properly to the new address. Custom Permalinks gives you ultimate control
15
  over your site structure.
16
 
19
 
20
  1. Unzip the package, and upload `custom-permalinks` to the `/wp-content/plugins/` directory
21
  2. Activate the plugin through the 'Plugins' menu in WordPress
22
+ 3. Edit any post, page, tag or category to set a custom permalink.
23
 
24
  == Changelog ==
25
 
26
+ 0.4: Support for pages, and a fix for draft posts/pages
27
+
28
  0.3.1: Discovered a typo that broke categories
29
 
30
  0.3: Largely rewritten to provide more robust handling of trailing slashes, proper support for trailing URL components (eg. paging)